在一个非泛型工厂类中缓存泛型类型应该怎么办

shuyoufeng shuyoufeng 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
需求如下:

在框架层面有个抽象的仓储类:

public abstract class Repository<T> : IRepository<T> where T : EntityBase
{
}

// 有一个仓储的工厂类:
internal class RepositoryFactory
{
// 这里该如何声明缓存字段?
private IDictionary<Type, IRepository<>> repoCache = new Dictionary<Type, IRepository<>>();

public IRepository<T> FindByEntity<T>(Type anEntityType)
{
// 这里先通过 repoCache 查找,如果没有则新建并缓存。
throw new NotImplementedException();
}
}


RepositoryFactory 这个类主要是框架内部使用,用于根据实体类型找到仓储。
现在的问题是如果 repoCache 字段不使用HasTable而使用Dictionary<>该如何声明。

使用框架时可以为每个聚合新建仓储类型,比如:
public class UserRepository: Repository<User>{}

...全文
评论 2
收藏 已收藏
分享
返回顶部
shuyoufeng
C# 111,130 成员 642,539 内容