我在下面的代码中注册了两次IRespository
(带有名称):
// Setup the Client Repository
IOC.Container.RegisterType<ClientEntities>(new InjectionConstructor());
IOC.Container.RegisterType<IRepository, GenericRepository>
("Client", new InjectionConstructor(typeof(ClientEntities)));
// Setup the Customer Repository
IOC.Container.RegisterType<CustomerEntities>(new InjectionConstructor());
IOC.Container.RegisterType<IRepository, GenericRepository>
("Customer", new InjectionConstructor(typeof(CustomerEntities)));
IOC.Container.RegisterType<IClientModel, ClientModel>();
IOC.Container.RegisterType<ICustomerModel, CustomerModel>();
但是,当我想要解决这个问题(使用IRepository
)时,我必须像这样手动解决:
public ClientModel(IUnityContainer container)
{
this.dataAccess = container.Resolve<IRepository>(Client);
.....
}
我想要做的是在构造函数中解析它(就像IUnityContainer
一样)。我需要一些方法来说明要解析到哪个命名类型。
如下所示:(注意:不是真正的代码)
public ClientModel([NamedDependancy("Client")] IRepository dataAccess)
{
this.dataAccess = dataAccess;
.....
}
有没有办法让我的伪代码正常工作?
https://stackoverflow.com/questions/7046779
复制相似问题