问题描述
我正在决定是否应该在贫血域模型上使用Rich Domain模型,并寻找两者的良好示例.
I am deciding if I should use a Rich Domain Model over an Anemic Domain Model, and looking for good examples of the two.
我一直在使用Anemic域模型构建Web应用程序,并由 Service->存储库-> Storage 层系统支持,并使用 FluentValidation 进行BL验证,并将我所有的BL放入Service层.
I have been building web applications using an Anemic Domain Model, backed by a Service --> Repository --> Storage layer system, using FluentValidation for BL validation, and putting all of my BL in the Service layer.
我已经读过Eric Evan的DDD书,他(以及Fowler等人)似乎认为Anemic Domain Models是一种反模式.
I have read Eric Evan's DDD book, and he (along with Fowler and others) seems to think Anemic Domain Models are an anti-pattern.
所以我只是真的想对这个问题有一些了解.
So I was just really wanting to get some insight into this problem.
此外,我真的在寻找Rich Domain模型的一些好(基本)示例,以及它提供的Anemic Domain Model带来的好处.
Also, I am really looking for some good (basic) examples of a Rich Domain Model, and the benefits over the Anemic Domain Model it provides.
推荐答案
区别在于,贫血模型将逻辑与数据分离.逻辑通常放在名为**Service
,**Util
,**Manager
,**Helper
等的类中.这些类实现了数据解释逻辑,因此将数据模型作为参数.例如
The difference is that an anemic model separates logic from data. The logic is often placed in classes named **Service
, **Util
, **Manager
, **Helper
and so on. These classes implement the data interpretation logic and therefore take the data model as an argument. E.g.
public BigDecimal calculateTotal(Order order){
...
}
而富域方法则通过将数据解释逻辑放入富域模型来逆转这种情况.因此,它将逻辑和数据放在一起,一个丰富的域模型将如下所示:
while the rich domain approach inverses this by placing the data interpretation logic into the rich domain model. Thus it puts logic and data together and a rich domain model would look like this:
order.getTotal();
这对对象一致性有很大的影响.由于数据解释逻辑会包装数据(只能通过对象方法访问数据),因此这些方法可以对其他数据的状态变化做出反应->这就是我们所说的行为.
This has a big impact on object consistency. Since the data interpretation logic wraps the data (data can only be accessed through object methods) the methods can react to state changes of other data -> This is what we call behavior.
在贫血模型中,数据模型不能保证它们处于合法状态,而在富域模型中则可以保证.富域模型应用了OO原理,例如封装,信息隐藏以及将数据和逻辑结合在一起,因此从OO角度来看,贫血模型是一种反模式.
In an anemic model the data models can not guarantee that they are in a legal state while in a rich domain model they can. A rich domain model applies OO principles like encapsulation, information hiding and bringing data and logic together and therefore a anemic model is an anti pattern from an OO perspective.
要获得更深入的了解,请查看我的博客 https://www.link-intersystems.com/blog/2011/10/01/anemic-vs-rich-domain-models/
For a deeper insight take a look at my blog https://www.link-intersystems.com/blog/2011/10/01/anemic-vs-rich-domain-models/
这篇关于富vs贫血域模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!