如何在MVVM应用程序中处理构造函数的过度注入(How to deal with constructor over

编程入门 行业动态 更新时间:2024-10-27 20:24:52
如何在MVVM应用程序中处理构造函数的过度注入(How to deal with constructor over-injection in MVVM application)

我一直在阅读有关构造函数过度注入的问题。 这一切都是有道理的,这是SRP未被正确遵循的标志等。(我顺便使用Ninject!)

但是,我很难理解如何在我的案例中解决这个问题。 最大的问题是在我的viewmodel中,我注入DTO映射器和存储库以与我的属性一起使用。

以下是我的viewmodel构造函数的示例:

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.Domain.Mappers中的Company.Product.DataAccess.Repositories和mappers中。

I have been reading about the problem of constructor over-injection. It all makes sense that this is a sign where the SRP is not being followed correctly etc. (I am using Ninject by the way!)

However, I am having a hard time understanding how this can be solved in my case. The biggest problem is in my viewmodel where I am injecting DTO mappers and Repositories to be used with my properties.

Here is an example of what my viewmodel constructor could look like:

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); }

My guess is that I am not using the repositories/mappers correctly and they should be moved out of the viewmodel... I'm not sure exactly where or how. Which is the reason for my question.

The architecture of the application looks like this:

Company.Product.Core Company.Product.DataAccess Company.Product.Domain Company.Product.Presentation

The GenericRepository is placed inside Company.Product.DataAccess.Repositories and mappers inside Company.Product.Domain.Mappers

最满意答案

查看构造函数列表,看起来事物成对出现:

productRepository / productMapper personRepository / personMapper orderRepository / orderMapper

这似乎表明MainViewModel组成了与产品,人员和订单相关的东西。

您可以改为对其进行建模,以便它构成其他三个View模型吗? 假设的ProductViewModel , PersonViewModel , OrderViewModel类..?

它甚至必须是这三个类吗? MainViewModel组成任意数量的其他View模型吗?

这会将构造函数减少到这样的:

public MainViewModel( IReadOnlyCollection<IViewModel> viewModels, IViewModelLoader viewModelLoader, IEventAggregator eventAggregator)

这似乎更合理。

这实际上是Composite模式的一种实现,它通常很适合UI建模 - 它通常被称为复合UI


让我想知道的另一件事是IVewModelLoader作用是什么?

Looking at the constructor list, it looks like things come in pairs:

productRepository/productMapper personRepository/personMapper orderRepository/orderMapper

This seems to indicate that MainViewModel composes something related to products, persons, and orders.

Could you instead model it so that it instead composes three other View Models? Hypothetical ProductViewModel, PersonViewModel, OrderViewModel classes..?

Does it even have to be exactly these three classes? Could MainViewModel instead compose any number of other View Models?

That would reduce the constructor to something like this:

public MainViewModel( IReadOnlyCollection<IViewModel> viewModels, IViewModelLoader viewModelLoader, IEventAggregator eventAggregator)

which seems much more reasonable.

This would essentially be an implementation of the Composite pattern, which is often a natural fit for UI modelling - where it's often called Composite UIs.


Another thing that makes me wonder is what that IVewModelLoader does?

更多推荐

本文发布于:2023-07-23 01:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1225564.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   函数   如何在   constructor   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!