AnemicDomainModel - 需要更简单的解释
我今天阅读了这篇文章,并试图澄清一些事情。本文是否意味着模型对象应该包含业务逻辑? 例如,假设有一个 Student 对象,我们通过 Hibernate 从数据库中检索该对象。本文是否说 Student 对象还应该包含业务逻辑,而不是只有 getter 和 setter?
I read this article today and am trying to clarify some things. Does this article mean that model objects should contain business logic?
For example let us say that there is a Student object that we retrieve form the database via Hibernate. Does this article say that the Student object should contain business logic as well rather than having only getters and setters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不管日期如何,马丁·福勒所说的内容在今天和八年前一样重要。 Fowler 并没有声明您应该将持久性混合到域对象中,恰恰相反:
“还值得强调的是,将行为放入域对象中不应与使用分层将域逻辑与持久性和表示等事物分开的可靠方法相矛盾的责任。”
您应该再次阅读这篇文章,因为该文章很好地描述了这种反模式,但我将尝试根据您所要求的内容为您总结它:
如果您要创建域模型,是的,您的域对象应该包含业务逻辑和状态,并且应该通过传达业务含义的方法来更改域实体的状态。贫乏领域模型是一种反模式,因为您会付出额外一层类的成本,但并没有获得好处。当域层传达的意图与使用活动记录样式方法(数据集等)完全相同的意图时,为什么还要费心去映射数据库的域层呢?因此,本文并没有说您应该有一个“学生对象”,但它指出,如果您这样做,您应该明确地向该类添加状态。
由于当今可用的技术,文章中关于如果您不对域进行建模,则没有一组对象来表示您的模型的观点可能会有点令人困惑。有一些很棒的工具可以毫不费力地在一组 POCO 和数据库之间移动数据(Nhibernate、EF、Simple Data、Massive、Dapper 等),所以在回顾中我会说你最终可能会得到一组当今大多数解决方案中的“实体”,真正的区别在于这只是数据库模型还是真正的领域模型。
最后,我将向您展示域入口点(命令处理程序)和域模型之间交互的示例。下面显示的方法位于命令处理程序中,该处理程序使用请求来更改域中的某些内容。请注意,域代码之上的层只是获取域实体并调用域上的一个方法?这是很重要的一点,因为我们正在建模的工作流程完全封装在域中,而不是域代码之上的层或其他任何地方:
Disregard the date, what Martin Fowler states is as relevant today as it was eight years ago. Fowler does not state that you should mix persistence into the domain objects, quite the contrary:
"It's also worth emphasizing that putting behavior into the domain objects should not contradict the solid approach of using layering to separate domain logic from such things as persistence and presentation responsibilities."
You should read the article again, because the article describes this anti-pattern extermely well, but I shall try to summarize it for you in the context of what you are asking:
If you are to create a domain model, yes your domain objects should contain business logic as well as state, and changes to the state of your domain entities should be done through methods which convey business meaning. The anemic domain model is an anti-pattern because you incur the cost of an extra layer of classes but you are not reaping the benefits. Why bother with a domain layer which you have to map against the database when it convey exactly the same intent as you get from using an active record style approach (dataset, etc)? So the article does not say that you should have a "student-object", but it states that if you do, you should definitively add state to that class.
The point in the article about not having a set of objects to represent your model if you don't also model your domain can be a bit confusing due to the technologies available today. There are great tools out there which can effortlessly move data between a set of POCOs and the database (Nhibernate, EF, Simple Data, Massive, Dapper, etc) so in that retrospectiv I would say that you would probably end up with a set of "entities" in most solutions today, the real difference being whether this is just a database model or a real domain model.
I'll close up by showing you an example of the interaction between a domain entry point (command handler) and a domain model. The method shown below lives in a command handler which consumes a request to change something in the domain. Notice that the layer-ontop-of-your-domain-code simply gets the domain entity and calls one method on the domain? Thats an important point because the workflow we are modelling is fully encapsulated in the domain, not in the layer-ontop-of-your-domain-code or anywhere else:
注意日期——该引文已经有八年多了。
马丁·福勒显然是一个非常聪明的人,我喜欢这篇文章的观点,但对此持保留态度。一般来说,将状态和行为封装在一起是一件好事,但它应该与分层考虑因素相平衡。持久性与业务逻辑不同。我仍然有一个单独的持久层;我不会将持久性放入模型对象中。
教条应该受到各种形式的挑战。了解别人的想法,但要独立思考。
Notice the date - the citation is over eight years old.
Martin Fowler is obviously a very smart guy, and I like the article's point, but take it with a grain of salt. Encapsulating state and behavior together is a good thing in general, but it should be balanced against layering considerations. Persistence isn't the same thing as business logic. I'd still have a separate persistence tier; I wouldn't put persistence in a model object.
Dogma should be challenged in all its forms. Be aware of other people's ideas, but think for yourself.