将MVC应用于领域驱动的设计 [英] Applying mvc to domain-driven design

查看:145
本文介绍了将MVC应用于领域驱动的设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从实用的角度来看,如何使域模型适应MVC模式?例如,我可以使用一些包装器类吗?

解决方案

它们并没有真正的联系.

MVC是一种设计模式,用于分离存储数据(模型),呈现数据的各种视图(视图)以及与该数据进行交互(控制器)的关注点.尽管这可能是设计"模式,但实际上是关于代码的设计.这些视图通常但不一定用于GUI.

域驱动设计是一种设计软件,其中您专注于对域建模,以创建问题域的一个易于理解的共享模型-域模型".域驱动的设计不仅是设计",还体现在代码,需求,各种利益相关者之间的对话等中.

因此,尽管您确实可以使用域模型中的对象来实现MVC,但您并不会真正使彼此适应".例如,如果您为BankAccount实体建模并为其编写了相应的类,则可以将其用作MVC三元组中的模型.也许控制器处理现金的存入和提取,并且在更改模型时会更新几个视图(例如,借方,贷方和汇总视图). MVC有多种形式,根据您选择的内容,最终可能会修改域模型.例如,您可以使用观察者模式,只要模型实体发生更改,便会在其中通知您的视图.这确实意味着您将把非域概念(观察者注册,通知等)混入域对象中.如果这对您很重要,那么在这种情况下,最好包装域对象,以使域模型和表示层之间保持清晰的分隔.也许这就是您将一个适应"到另一个的意思.

From a practical point of view, how can you adapt the domain model to the MVC pattern? For example, could I use some wrapper classes?

解决方案

They aren't really related.

MVC is a design pattern for separating the concerns of storing data (model), presenting various views of the data (view), and interacting with that data (controller). While it may be a "design" pattern, it is really about the design of code. The views are usually, but not necessarily used for GUIs.

Domain-Driven Design is a style of designing software where you focus on modeling the domain to create a shared well-understood model of the problem domain - a "domain model". Domain-Driven design is not just "design", but also represented in the code, requirements, conversations amongst various stakeholders, etc.

So, you wouldn't really "adapt" one to the other, though you certainly can implement MVC using objects from your domain model. For example, if you modeled a BankAccount entity and wrote a corresponding class for it, you could use that as the model in an MVC triad. Perhaps the controller handles depositing and withdrawing cash, and several views (for example a debit, credit, and summary view) are updated upon model change. There are multiple flavors of MVC, and depending on what you pick you may end up modifying your domain model. For example, you could use the observer pattern where your views are notified whenever a model entity changes. This does mean you would be mixing non-domain concepts (observer registration, notification, etc.) into your domain object. It may be better to wrap the domain object in this case to keep a clean separation between the domain model and presentation layer, if that's important to you. Perhaps that is what you mean by "adapting" one to the other.

这篇关于将MVC应用于领域驱动的设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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