WPF Prism.Unity - 如何使用命名依赖注入构建 ViewModel
WPF Prism.Unity - how to construct a ViewModel using named dependency injection
使用Prism
7.1,我可以注册两个命名依赖项:
containerRegistry.Register<ICustomersService, CustomersFakeService>("One");
containerRegistry.Register<ICustomersService, CustomersFakeService>("Two");
现在 - 我如何在 ViewModel
构造函数中使用它们中的任何一个?
public CustomersViewModel(ICustomersService customersService, EventAggregator eventAggregator)
抛出异常:
Resolution of the dependency failed, type =
'WPFPrismDI.ViewModels.CustomersViewModel', name = '(none)'. Exception
occurred while: while resolving. Exception is:
InvalidOperationException - The current type,
WPFPrismDI.Services.ICustomersService, is an interface and cannot be
constructed. Are you missing a type mapping?
----------------------------------------------- At the time of the exception, the container was: Resolving
WPFPrismDI.ViewModels.CustomersViewModel,(none) Resolving parameter
'customersService' of constructor
WPFPrismDI.ViewModels.CustomersViewModel(WPFPrismDI.Services.ICustomersService
customersService, Prism.Events.EventAggregator eventAggregator)
Resolving WPFPrismDI.Services.ICustomersService,(none)
我的服务实现:
public interface ICustomersService
{
ObservableCollection<CustomerModel> CustomerGetAll();
}
public class CustomersDBService : ICustomersService
{
public ObservableCollection<CustomerModel> CustomerGetAll()
{
ObservableCollection<CustomerModel> returnValue = new ObservableCollection<CustomerModel>();
returnValue.Add(new CustomerModel() { NameFirst = "DB", });
return returnValue;
}
}
Now - how can I use any of them in CustomersViewModel
constructor?
通过全部注入:
public CustomersViewModel(ICustomersService[] customersServices, IEventAggregator eventAggregator)
使用Prism
7.1,我可以注册两个命名依赖项:
containerRegistry.Register<ICustomersService, CustomersFakeService>("One");
containerRegistry.Register<ICustomersService, CustomersFakeService>("Two");
现在 - 我如何在 ViewModel
构造函数中使用它们中的任何一个?
public CustomersViewModel(ICustomersService customersService, EventAggregator eventAggregator)
抛出异常:
Resolution of the dependency failed, type = 'WPFPrismDI.ViewModels.CustomersViewModel', name = '(none)'. Exception occurred while: while resolving. Exception is: InvalidOperationException - The current type, WPFPrismDI.Services.ICustomersService, is an interface and cannot be constructed. Are you missing a type mapping? ----------------------------------------------- At the time of the exception, the container was: Resolving WPFPrismDI.ViewModels.CustomersViewModel,(none) Resolving parameter 'customersService' of constructor WPFPrismDI.ViewModels.CustomersViewModel(WPFPrismDI.Services.ICustomersService customersService, Prism.Events.EventAggregator eventAggregator) Resolving WPFPrismDI.Services.ICustomersService,(none)
我的服务实现:
public interface ICustomersService
{
ObservableCollection<CustomerModel> CustomerGetAll();
}
public class CustomersDBService : ICustomersService
{
public ObservableCollection<CustomerModel> CustomerGetAll()
{
ObservableCollection<CustomerModel> returnValue = new ObservableCollection<CustomerModel>();
returnValue.Add(new CustomerModel() { NameFirst = "DB", });
return returnValue;
}
}
Now - how can I use any of them in
CustomersViewModel
constructor?
通过全部注入:
public CustomersViewModel(ICustomersService[] customersServices, IEventAggregator eventAggregator)