干净的领域驱动设计(DDD)是乌托邦吗?

发布于 2024-11-14 16:42:20 字数 864 浏览 7 评论 0原文

我想我读了所有关于 DDD 以及如何在 C# 中应用它的书(3-5 本书,不记得是几年前的事了)。

但在过去的两年里,我在所有项目中都使用了贫血领域模型,并且效果很好。实际上我从未尝试过实现纯域模型,而且我从未见过使用干净域模型方法的代码。

以书中关于 DDD 的代码为例,代码不遵循 DDD 风格。以 Tim MacCarthy 书中关于 DDD http://dddpds.codeplex.com/ 的代码为例,请查看他的域对象,它们没有任何行为,这只是 DTO,他集成到域对象中的唯一东西就是验证,如果我在 DDD 中没记错的话,你的域不应该处于无效状态,所以你应该防止设置无效值并抛出异常而不是允许设置它们并在某个时刻进行验证。

与其他书籍和示例代码的情况相同,它们都非常简单,仅显示一个具有属性的类,您最多可以找到一组用于插入\删除设置父属性的子对象的方法。例如,我几乎从未见过使用域事件或将服务注入域对象的代码。

2003 年,Martin Fowler 写了一篇文章,告诉我们贫血域模型是一件坏事http://www. martinfowler.com/bliki/AnemicDomainModel.html 8 年过去了,我认为 Anemic 模型现在相当流行(它演变成分层架构)。并在 3 层 RIA 开发中大量使用。

所以我的主要问题是干净的 DDD 是一个乌托邦吗?它死了吗?因为过去 3 年我看到的所有代码都是分层架构(在我看来,它本质上是贫乏的领域模型)。我非常希望看到一些干净的 DDD 代码(如果存在)。

I think I read all the book's on DDD and how to apply it in C# (3-5 books, don't remember it was few years ago).

But during last 2 years I was using anemic domain model in all projects and it worked well. Actually I have never tried to implement pure domain model, moreover I have never seen a piece of code using clean domain model approach.

Take for example code from the book's about DDD doesn't follow DDD style. Take for example code code from Tim MacCarthy book about DDD http://dddpds.codeplex.com/, look at his domain objects, they are clean from any behavior's it's just DTO's the only thing he integrated into domain object is validation, if I remember correctly in DDD your domain should not be in invalid state, so you should prevent setting invalid values and throw exception's instead of allowing to set them and do validation at some point.

Same situation with other books and sample codes, they all are very simple and shows just a class with properties, maximum what you can find is a set of methods for inserting\removing child objects which set the parent property. For example I almost neven seen code using domain events, or injecting services into domain objects.

In 2003 Martin Fowler wrote an article telling us that Anemic Domain Model is a bad thing http://www.martinfowler.com/bliki/AnemicDomainModel.html 8 years passed and I think Anemic model is quite popular now (it evoluted to Layered Architecture). And is used alot in 3-Tier RIA development.

So my main question is clean DDD a utopia? Is it dead? Becouse all the code I see around for last 3 years is Layered Architecture (in my opinion it's essetialy anemic domain model). And I would very like to see some clean DDD code if it exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄与心 2024-11-21 16:42:20

你并不孤单。在大多数地方,真正的 DDD 将是一个乌托邦。根据我的经验,这主要有两个原因。

  1. 业务还不成熟,无法帮助开发者做领域建模。没有足够的时间、资源、精力来做这件事。
  2. 项目团队经验不是很丰富,特别是如果他们以前没有做过DDD。

DDD 也不适合许多 CRUDy 应用程序。还有 CQRS 等替代方法。
另请参阅以下博客链接,这些链接为贫乏域模型提供了案例。

http://codebetter.com/gregyoung/2009/ 07/15/the-anemic-domain-model-pattern/
http://lostechies.com/jimmybogard/2009/ 12/03/持久性模型和领域贫血/

You are not alone. In most places true DDD will be an utopia. In my experience it is due to two main reasons.

  1. The business is not mature to help developers to do the domain modelling. There is not enough time, resource, energy to do it.
  2. The project team is not very experienced specially if they haven't done DDD before.

DDD is also not suitable in many CRUDy apps. There is also alternate approaches like CQRS.
see also the following blog links that make a case for anaemic domain models.

http://codebetter.com/gregyoung/2009/07/15/the-anemic-domain-model-pattern/
http://lostechies.com/jimmybogard/2009/12/03/persistence-model-and-domain-anemia/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文