DDD
DDD
#2 Cache Resource
#3 3 main Book
DDD Website:
#3 Other Books
#3 CQRS
CQRS, The example
#3 Video Course
#3 DSL
DSL in Action
Domain-Specific Languages
#3 Conf
slide-link
video-link
#3 Couser for comprehension course
link
#3 Other
valignatev/ddddynamic
#2 Learning Flow
en.wikipedia.org/wiki/Domain-driven_design
其运营的官⽹: domainlanguage.com
官⽹上有付费的⽜逼课程,准备购买
⼜看到另⼀篇⽂章: 今天我们还需要关注DDD吗?
了解到DDD是微服务,Eventstorm的⿐祖. 这⾥有⼏个重要实现与其对应的⼈:
(wiki还没看完)⾥⾯讲了很多其他的design idea.
已付费,开始学习domainlanguage.com - elearning
#4 Maps and Models
Assertions are a key aspect of the model: "direction between points are preserved in the projection"
Characteristics of models
Model Definitions
Usefulness of models
有些时候⼀个好好的锤⼦,被改成锤⼦,锯⼦两⽤的 .
#4 Aggregates
#4 Collaboration
Context: The setting in which a word or statement appears that determines its meaning
Bounded Context: The condition under which a particular model is defined and
applicable.
Map what it is .
不⽤做⼀个完美的Context,遇到什么问题,就建⽴什么context
#4 Context Mapping I
Upstream-Donwstream
Upstream的成功与否会影响Downstream
Donwstream的成功与否不会影戏那个Upstream
The upstream team may succeed independently of the fate of the downstream team.
Customer/Supplier Development
A downstream team can be helpless, at the mercy of upstream priorities. Meanwhile, the
upstream team may be inhibited, worried about breaking downstream systems. The
problems of the downstream team are not improved by cumbersome change request
procudures with complex approval processes. And the freewheeling development of the
upstream team will stop if the downstream team has veto power over changes.
最后应该这样:
需要时刻问⾃⼰ Which ubiquite languge should you use?
说⽩了就是,你在哪⼀个context⾥(或哪⼀层),你就应该⽤哪⼀个context的ubiquate
language
Generic subsomains
Supporting subsomains
Core domain
#3 Book - DDD
For most software projects, the primary focus should be on the domain and domain
logic.
Complex domain designs should be based on a model
Two practices are prerequisites for applying the approach in this book:
Development is iterative
Surviving Object-Oriented Projects
Extreme Programming Explained
Developers and domain experts have a close relationship
The XP process assumes that you can improve a design by refactoring, and that you will
do this often and rapidly.
但是开发者技术和理解的参差不⻬,导致之后的交流越来越困难,因为每次的refactoring会朝
着不同的⽅向发展,不⼀定是好事。所以DDD估计是来解决这种交流问题和保证refacotring的
⼀致性
Books Structure:
Maps are models, and every model represents some aspect of reality or an idea that is of
interest.
A model is an interpretation of reality that abstracts the aspects relevant to solving the
problem at hand and ignores extraneous detail.
1. The model and the heart of the design shape each other
2. The model is the backbone of a language used by all team members
3. The model is distilled knoledge
Knowledge Crunching
意思我们要像Domain experts⼀样,甚⾄和他们⼀起,不断去打磨⾃⼰的认知,然后使⽤通
⽤的想法和视⻆来进⾏开发
Continuous Learning
要学会如何跟Domain Exports交流,要持续互相学习
Knowledge-Rich Design
抽出适当的逻辑 (要考虑,其他⼈是否能看懂)
1. In order to bring the design to this stage, the programmers and everyone else involved
will have come to understand the nature of overbooking as a distinct and important
business rule, not just an obscure calculation.
2. Programmers can show business experts technical artifacts, even code, that should be
intelligible to domain experts (with guidance), thereby closing the feedback loop.
Deep Models
最初的domain设计很可能是错的或者过于局限,要与domain⼀起探讨更多以后,再进⾏更完
整的设计
Ubiquitous Language
所以使⽤通⽤语⾔是很重要的。
这⾥⾯有⼀段对话演⽰和⽐较,⾮常有意思。基本上就是要把那些关键的concept都抽象为
model放在图中,然后交流的时候持续使⽤他们交流。说⽩了就是要把开发和交流对接起来,
这个对接就是model的定义,model承载了两重作⽤。
The model is not the diagram. The diagram's purpose is to help communicate and
explain the model.
A documnet shouldn't try to do what the code already does well. Document Should Work
for a Living and Stay Current.
问题:这种⽂档神恶魔时候写,⻓什么样⼦,它的粒度是什么?
Explanatory Models
MODEL - DRIVEN DESIGN discards the dichotomy of analysis model and design to search
out a single model that serves both purposes. Setting aside purely technical issues, each
object in the design plays a conceptual role described in the model. This requires us to
be more demanding of the chosen model, since it must fulfill two quite different
objectives.
Development becomes an iterative process of refining the model, the design, and the
code as a single activity
这⾥⾯看到⼀个很好的案例。关于浏览器favariot的功能报错问题
The design style in this book largely follows the principle of “responsibility-driven
design,” put forward in Wirfs-Brock et al . 1990 and updated in Wirfs-Brock
2003. It also draws heavily (especially in Part III) on the ideas of “design by contract”
described in Meyer 1988. It is consistent with the general background of other widely
held best practices of object-oriented design, which are described in such books as
Larman 1998.
Layered Architecture
这个不适合DDD
Associations
不是很明⽩想说什么
关系的双向 不⼀定要在domain⾥出现,根据情况,可设置为单向的,还可以加限制条件
#3 Slide
slide
正常的MVC+services的样⼦:
通⽤语⾔:
可以先使⽤railroady 和 rails-erd来输出初始数据
Core domain
找出Core domain
如: Transportation
Supporting Domians
找出Supporting Domain
如:
Bounded Context
Draw out the dependencie
Upstream/Downstream
An Ideal Architecture
module RideSharring
class Trip < ActiveRecord::Base
belongs_to :vehicle
belongs_to :passenger
belongs_to :driver
end
end
module Ridesharing
class TripsController < ApplicationController
# ...
end
end
# config/routes.rb
resources :trips
class Invoice
belongs_to :trip
end
class PaymentConfirmation
belongs_to :trip, class_name: Ridesharing::Trip
belogns_to :passenger, class_name: Ridesharing::Passenger
belongs_to :credit_card
has_many :menu_items
belongs_to :coupon_code
has_one :email_job
# ad infinitum
end
Aggregate Root are top-level domain models that reveal an object graph of related
entities beneath them.
publicly via:
Your source domain can provide a service that returns the Aggregate Root as a facade
# OLD: payment_confirmation.passenger
# New: payment_confirmation.trip.passenger
video
这个⼈是dotenv的作者
把 UsersControler#forgot_passoword ⾥的代码refactor
到 Accounts::ForgotPassword.send_password_reset
Naming
不使⽤ OrderService
Events
不使⽤callback
使⽤ Wisper Publisher
Policy Objects
这个实际上就是使⽤以来倒转的⽅法,把这种Policy的内容抽象出去
#3 Bounded Context
A bounded context is not a Module. A bounded Context provides the logical frame inside
of which the model evolves. Modules are used to organize the elements of a model, so
Bounded Context encompasses the Module.
It is much simpler for the e-shop application to send Value Objects containing purchase
information to the warehouse using asynchronous messaging.
At the boundaries of the upstream context then, the domain objects are converted into
DTOs, which are ini turn serialized into JSON, XML, or some other serialization format.
At the downstream context, the process is repeated in the other direction: the JSON or
XML is deserialized into a DTO, which in turn is converted into a domain object:
In practive, the to-level DTOs that are serialized are typically Events DTOs, which in turn
contain child DTOs such as a DTO for Order, which in turn contains additional child DTOs
(e.g. a list of DTOs representing OrderLines.)
每个进⼊Boundaries的DTO都需要经过⼀层Validation验证
output也有⼀层验证
The job of the output gate is to ensure that private information doesn't leak out of the
bounded context, both to avoid accidental coupling between contexts, and for security
reasons.
不过与其说是验证,不如说是作为⼀个最后的输出层,也就是serialize层就够了
作者的意思应该是把validation与serialization分开
对内,或对外
把上⾯四种综合起来,放在⼀起看
Shipping and Order-taking use a jointly own the communications contract
Order-taking and Billing: Billing defined the contrast
顺着⽅向说:客⼾你是上帝,你说了算
Order-taking and Production CataLog: Product CataLog defined the contrast
顺着⽅向说:你给我好好遵守
Order-taking and Address Checking: we insert an ACL to deal with transalte. It helps us
avoid vendor lock-in, and lets us swap to a different service later.
这个的实现上需要再看看具体怎么实现的
有⼀点Funcitonal的思路在⾥⾯
对读写Database的功能,不要写在workflow中间,应该写在两头
To push any I/O to the edges of the onion - to only access a database at the start or end of
a workflow, but not inside the workflow
The View layer receives an object, often a Data Transfer Object(DTO) insteand of
instances of the Model layer, gathering all the needed information to be successfully
represented.
Customer/Supplier ⾥的的测试应该是要写在UpStream这⼀边,来确定⾃⼰是否满⾜
downstream的要求
Jointly develop automated acceptance tests that will validate the interface expected. Add
these tests to the upstream team’s test suite, to be run as part of its’ continuous
integration. This testing will free the upstream team to make changes without fear of side
effects downstream.
Every bounded context contans a single domain model, but the idea of a bounded
context is to make explicit the relationship between one system and another.
这⾥提到bouded context的另⼀个好处,是可以很容易的去划分团队,每个团队做它⾃⼰需要
做的事,⾮常清晰
Subdomain are used in the problem space to partition the problem domain. Bounded
contexts are used to defined the applicability of a model in the solution space.
A single team should own a bounded context.
Architectural patterns apply at the bounded context level, not at the application level.
If you don't have complex logic in a bounded context, use a simple CRUD architecture.
Speak a ubiquitous language within an explicitly bounded context.
A bounded context should be autonomous - owning the entire code stack from
presentation through domain logic and onto the database and schema.
#3 Aggregate
#3 Subdomain
#3 Context Mapping