Download - Brief introduction to domain-driven design
Brief Introduction to Domain Driven
Design(DDD)Yongqiang
Outline
What’s Domain? What’s the heart of a software? What’s DDD? Crunching Knowledge Ubiquitous Language Basic Elements in DDD Summary References
What’s Domain?
Domain在 DDD中所指就是企业软件所涉及的业务领域,其中包含大量业务知识。
What’s the heart of a software?
导演 ->软件用户剪辑师 ->软件工程师
剪辑师为什么没有解决导演的问题?
The heart of software is its ability to solve domain-related problems for its users! (软件的核心价值是为客户解决业务问题)。
如果想做出色的业务软件,前提就是对用户所处的领域有充分的,全面的,深刻的理解!
What’s DDD?
领域驱动开发 Eric Evans 创始人目标:让软件成为领域的映射。
软件需要具现领域里重要的核心概念和元素,并精确实现它们之间的关系。
软件需要对领域进行建模,得到一个关于领域的模型。
中心内容:如何将业务领域概念映射到软件工件中
Crunching Knowledge(知识汲取 )
Talk with domain expert Lead by developers Domain model is focused on the most
valuable part Developer needs feedback We never know enough We need knowledge-rich design Binding model and implementation
Ubiquitous Language(通用语言 ) For each term, we need
a very precisely and unambiguously defined meaning
spanning from discussions with the domain expert to the code level
assures that everybody in the team shares the same vision about the domain and the software.
So...
Modeling out loud! One Team, One Language!
Basic Elements in DDD
Layered Architecture
Entity
An object define primarily by its identity is called an Entity.
Most basic responsibility of Entities is to establish continuity so that behavior can be clear and predictable.
class Person CD
BankAccount
- AccountId- Deposit- UserName
Attributes VS. Identity
class Person CD
Person
- BirthDate- BirthPlace- Id- Name- Parents
Value Object(值对象 )
An object that represents a descriptive aspect of the domain with no conceptual identity is called a VALUE OBJECT.
VALUE OBJECTS are often passed as parameters in messages between objects.
class Person CD
Point
- X: int- Y: int
class Person CD
Address
- City- Street
帮助简化设计没有标识符,值对象可以被轻易地创建或者丢弃。
极力推荐值对象是不变的。
Service(服务 )
Some of these are intrinsically activities or actions, not things.
A service tends to be named for an activity, rather that an entity-a verb rather that a noun.
A service does not do much on its own; it would ask other objects to do most of work.
Partition Services into Layers Application Domain Infrastructure
Life Cycle of Domain Object
Aggregates 聚合 用于打破对象的网状关系 , 定义所有权和边界
Factories 工厂 用于领域对象生命周期的开始部分 , 创建或者重组复杂的对象或 Aggregates.
Repositories 资源库 用于领域对象生命周期的中后部 ,对存储 ,检索查询等功能进行了封装 .
Aggregate(聚合 )
An aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes. Each aggregate has a root of a boundary.
The root is the only member of the Aggregate that outside objects are allowed to hold references.
Invariants, which are consistency rules that must be maintained whenever data changes.
Factory(工厂 )
Creation of an object can be a major operation in itself, but complex assembly operations do not fit the responsibility of the created objects. Combining such responsibilities can produce ungainly designs that are hard to understand.
A program element whose responsibility is the creation of other objects is called a FACTORY.
Two basic requirements for any good Factory are Each creation method is atomic and enforces all
invariants of the created object or AGGREGATE. The FACTORY should be abstracted to the type
desired, rather than the concrete class(es) created.
Repositories(资源库 )
Global Access Aggregate roots which are not convenient to
reach by traversal. Value Objects with complex internal structure. enumerated values.
Two ways to query a repository Hard-coded query with specific parameter. Specification-based query.
By specific parameter
By criteria
Summary
DDD目标是建立最适合的模型映射领域知识,以模型做为团队交流和软件开发的基础。
DDD对企业应用软件中的基础元素做了合理的抽象。
DDD可以和 TDD,敏捷开发方法联合使用。
References
Eric Evans 《领域驱动设计—软件核心复杂性应对之道》
InfoQ 《领域驱动设计精简版》