簡體   English   中英

Unity依賴注入-具有通用存儲庫返回類型的方法

[英]Unity Dependency Injection - Method with Generic Repository return type

我在弄清楚如何使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 ,所以分配CustomerRepositoryRepository<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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM