In Domain Driven Design, may a transaction modify more than one aggregate?

meriton

In "Domain Driven Design: Tackling Complexity in the Heart of Software", Evans defines

An Aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes.

Obviously this implies that an aggregate must be updated in a single transaction.

However, must a transaction only update a single aggregate? If so, why?

My Research

I ask because, in "Implementing Domain Driven Design", page 360, Vernon writes:

Both the referencing aggregate and the referenced aggregate must not be modified in the same transaction. Only one or the other may be modified in a single transaction.

but does not give a reason for this rule.

I understand that if business rules require a single transaction, this indicates a hidden invariant, which would require the entities to be part of the same aggregate. But what if the business does not care, and developers simply find it convenient?

On page 437, Vernon also writes:

Be careful not to overuse the ability to commit modifications to multiple aggregates in a single transaction just because it works in a unit test environment. If you aren't careful, what works well in development and test can fail severely in production because of concurrency issues.

Which concurrency issues are those?

plalx

Optimistic concurrency is often used to avoid data losses in an environment where contention exists.

Let's see what would cause a concurrency exception with that mechanism in place:

  1. User Foo load aggregate A which is of version V1.
  2. User Bar load aggregate A which is of version V1.
  3. User Foo changes the aggregate A and persists (version is incremented to V2).
  4. User Bar changes the aggregate A and tries to persist but will get a concurrency exception because his changes were based on V1, but the aggregate is now at V2.

If you allow modifying more than a single aggregate per transaction, you are increasing the risk of concurrency exceptions, which might harm the scalability of your system up to the point of making it unusable.

Aggregate roots (AR) are transactionnal boundaries, where invariants are transactionnaly consistent, so if you find yourself trying to modifying multiple ARs in the same transaction it means that your AR boundaries are probably wrong and that you might be missing the chance of making an implicit concept explicit.

However, note that there should be no problem in creating multiple ARs in a single transaction.

Common mistakes when designing ARs are to create large clusters by giving too much importance to the relation words in statements like:

"Posts have comments"

There's no reason to cluster Post and Comment together if there's no invariant enforcement which requires that composition. Should two authors posting on the same comment at the same time cause a concurrency exception? The answer is probably no, depending on your domain, but it would if Post contained a collection of Comment.

But what if the business does not care, and developers simply find it convenient?

Well, if the business do not care about their system being unscalable due to bad design decisions I guess that's their choice, but developers shouldn't make a habit of designing for convenience. DDD is about modeling the domain the way it is, not the way it is convenient to model.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

When we shouldn't use Domain-Driven Design approach?

来自分类Dev

Crouton with more than one OS

来自分类Dev

Remove more than one view on PagerAdapter

来自分类Dev

How can I populate more than one zendform?

来自分类Dev

Visual Studio Code: "Program has more than one entry point defined"

来自分类Dev

SQL query for selecting foreign-key rows with more than one link to the primary-key table

来自分类Dev

Select rows where column contains same data in more than one record

来自分类Dev

Ninject - Multiple Classes Using Single Interface (more than one matching bindings are available)

来自分类Dev

Error while copying more than 8202 characters from one cell to another

来自分类Dev

"List elements that take up more than one line are not permitted" error with complex IF statement

来自分类Dev

子查询结果 'Single Row Sub-Query results more than One Row'

来自分类Dev

How to Select more than WMI Query

来自分类Dev

How to use $compile service more than once?

来自分类Dev

Merge Statement for more than hundreds of columns

来自分类Dev

Design appearance programmatically or graphically, which one is better?

来自分类Dev

Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

来自分类Dev

Is igraph R package more perfect than python-igraph?

来自分类Dev

Is there a way to avoid writing inline views more than once in MySQL

来自分类Dev

How can A left outer join B return more rows than are in A?

来自分类Dev

Python 2.7 Value Error : need more than 2 values to unpack

来自分类Dev

Cannot add same key to dictionary more than once

来自分类Dev

Prevent angular directive from compiling more than once

来自分类Dev

How to handle more than 11 dependencies to a task in SBT

来自分类Dev

Database update error No Value given for one or more required parameters

来自分类Dev

使用AngularJS中的指令将templateUrl更改为“ more then one”

来自分类Dev

java recursion: object is replaced rather than adding a new one

来自分类Dev

Find files recursively that are older than one year and not owned by a specific user

来自分类Dev

Inline-block <div> pushed down outside of parent, height more than parent despite height: inherit;

来自分类Dev

is calling a variable more expensive than calling a function in another function's paramters?

Related 相关文章

  1. 1

    When we shouldn't use Domain-Driven Design approach?

  2. 2

    Crouton with more than one OS

  3. 3

    Remove more than one view on PagerAdapter

  4. 4

    How can I populate more than one zendform?

  5. 5

    Visual Studio Code: "Program has more than one entry point defined"

  6. 6

    SQL query for selecting foreign-key rows with more than one link to the primary-key table

  7. 7

    Select rows where column contains same data in more than one record

  8. 8

    Ninject - Multiple Classes Using Single Interface (more than one matching bindings are available)

  9. 9

    Error while copying more than 8202 characters from one cell to another

  10. 10

    "List elements that take up more than one line are not permitted" error with complex IF statement

  11. 11

    子查询结果 'Single Row Sub-Query results more than One Row'

  12. 12

    How to Select more than WMI Query

  13. 13

    How to use $compile service more than once?

  14. 14

    Merge Statement for more than hundreds of columns

  15. 15

    Design appearance programmatically or graphically, which one is better?

  16. 16

    Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

  17. 17

    Is igraph R package more perfect than python-igraph?

  18. 18

    Is there a way to avoid writing inline views more than once in MySQL

  19. 19

    How can A left outer join B return more rows than are in A?

  20. 20

    Python 2.7 Value Error : need more than 2 values to unpack

  21. 21

    Cannot add same key to dictionary more than once

  22. 22

    Prevent angular directive from compiling more than once

  23. 23

    How to handle more than 11 dependencies to a task in SBT

  24. 24

    Database update error No Value given for one or more required parameters

  25. 25

    使用AngularJS中的指令将templateUrl更改为“ more then one”

  26. 26

    java recursion: object is replaced rather than adding a new one

  27. 27

    Find files recursively that are older than one year and not owned by a specific user

  28. 28

    Inline-block <div> pushed down outside of parent, height more than parent despite height: inherit;

  29. 29

    is calling a variable more expensive than calling a function in another function's paramters?

热门标签

归档