贫血的数据模型,DAO的,...权威的参考? [英] anemic data model, dao's, ... authoritative reference?

查看:153
本文介绍了贫血的数据模型,DAO的,...权威的参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然是实验程序员和建筑师,同样的老问题基本回来反复地。我有我自己关于它的宗教,但我需要一些权威来源。

Although an experimented programmer and architect, the same old basic problem comes back recurrently. I have my own religion about it, but I need some authoritative source.

是贫血的数据模型((三)Martin Fowler的?)本来不好?如果一个蛋糕能烤自己?如果发票知道如何(以及何时应该允许),以行添加到自身,还是应该另一层做到这一点? rabbit.addToHole(孔)或hole.addRabbit(兔)?它已被证明是一个ADM更容易出错,或者更容易维护,或什么?

Are anemic data models ( (c) Martin Fowler?) inherently bad? Should a cake be able to bake itself? Should an invoice know how (and when it should allow) to add lines to itself, or should another layer do that? rabbit.addToHole(hole) or hole.addRabbit(rabbit)? Has it been proved that an ADM is more bug-prone, or easier to maintain, or anything?

您可以找到很多在网络上索赔,但我真的想一些权威报价,引用或事实,如果可能的话,双方。

You can find a lot of claims on the web, but I'd really want some authoritative quotes, references or facts, if possible from both sides.

推荐答案

请参阅此 StackOverflow的答案获得启示。

这是我的意见:

贫血领域模型是坏的,只有在充分OOP的条款。它被认为是不好的设计,主要是因为你不能创建UML类和关系,与里面嵌入的行为。例如,具有丰富的域模型(RDM)您的发票类:

Anemic domain model is bad, only in terms of full oop. It is considered as bad design, mainly because you cannot create UML classes and relations with embedded behavior inside it. For example, in your Invoice class with Rich Domain Model (RDM):


  • 类名称订购

  • 实施:ICommittable,IDraftable,...

  • 属性:没有,用户ID,总金额,...

  • 行为:提交(),SaveDraft(),...

类是自我证明,自我解释什么可以做,什么不能。

The class is self-documented and self explaining about what it can do and what can't.

如果是贫血的域模型,它不具有的行为,我们需要搜索的类负责提交并保存草稿。而且,由于UML类图仅显示每个类之间的关系(一对多/多对多/汇总/复合材料),与服务类的关系不能被记录,并Martin Fowler的有他点右键。

If it is anemic domain model, it does not has the behavior, and we need to search which class is responsible for Committing and Saving Draft. And since the UML class diagram only shows the relation between each classes (one to many / many to many / aggregate / composite), the relation with service class cannot be documented, and Martin Fowler has his point right.

在一般情况下,更行为您在服务发现,越
  可能你要抢劫自己的域的好处
  模型。如果你所有的逻辑是在服务业,你抢了自己瞎了。

In general, the more behavior you find in the services, the more likely you are to be robbing yourself of the benefits of a domain model. If all your logic is in services, you've robbed yourself blind.

这是由拉尔斯Mathiassen 基于类图UML在OOAD的书。我不知道是否有更新的类图UML可以重新present服务类。

This is based on class diagram UML in OOAD book by Lars Mathiassen. I don't know if newer class diagram UML can represent service class.

在ADM公司在继承的观点和compisition,RDM(富域模型)违反SRP。这可能是事实,但你可以参考这篇<一个href=\"http://stackoverflow.com/questions/1399027/single-responsibility-principle-vs-anemic-domain-model-anti-pattern\">question讨论。

In ADM's point of view and compisition over inheritance, RDM (rich domain model) violates SRP. It may be true, but you can refer to this question for discussion.

不久,在视ADM的角度来看,SRP等于一类做一件事,只有一件事。 的任何改变进级有且只有一个理由。

Shortly, in ADM's point of view, SRP equals one class doing one thing and one thing only. Any change into the class has one and only one reason.

在RDM的角度来看,SRP等于有关并仅在接口本身所有的责任。只要操作涉及其他类,那么需要投入其他界面的操作。实施本身可以变化,这样,如果一个类可以实现2个或更多的接口。它只是说,如果接口的操作需要改变,这是只为一个原因

In RDM's point of view, SRP equals all responsibility related to and only to the interface itself. As soon as the operation involve other class, then the operation need to be put into other interface. The implementation itself may vary, as such if a class can implement 2 or more interfaces. It is simply said as if an operation in interface need to be changed, it is for and only for one reason.

霉素是非常容易使用静态方法被滥用 - 服务类。它可以与RDM做太多,但它需要另一个抽象层,不值得。静态方法通常是设计不良的标志,它减少了可测试性和可能引入竞争条件,以及隐藏的依赖。

ADM is very easy to be abused with static methods - service class. It can be done with RDM too, but it need another layer of abstraction and not worth it. Static methods are usually a sign of bad design, it reduced testability and may introduce race conditions, as well as hiding the dependency.

ADM可以有很多肮脏的黑客,因为操作不是由对象定义的约束(嘿,我可以创建另一个类的!)。在恶劣的设计师手中,这可以成为灾难性的。在RDM这是很难,请阅读信息的下一个点。

ADM can has many dirty hacks because the operations are not being constrained by the object definition (hey, I can create another class for this!). In hand of bad designer, this can become catastrophic. In RDM it is harder, please read next point for information.

通常RDM的实现不能重复使用和嘲笑。在TDD方式,减少了可测试性(请纠正我,如果有RDM可以嘲笑和重复使用)。想象一下,这个继承树的情况:

Usually RDM's implementation cannot be reused and mocked. In TDD manner, it reduced testability (please correct me if there is RDM which can be mocked and reused). Imagine a situation with this inheritance tree:

    A
   / \
  B   C

如果用C语言实现,B就不需要逻辑,它无法做到的。用组合物在继承,这可以实现。在RDM,可以用这样的设计来完成:

If B need logic implemented in C, it cannot be done. Using composition over inheritance, it can be achieved. In RDM, it can be done with a design like this:

    A
    |
    D
   / \
  B   C

在其中引入更多的继承。然而,为了实现年初利落的设计,你需要知道系统的第一手资料流动。这就是说,RDM需要您知道系统的行为做任何设计之前,或者你不会知道任何命名ISubmitable,IUpdateable,ICrushable,IRenderable,ISoluble等,适合你的系统的接口。

In which introduce more inheritance. However, in order to achieve neat design early, you will need to know the system flow firsthand. That said, RDM require you to know the system's behavior before doing any design, or you won't know any of the interfaces named ISubmitable, IUpdateable, ICrushable, IRenderable, ISoluble, etc, suitable for your system.

这就是我对这种神圣的战争的看法。既有优点和缺点。我通常去ADM,因为它似乎是更大的灵活性,甚至有更少的可靠性。无论ADM或RDM,如果你设计你的系统坏了,维修是很难的。任何类型的电锯将仅由熟练的木匠普照时举行。

That's all my opinion about this kind of holy war. Both has pros and cons. I usually go for ADM because it seems like higher flexibility even has less reliability. Regardless of ADM or RDM, if you design your system bad, the maintenance is hard. Any type of chainsaw will only shines when held by skillful carpenter.

这篇关于贫血的数据模型,DAO的,...权威的参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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