Brief introduction to domain-driven design

33
Brief Introduction to Domain Driven Design(DDD) Yongqiang

Transcript of Brief introduction to domain-driven design

Page 1: Brief introduction to domain-driven design

Brief Introduction to Domain Driven

Design(DDD)Yongqiang

Page 2: Brief introduction to domain-driven design

Outline

What’s Domain? What’s the heart of a software? What’s DDD? Crunching Knowledge Ubiquitous Language Basic Elements in DDD Summary References

Page 3: Brief introduction to domain-driven design

What’s Domain?

Domain在 DDD中所指就是企业软件所涉及的业务领域,其中包含大量业务知识。

Page 4: Brief introduction to domain-driven design

What’s the heart of a software?

导演 ->软件用户剪辑师 ->软件工程师

剪辑师为什么没有解决导演的问题?

Page 5: Brief introduction to domain-driven design

The heart of software is its ability to solve domain-related problems for its users! (软件的核心价值是为客户解决业务问题)。

如果想做出色的业务软件,前提就是对用户所处的领域有充分的,全面的,深刻的理解!

Page 6: Brief introduction to domain-driven design

What’s DDD?

领域驱动开发 Eric Evans 创始人目标:让软件成为领域的映射。

软件需要具现领域里重要的核心概念和元素,并精确实现它们之间的关系。

软件需要对领域进行建模,得到一个关于领域的模型。

中心内容:如何将业务领域概念映射到软件工件中

Page 7: Brief introduction to domain-driven design

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

Page 8: Brief introduction to domain-driven design

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.

Page 9: Brief introduction to domain-driven design

So...

Modeling out loud! One Team, One Language!

Page 10: Brief introduction to domain-driven design

Basic Elements in DDD

Page 11: Brief introduction to domain-driven design
Page 12: Brief introduction to domain-driven design
Page 13: Brief introduction to domain-driven design

Layered Architecture

Page 14: Brief introduction to domain-driven design
Page 15: Brief introduction to domain-driven design

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

Page 16: Brief introduction to domain-driven design

Attributes VS. Identity

class Person CD

Person

- BirthDate- BirthPlace- Id- Name- Parents

Page 17: Brief introduction to domain-driven design

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

Page 18: Brief introduction to domain-driven design

帮助简化设计没有标识符,值对象可以被轻易地创建或者丢弃。

极力推荐值对象是不变的。

Page 19: Brief introduction to domain-driven design

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.

Page 20: Brief introduction to domain-driven design

Partition Services into Layers Application Domain Infrastructure

Page 21: Brief introduction to domain-driven design
Page 22: Brief introduction to domain-driven design
Page 23: Brief introduction to domain-driven design

Life Cycle of Domain Object

Aggregates 聚合 用于打破对象的网状关系 , 定义所有权和边界

Factories 工厂 用于领域对象生命周期的开始部分 , 创建或者重组复杂的对象或 Aggregates.

Repositories 资源库 用于领域对象生命周期的中后部 ,对存储 ,检索查询等功能进行了封装 .

Page 24: Brief introduction to domain-driven design

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.

Page 25: Brief introduction to domain-driven design
Page 26: Brief introduction to domain-driven design

Invariants, which are consistency rules that must be maintained whenever data changes.

Page 27: Brief introduction to domain-driven design

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.

Page 28: Brief introduction to domain-driven design

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.

Page 29: Brief introduction to domain-driven design
Page 30: Brief introduction to domain-driven design

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.

Page 31: Brief introduction to domain-driven design

By specific parameter

By criteria

Page 32: Brief introduction to domain-driven design

Summary

DDD目标是建立最适合的模型映射领域知识,以模型做为团队交流和软件开发的基础。

DDD对企业应用软件中的基础元素做了合理的抽象。

DDD可以和 TDD,敏捷开发方法联合使用。

Page 33: Brief introduction to domain-driven design

References

Eric Evans 《领域驱动设计—软件核心复杂性应对之道》

InfoQ 《领域驱动设计精简版》