我一直在阅读有关构造函数过度注入的问题。 这一切都是有道理的,这是一个标志,SRP没有被正确地遵循等等(我正在使用Ninject!)如何在MVVM应用程序中处理构造函数过度注入
但是,我很难理解如何解决这个问题在我的案件。 最大的问题是在我的视图模型中,我注入了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);
}
我的猜测是,我无法正常使用库/映射器,他们应该被移出视图模型的......我我不确定究竟在哪里或如何。 这是我的问题的原因。
应用程序的体系结构是这样的:
Company.Product.Core
Company.Product.DataAccess
Company.Product.Domain
Company.Product.Presentation
的GenericRepository放在里面Company.Product.DataAccess.Repositories 和映射器内Company.Product.Domain.Mappers
您是否从一个窗口/视图访问产品,人员和订单? –
@YacoubMassad。上面的存储库已被重命名为一些示例。但我真正的构造函数看起来像这样,是的,他们都在一个视图中访问。^ –
为什么你注入你的构造函数?你在使用某种IoC容器吗? – Lance