我无法弄清楚如何让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时的错误是:
无法隐式转换类型&#39; CustomerRepository&#39;到&#39;存储库&#39;
我完全迷失了,我已经尝试了很多方式来注册它,我开始认为我需要做一些其他事情来实现我想要的东西......不知道我在使用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;
}
答案 0 :(得分:0)
如果我理解正确,并且在更多行中分解你的代码,那么你就是这样做的:
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>
即可。
*编辑*
根据您的意见,提供一些新的见解:
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);
}