视图模型和依赖注入 [英] View Models and dependency injection

查看:184
本文介绍了视图模型和依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然工作有很多使用 PRISM 和<一HREF =HTTP://entlib.$c$cplex.com/相对=nofollow>企业库,我注意到一个循环模式,似乎讨厌。对于每一个域模型实体(如联系),我觉得我自己有一个视图模型将其包装(如ContactVM),那么我引入一个新的 ContactsVM (注意S ),其中后一类接受它是用来填充的的ObservableCollection℃的储存库接口; ContactVM&GT; 和每联系方式实体我从存储库中读取,我把它包在 ContactVM ,我还有需要我的ViewModel其他企业图书馆的服务通过构造函数传递的实体。

While working on a LOB desktop application with a lot of CRUD operations using PRISM and Enterprise Library, I have noticed a recurring pattern that seems annoying. For every domain model entity (eg. Contact) I find my self wrapping it with a view model (eg. ContactVM) then I introduce a new ContactsVM (notice the 's') where the latter class accepts a repository interface which is used to populate an ObservableCollection<ContactVM> and for every Contact entity that I read from the repository, I wrap it in a ContactVM which I pass the entity to via the constructor along with other enterprise library services needed by my ViewModel.

的问题是,我所有的视图模型的构造开始采取这种模式是这样的:

The problem is that all my view model constructors started taking this pattern like this:

视图模型(EntityToWrap E,DependencyFromEntLib,OtherDependencies ...)

现在这是一个问题,因为大多数工具和库需要一个默认参数的构造函数(如一些商业数据网格需要能够提供过滤支持),再加上你不能使用设计数据可视化的实体,因为他们需要参数构造函数太。最后的问题是:什么是正确的方式来建立视图模型,并应Entlib通过构造函数或通过的服务定位

Now that is a problem because most tools and libraries require a default parameterless constructor (eg. some commercial data grids need that to provide filtering support), plus you can't use design data to visualize entities because they need parameterless constructors too. and finally the question: What is the right way to build view models and should Entlib services be provided via constructors or via the ServiceLocator ?

推荐答案

下面是许多不同的方法来解决这个问题之一。

我preFER视图模型这是更为轻巧。我再加入一个类,其职责是从一个或多个源(如库)组成视图模型。这并不能消除级联依赖性的问题,但它释放您的视图模型的构造。

I prefer view models which are much more lightweight. I then add a class whose responsibility is to compose the view model from one or more sources (e.g. a repository). This doesn't eliminate the problem of cascading dependencies, but it does free up your view model constructors.

这也保持了逻辑的控制器,并允许视图模型可以重复使用(在适当的时候,当然)。

It also keeps logic out of the controllers, and allows view models to be reused (when appropriate, of course).

  • 轻型视图模型

  • Lightweight view models

轻型控制器知道如何找到一个作曲家是组装视图模型(你可以使用一个DI框架来建立其所有的依赖作曲家)。控制器可以发挥在安装过程中起次要作用,但它应保持简单

Lightweight controller knows how to locate a composer which assembles the view model (you could use a DI framework to setup the composer with all of its dependencies). The controller may play a minor role in the setup process, but it should be kept simple.

控制器知道如何视图模型应该进行组装,并分享与作曲家类。例如,该操作可能会请求一个摘要视图仍然可以利用无子女居住在同一视图模型。

Controller knows how the view model should be assembled, and shares that with the composer class. For example, the action might request a summary view which can still leverage the same view model with no children populated.

作曲家汇集了必要的信息来完成视图模型。作曲者可以使用其他作曲家的收集,这就是它不直接负责的信息。再次,一个框架的DI这里可以使用使这些作曲家也给予他们需要的依赖关系。

Composer assembles the necessary information to complete the view model. Composer may use other composers to gather information for which it is not directly responsible. Again, a DI framework can be used here so that these composers are also given the dependencies that they need.

控制器呈现视图像往常一样与完成视图模型。

Controller renders the view as usual with the completed view model.

在我看来,这也提供了更好的抽象级别。仅仅因为一个视图模型通常看起来像一个特定的域模型,这并不意味着它会永远是这样。

In my opinion, this also provides a better level of abstraction. Just because a view model often looks like a particular domain model, that doesn't mean it will always be the case.

最终结果是:

  • 很多类(不利的一面,授予),但code最小重复(即干)

  • Lots of classes (a downside, granted), but minimal repetition of code (i.e. DRY)

瘦视图模型这是可测试(如果需要的话......他们可能包含任何测试)

Thin view models which are testable (if needed...they may contain nothing to test)

薄,可测试控制器。

它可以为不同的场景中重用,因为他们(presumably)知道如何装配视图模式(S)出于各种目的可测试作曲家的对象。

Testable composer objects which can be reused for different scenarios because they (presumably) know how to assemble view model(s) for various purposes.

灵活地混合和匹配视图模型,控制器和作曲家以支持不同的方案。

Flexibility to mix and match view models, controllers, and composers to support different scenarios.

这篇关于视图模型和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆