Applying mvc to domain-driven design

不打扰是莪最后的温柔 提交于 2019-12-13 01:39:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/2824495/applying-mvc-to-domain-driven-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!