简体   繁体   English

将MVC应用于领域驱动的设计

[英]Applying mvc to domain-driven design

From a practical point of view, how can you adapt the domain model to the MVC pattern? 从实际的角度来看,如何使域模型适应MVC模式? 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). MVC是一种设计模式,用于分离存储数据(模型),呈现数据的各种视图(视图)以及与该数据进行交互(控制器)的关注点。 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. 这些视图通常但不一定用于GUI。

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. 因此,尽管您确实可以使用域模型中的对象来实现MVC,但是您并不会真正使彼此“适应”。 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. 例如,如果您为BankAccount实体建模并为其编写了相应的类,则可以将其用作MVC三元组中的模型。 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. MVC有多种形式,根据您选择的内容,最终可能会修改域模型。 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. 也许这就是您使一个“适应”另一个的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM