gpt4 book ai didi

c# - 在 C# 中转换为泛型类型

转载 作者:行者123 更新时间:2023-11-30 22:17:04 27 4
gpt4 key购买 nike

我们在 C# 中有一个抽象泛型类,非常像这样:

public abstract class Repository<T>
where T: Entity
{
public abstract void Create(T t);
public abstract T Retrieve(Id id);
//etc.
}

我们有几个派生类,例如:

public class EventRepository
: Repository<Event>
{
//etc.
}

我们正在实现一个工作单元模式,该模式保留一个字典以将实体类型映射到存储库类型,以便在需要创建或更改实体时,它知道要实例化哪个存储库:

private Dictionary<Type, Type> m_dicMapper;

该字典已初始化并加载了所有映射,如下所示:

m_dicMapper.Add(typeof(Event), typeof(EventRepository));
//and so on for a few other repository classes.

然后,当一个实体e需要创建,例如:

//retrieve the repository type for the correct entity type.
Type tyRepo = m_dicMapper[e.GetType()];
//instantiate a repository of that type.
repo = Activator.CreateInstance(tyRepo);
//and now create the entity in persistence.
repo.Create(e);

问题是,repo 是什么类型?在上面的代码中?我想将其声明为通用 Repository<T>类型,但显然 C# 不允许我这样做。以下行均未编译:

Repository repo;
Repository<T> repo;
Repository<e.GetType()> repo;

我可以将其声明为 var , 但后来我无法访问 Create和其他方法 Repository<T>实现。我希望能够使用通用类来通用地使用存储库!但我想我做错了什么。

所以我的问题是,我可以使用哪些编码和/或设计变通办法来解决这个问题?谢谢。

最佳答案

我个人建议使用单独的单例类封装您对字典的访问,然后包装您的字典 getter 和 setter,类似于 RepositoryStore类。

我建议更改 Dictionary<Type, Type>Dictionary<Type, object> , 然后处理 RepositoryStore 内的类型转换;是这样的吗?

更新(使用 TypeLazy<T> )

如果您使用的是 .NET 4,则可以充分利用 Lazy类,并将字典类型更改为 IDictionary<Type, Lazy<object>> .我修改了我原来的答案以反射(reflect)这可能是如何工作的:

class RepositoryStore
{
private IDictionary<Type, Lazy<object>> Repositories { get; set; }

public RepositoryStore()
{
this.Repositories = new Dictionary<Type, Lazy<object>>();
}

public RepositoryStore Add<T, TRepo>() where TRepo : Repository<T>
{
this.Repositories[typeof(T)] = new Lazy<object>(() => Activator.CreateInstance(typeof(TRepo)));
return this;
}

public Repository<T> GetRepository<T>()
{
if (this.Repositories.ContainsKey(typeof(T)))
{
return this.Repositories[typeof(T)].Value as Repository<T>;
}

throw new KeyNotFoundException("Unable to find repository for type: " + typeof(T).Name);
}
}

用法就很简单了...

var repositoryStore = new RepositoryStore()
// ... set up the repository store, in the singleton?

Repository<MyObject> myObjectRepository = repositoryStore.GetRepository<MyObject>();
myObjectRepository.Create(new MyObject());

关于c# - 在 C# 中转换为泛型类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16962687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com