Unity依赖注入-具有通用存储库返回类型的方法
本文关键字:存储 返回类型 方法 依赖 注入 -具 Unity | 更新日期: 2023-09-27 17:54:58
我很难弄清楚如何让Unity与我的通用存储库一起工作。下面是我的基本设置:
public static Repository<IEntity> GetRepository()
{
//This is of type CustomerRepository
return unitOfWork.CustomerRepository;
}
CustomerRepository : Repository<Customer>, ICustomerRepository
Repository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity
ICustomerRepository : IRepository<Customer>
IRepository<TEntity> : IDisposable where TEntity : class, IEntity
Customer : IEntity
返回unitOfWork的错误。CustomerRepository是:
不能隐式地将类型'CustomerRepository'转换为'Repository'
我完全迷路了,我已经尝试了很多方法来注册它,我开始认为我需要做一些其他的事情来实现我想要的…现在还不知道我在用RegisterTypes做什么,如您所见:
container.RegisterType<IEntity, Customer>();
container.RegisterType(typeof(Repository<Customer>), typeof(CustomerRepository));
container.RegisterType(typeof(CustomerRepository), typeof(Repository<Customer>));
container.RegisterType(typeof(Repository<IEntity>), typeof(Repository<Customer>));
container.RegisterType(typeof(Repository<Customer>), typeof(Repository<IEntity>));
container.RegisterType(typeof(Repository<>), typeof(Repository<IEntity>));
container.RegisterType(typeof(Repository<IEntity>), typeof(Repository<>));
//---------
container.RegisterType(typeof(IRepository<Customer>), typeof(Repository<IEntity>));
container.RegisterType<IRepository<Customer>, CustomerRepository>();
container.RegisterType(typeof(Repository<IEntity>), typeof(CustomerRepository));
//--------------------
container.RegisterType(typeof(IRepository<>), typeof(Repository<>));
container.RegisterType<ICustomerRepository, CustomerRepository>();
container.RegisterType<IRepository<Customer>, ICustomerRepository>();
container.RegisterType<Repository<Customer>, CustomerRepository>();
container.RegisterType<IRepository<Customer>, CustomerRepository>();
container.RegisterType<IRepository<Customer>, ICustomerRepository>();
container.RegisterType<Repository<Customer>, CustomerRepository>();
container.RegisterType<IRepository<Customer>, CustomerRepository>();
container.RegisterType(typeof(IRepository<IEntity>), typeof(IRepository<Customer>));
这些注册方式都没有帮助。
编辑1:
我怎样才能使它工作?:
public static Repository<TEntity> GetRepository<TEntity>(string entityName, string conectionName) where TEntity : class, IEntity
{
var unitOfWork = new UnitOfWork(connectionName);
switch(entityName){
case "Customer":
return unitOfWork.CustomerRepository;
}
return null;
}
如果我理解正确,并将您的代码分解为更多行,您正在这样做:
public static Repository<IEntity> GetRepository()
{
CustomerRepository repository1 = unitOfWork.CustomerRepository;
Repository<IEntity> repository2 = repository1;
return repository2;
}
因此,您将间接地将CustomerRepository
类型的值赋给Repository<IEntity>
类型,这将不起作用,因为Repository<IEntity>
不是CustomerRepository
的基本类型。另一方面,Repository<Customer>
类型是CustomerRepository
的基类,因此将CustomerRepository
赋值给Repository<Customer>
也可以。
* EDIT *
根据你的评论,一些新的见解:
public static Repository<TEntity> GetRepository<TEntity>()
where TEntity : class, IEntity
{
if (typeof(TEntity) == typeof(Customer))
return (Repository<TEntity>)(object)new Repository<Customer>();
if (typeof(TEntity) == typeof(Product))
return (Repository<TEntity>)(object)new Repository<Product>();
throw new Exception("Entity of type " + typeof(IEntity).Name + " is not supported.");
}
还是……使用反射(较慢):
public static Repository<TEntity> GetRepository<TEntity>()
where TEntity : class, IEntity
{
Type repositoryType = typeof(Repository<>).MakeGenericType(typeof(TEntity));
return (Repository<TEntity>)Activator.CreateInstance(repositoryType);
}