如何处理 MVVM 应用程序中的构造函数过度注入
How to deal with constructor over-injection in MVVM application
我一直在阅读有关构造函数过度注入的问题。
这一切都是有道理的,这是一个没有正确遵循 SRP 等的标志。(顺便说一句,我正在使用 Ninject!)
但是,我很难理解在我的案例中如何解决这个问题。
最大的问题是在我的视图模型中,我在其中注入 DTO 映射器和存储库以与我的属性一起使用。
这是我的视图模型构造函数的示例:
public MainViewModel(
IGenericRepository<MainDbContext, Product> productRepository,
IGenericRepository<MainDbContext, Person> personRepository,
IGenericRepository<MainDbContext, Order> orderRepository,
ProductMapper productMapper,
PersonMapper personMapper,
OrderMapper orderMapper,
IViewModelLoader viewModelLoader,
IEventAggregator eventAggregator)
{
_productRepository = productRepository;
_personRepository = personRepository;
_orderRepository = orderRepository;
_productMapper = productMapper;
_personMapper = personMapper;
_orderMapper = orderMapper;
_viewModelLoader = viewModelLoader;
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
我的猜测是我没有正确使用 repositories/mappers,应该将它们移出视图模型...我不确定确切位置或方式。
这就是我提问的原因。
应用程序的架构如下所示:
Company.Product.Core
Company.Product.DataAccess
Company.Product.Domain
Company.Product.Presentation
GenericRepository 放在里面 Company.Product.DataAccess.Repositories
和 Company.Product.Domain.Mappers
内的映射器
看构造函数列表,好像是成对出现的:
productRepository
/productMapper
personRepository
/personMapper
orderRepository
/orderMapper
这似乎表明 MainViewModel
构成了与产品、人员和订单相关的内容。
您能否改为对其建模,使其由其他三个视图模型组成?假设 ProductViewModel
, PersonViewModel
, OrderViewModel
类..?
难道一定要是这三个类? MainViewModel
可以组合任意数量的其他视图模型吗?
这会将构造函数简化为如下所示:
public MainViewModel(
IReadOnlyCollection<IViewModel> viewModels,
IViewModelLoader viewModelLoader,
IEventAggregator eventAggregator)
这似乎更合理。
这本质上是 Composite pattern 的一个实现,它通常非常适合 UI 建模 - 它通常被称为 Composite UIs.
另一件让我想知道的事情是 IVewModelLoader
做了什么?
我一直在阅读有关构造函数过度注入的问题。 这一切都是有道理的,这是一个没有正确遵循 SRP 等的标志。(顺便说一句,我正在使用 Ninject!)
但是,我很难理解在我的案例中如何解决这个问题。 最大的问题是在我的视图模型中,我在其中注入 DTO 映射器和存储库以与我的属性一起使用。
这是我的视图模型构造函数的示例:
public MainViewModel(
IGenericRepository<MainDbContext, Product> productRepository,
IGenericRepository<MainDbContext, Person> personRepository,
IGenericRepository<MainDbContext, Order> orderRepository,
ProductMapper productMapper,
PersonMapper personMapper,
OrderMapper orderMapper,
IViewModelLoader viewModelLoader,
IEventAggregator eventAggregator)
{
_productRepository = productRepository;
_personRepository = personRepository;
_orderRepository = orderRepository;
_productMapper = productMapper;
_personMapper = personMapper;
_orderMapper = orderMapper;
_viewModelLoader = viewModelLoader;
_eventAggregator = eventAggregator;
_eventAggregator.Subscribe(this);
}
我的猜测是我没有正确使用 repositories/mappers,应该将它们移出视图模型...我不确定确切位置或方式。 这就是我提问的原因。
应用程序的架构如下所示:
Company.Product.Core
Company.Product.DataAccess
Company.Product.Domain
Company.Product.Presentation
GenericRepository 放在里面 Company.Product.DataAccess.Repositories 和 Company.Product.Domain.Mappers
内的映射器看构造函数列表,好像是成对出现的:
productRepository
/productMapper
personRepository
/personMapper
orderRepository
/orderMapper
这似乎表明 MainViewModel
构成了与产品、人员和订单相关的内容。
您能否改为对其建模,使其由其他三个视图模型组成?假设 ProductViewModel
, PersonViewModel
, OrderViewModel
类..?
难道一定要是这三个类? MainViewModel
可以组合任意数量的其他视图模型吗?
这会将构造函数简化为如下所示:
public MainViewModel(
IReadOnlyCollection<IViewModel> viewModels,
IViewModelLoader viewModelLoader,
IEventAggregator eventAggregator)
这似乎更合理。
这本质上是 Composite pattern 的一个实现,它通常非常适合 UI 建模 - 它通常被称为 Composite UIs.
另一件让我想知道的事情是 IVewModelLoader
做了什么?