什么是域驱动设计(DDD)? [英] What is Domain Driven Design (DDD)?

查看:135
本文介绍了什么是域驱动设计(DDD)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文章中经常看到使用DDD(域驱动设计)的情况-我已经阅读了有关DDD的Wikipedia条目,但仍无法弄清DDD是什么,以及如何在创建网站时实现它?

I keep seeing DDD (Domain Driven Design) being used a lot in articles - I have read the Wikipedia entry about DDD but still can't figure out what it actually is and how I would go about implementing it in creating my sites?

推荐答案

首先,如果您不知道自己是否需要它,则有可能不需要它。如果您不认识DDD解决的问题,那么也许您就没有这些问题。甚至DDD倡导者也会经常指出DDD仅适用于大型(> 6个月)项目。

Firstly, if you don't know that you need it then it's possible that you don't need it. If you don't recognize the problems that DDD solves then maybe you don't have those problems. Even DDD advocates will frequently point out that DDD is only intended for large (>6 month) projects.

假设您现在仍在阅读,我的看法是DDD是这样的:

Assuming that you're still reading at this point, my take on DDD is this:

DDD旨在使您的软件成为真实系统或过程的模型。使用DDD时,您应与 域名专家 紧密合作谁可以解释实际系统的工作原理。例如,如果您正在开发一个处理赛马投注的系统,则您的域名专家可能是经验丰富的庄家。

DDD is about trying to make your software a model of a real-world system or process. In using DDD, you are meant to work closely with a domain expert who can explain how the real-world system works. For example, if you're developing a system that handles the placing of bets on horse races, your domain expert might be an experienced bookmaker.

您自己和域名专家之间,您将构建普遍存在的语言(UL),这基本上是系统的概念描述。这个想法是,您应该能够以域专家可以读取并验证系统正确性的方式记录系统的性能。在我们的投注示例中,无处不在的语言将包括单词 race, bet, odds等的定义。

Between yourself and the domain expert, you build a ubiquitous language (UL), which is basically a conceptual description of the system. The idea is that you should be able to write down what the system does in a way that the domain expert can read it and verify that it is correct. In our betting example, the ubiquitous language would include the definition of words such as 'race', 'bet', 'odds' and so on.

UL将成为您面向对象设计的基础。 DDD为您的对象如何交互提供了一些明确的指导,并帮助您将对象分为以下类别:

The concepts described by the UL will form the basis of your object-oriented design. DDD provides some clear guidance on how your objects should interact, and helps you divide your objects into the following categories:


  • 值对象,它们表示可能包含子部分的值(例如,日期可能具有日,月和年)

  • 实体,它们是具有 identity 的对象。例如,每个客户对象都有其自己的身份,因此我们知道两个具有相同名称的客户不是同一位客户

  • 聚合根是拥有其他对象的对象。这是一个复杂的概念,其依据是有些对象除非拥有所有者,否则它们是没有意义的。例如,没有订单就没有订单行对象是没有意义的,因此我们说订单是集合根,订单行对象只能通过Order对象中的方法进行操作

  • Value objects, which represent a value that might have sub-parts (for example, a date may have a day, month and year)
  • Entities, which are objects with identity. For example, each Customer object has its own identity, so we know that two customers with the same name are not the same customer
  • Aggregate roots are objects that own other objects. This is a complex concept and works on the basis that there are some objects that don't make sense unless they have an owner. For example, an 'Order Line' object doesn't make sense without an 'Order' to belong to, so we say that the Order is the aggregate root, and Order Line objects can only be manipulated via methods in the Order object

DDD还建议以下几种模式:

DDD also recommends several patterns:


  • < href = http://martinfowler.com/eaaCatalog/repository.html rel = noreferrer>存储库,这是一种持久性模式(通常将数据保存到数据库或从数据库加载数据) li>
  • 工厂,一种用于创建对象的模式

  • 服务,一种创建对象的模式,该对象可以操作您的主域对象而又不属于域本身

  • Repository, a pattern for persistence (saving and loading your data, typically to/from a database)
  • Factory, a pattern for object creation
  • Service, a pattern for creating objects that manipulate your main domain objects without being a part of the domain themselves

现在,在这一点上,我不得不说,如果您以前从未听说过这些事情,那么您不应该尝试在有期限的任何项目上使用DDD。在尝试DDD之前,您应该熟悉设计模式企业设计模式。了解这些使DDD更加容易掌握。而且,如上所述,我们提供了免费DDD介绍。 InfoQ(您也可以在其中找到有关DD​​D的讨论)。

Now, at this point I have to say that if you haven't heard of any of these things before, you shouldn't be trying to use DDD on any project that you have a deadline for. Before attempting DDD, you should be familiar with design patterns and enterprise design patterns. Knowing these makes DDD a lot easier to grasp. And, as mentioned above, there is a free introduction to DDD available from InfoQ (where you can also find talks about DDD).

这篇关于什么是域驱动设计(DDD)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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