DDD - 虚构聚合根
DDD - Fictitious Aggregate Root
有时我会遇到这种情况,因为我有一堆 实体域模型 应该在事务上持久化但没有逻辑域模型可以成为所有这些实体域模型[的聚合根 =27=].
在这些情况下,拥有一个 虚构聚合根 域模型是否是一个好主意,它没有类比数据库实体,并且将不会持久保存在数据库中,但只会在自身中存储用于事务持久化 实体域模型 ?
的逻辑
P.S。我对此很纠结,因为让一个数据库 table 只存储一列聚合根 ID 对我来说似乎是错误的。
Is it a good idea in these cases to have a fictitious aggregate root domain model which will have NO analogical database entity and will not be persisted in the database but will store in itself only logic for transactionally persisting entity domain models ?
有点。
有一个 PurpleMonkeyDishwasher
将构成您的聚合的实体组合在一起是非常好的,这样您就可以确保您的数据保持一致并满足您的域不变性。
但是真的很可疑它没有名字。这表明您并没有真正理解您正在建模的问题。
它相当于代码味道的建模。可能有一个主题将这些实体安排在一起建模,排除其他实体,而不是采用其他安排。在一起讨论这些实体时,您的领域专家可能会使用一个名词。去找吧。这是工作的一部分。
“没有类比数据库实体且不会持久保存在数据库中的聚合根域模型”不是 "虚构集合";它是一个标准聚合,就像另一个需要持久化的聚合一样。聚合的目的是控制遵循域规则的变化,以确保一致性和不变性。
有时聚合 是 变化(并且需要持久化)但有时它 不是 和之后要持久化的东西更改是 parts/full 实体 and/or VO,它们在聚合内部发生更改,并在其自身的持久性中进行映射,而无需编写持久性概念(table/s、文档等)。这是关于您如何决定保留域数据的实施细节。
DDD的第一个前提:没有DataBase。这有助于您在尝试在您的域中映射持久性概念时不要过于偏颇。
Mike his blog 解释得比我好。
The purpose of our aggregate is to control change, not be the change.
Yes, we have data there organized as Value Objects or Entity
references but that’s because it’s the easiest and most maintainable
way to enforce the business rules. We’re not interested in the state
itself, we’re interested in ensuring that the intended changes respect
the rules and for that we’re ‘borrowing’ the domain mindset i.e we
look at things as if WE were part of the business.
An aggregate instance communicates that everything is ok for a
specific business state change to happen. And, yes, we need to persist
the busines state changes. But that doesn’t mean the aggregate itself
needs to be persisted (a possible implementation detail). Remember
that the aggregate is just a construct to organize business rules,
it’s not a meant to be a representation of state.
So, if the aggregate is not the change itself, what is it? The change
is expressed as one or more relevant Domain Events that are generated
by the aggregate. And those need to be recorded (persisted) and
applied (interpreted). When we apply an event we “process” the
business implications of it. This means some value has changed or a
business scenario can be triggered.
有时我会遇到这种情况,因为我有一堆 实体域模型 应该在事务上持久化但没有逻辑域模型可以成为所有这些实体域模型[的聚合根 =27=].
在这些情况下,拥有一个 虚构聚合根 域模型是否是一个好主意,它没有类比数据库实体,并且将不会持久保存在数据库中,但只会在自身中存储用于事务持久化 实体域模型 ?
的逻辑P.S。我对此很纠结,因为让一个数据库 table 只存储一列聚合根 ID 对我来说似乎是错误的。
Is it a good idea in these cases to have a fictitious aggregate root domain model which will have NO analogical database entity and will not be persisted in the database but will store in itself only logic for transactionally persisting entity domain models ?
有点。
有一个 PurpleMonkeyDishwasher
将构成您的聚合的实体组合在一起是非常好的,这样您就可以确保您的数据保持一致并满足您的域不变性。
但是真的很可疑它没有名字。这表明您并没有真正理解您正在建模的问题。
它相当于代码味道的建模。可能有一个主题将这些实体安排在一起建模,排除其他实体,而不是采用其他安排。在一起讨论这些实体时,您的领域专家可能会使用一个名词。去找吧。这是工作的一部分。
“没有类比数据库实体且不会持久保存在数据库中的聚合根域模型”不是 "虚构集合";它是一个标准聚合,就像另一个需要持久化的聚合一样。聚合的目的是控制遵循域规则的变化,以确保一致性和不变性。
有时聚合 是 变化(并且需要持久化)但有时它 不是 和之后要持久化的东西更改是 parts/full 实体 and/or VO,它们在聚合内部发生更改,并在其自身的持久性中进行映射,而无需编写持久性概念(table/s、文档等)。这是关于您如何决定保留域数据的实施细节。
DDD的第一个前提:没有DataBase。这有助于您在尝试在您的域中映射持久性概念时不要过于偏颇。
Mike his blog 解释得比我好。
The purpose of our aggregate is to control change, not be the change. Yes, we have data there organized as Value Objects or Entity references but that’s because it’s the easiest and most maintainable way to enforce the business rules. We’re not interested in the state itself, we’re interested in ensuring that the intended changes respect the rules and for that we’re ‘borrowing’ the domain mindset i.e we look at things as if WE were part of the business.
An aggregate instance communicates that everything is ok for a specific business state change to happen. And, yes, we need to persist the busines state changes. But that doesn’t mean the aggregate itself needs to be persisted (a possible implementation detail). Remember that the aggregate is just a construct to organize business rules, it’s not a meant to be a representation of state.
So, if the aggregate is not the change itself, what is it? The change is expressed as one or more relevant Domain Events that are generated by the aggregate. And those need to be recorded (persisted) and applied (interpreted). When we apply an event we “process” the business implications of it. This means some value has changed or a business scenario can be triggered.