{"id":389,"date":"2018-12-03T20:07:27","date_gmt":"2018-12-03T19:07:27","guid":{"rendered":"http:\/\/blog.opus.ch\/?p=389"},"modified":"2021-02-23T17:43:12","modified_gmt":"2021-02-23T16:43:12","slug":"ddd-concepts-and-patterns-entities","status":"publish","type":"post","link":"https:\/\/opus.ch\/en\/ddd-concepts-and-patterns-entities\/","title":{"rendered":"DDD Concepts and Patterns \u2013 Entities"},"content":{"rendered":"<div class=\"wpb-content-wrapper\">[vc_row][vc_column][vc_column_text]The first pattern of domain driven design (DDD) I would like to take a closer look at is the \u201cEntity\u201d tactical design pattern. We all know the term \u201centity\u201d and may have a certain picture in our mind when we read it. I was first introduced to it while learning about entity relationship diagrams that are used to design databases. However, in DDD an entity is an element of the domain model, a kind of domain object.<\/p>\n<p>The domain model entity element has distinct characteristics which separate it from value objects, another pattern that DDD uses to model domain objects. The main differences are that entities have an identity which stays the same during their lifecycle and that they are mutable whereas value objects are immutable.<\/p>\n<h3>The Pattern<\/h3>\n<p>An entity is a domain model element that represents some domain object. It has attributes and methods. For example, a ship that has a certain name and capacity.<\/p>\n<p>One of the main characteristics of an entity is that it has an identity. The identity never changes through the life cycle of an entity. An example of this is a person who has a certain name. When the name changes if that person is married for example, the identity has to be preserved.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-398\" src=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogpersonentity-300x67.png\" alt=\"\" width=\"502\" height=\"112\" srcset=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogpersonentity-300x67.png 300w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogpersonentity-600x134.png 600w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogpersonentity-768x171.png 768w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogpersonentity.png 852w\" sizes=\"auto, (max-width: 502px) 100vw, 502px\" \/><\/p>\n<p>When comparing entities with each other, we cannot rely on their attributes. Two person objects with the same name value don\u2019t necessarily have to represent the same Person. On the other hand, consider a contact detail object with an attribute phone number. This object doesn\u2019t have an identity on its own, every contact detail object with the same phone number could replace it with no change in meaning.<\/p>\n<p>Entities have a life cycle which starts when they are created. They can be loaded from a persistence store, changed due to an event or action and then stored to persistence again. At the end of their lifecycle, they are destroyed. For example, a car is built which starts its lifecycle. Then after some years in use, some parts are replaced. Finally, when we take the car to the scrap yard where they dismantle it, its lifecycle ends.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-396 size-large\" src=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogentityidentity2-600x167.png\" alt=\"\" width=\"600\" height=\"167\" srcset=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogentityidentity2-600x167.png 600w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogentityidentity2-300x83.png 300w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogentityidentity2-768x213.png 768w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogentityidentity2.png 1181w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<h3>Commands and Queries<\/h3>\n<p>Methods of entities are either commands or queries. Commands are used to change the state of the entity, for example set an attribute to a new value. They never return state. In contrast, query methods should never change the state of an entity but return some value.<\/p>\n<p>This distinction makes it easier to reason about our code. We know that a query method doesn\u2019t change anything. When calling a command, we know that we may better carefully inspect what it does exactly.<\/p>\n<h3>Validation of invariants<\/h3>\n<p>The entity pattern advises us to model the invariants of an entity. Invariants are facts or rules that are always true after a command made a change to an entity. For example, a car needs to have always exactly four wheels. So after the command that changes the wheel of a car, we need to check this invariant.<\/p>\n<p>Therefore after each command or series of commands that we execute on the entity we need to validate it. Even when factories create new entities, they should be in a valid state from the beginning of their life cycle.<\/p>\n<h3>Associations<\/h3>\n<p>Eric Evans also discusses associations in his DDD book [Eva04]. Many associations are bi-directional in nature, and therefore we model them as such initially. If we translate those directly into the software, the model becomes very entangled and hard to maintain. Therefore we should strive to minimize associations within the model and code.<\/p>\n<p>Three ways to reduce associations are imposing a traversal direction, reducing multiplicity by introducing a qualifier and removing the association altogether. The following sections describe a sample of each possibility.<\/p>\n<h3>Impose direction<\/h3>\n<p>Imagine my favorite sample of the webshop. There are baskets to which users add products. The association between baskets and products is many-to-many. Does this imply that we need to maintain a list of baskets inside the product entity?<\/p>\n<p>Most of the time it should suffice to be able to query the basket for its products. A product doesn\u2019t have to know in which baskets it is stored. That\u2019s a question the application may never need to answer. If it does, we can query the repository (database) for the answer.<\/p>\n<p>If we needed to maintain both directions within the model, every addition of a product to the basket would need another operation that adds the basket to the list of baskets of the product. By imposing the direction, we can simplify the code.<\/p>\n<h3>Reducing Multiplicity<\/h3>\n<p>The following diagram shows the association between student and subject. It is a many-to-many association because students study multiple subjects and subjects are studied by multiple students.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-400\" src=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubject-300x84.png\" alt=\"\" width=\"501\" height=\"140\" srcset=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubject-300x84.png 300w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubject-600x168.png 600w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubject-768x215.png 768w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubject.png 916w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/p>\n<p>To reduce the multiplicity we could, for example, add time slots to the model. At a given timeslot a student can only study one subject. Therefore the multiplicity is reduced to one-to-many.<br \/>\nThis association is much easier to maintain in the code of the model.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-401\" src=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubjectbetter.png\" alt=\"\" width=\"600\" height=\"274\" srcset=\"https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubjectbetter.png 1016w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubjectbetter-300x137.png 300w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubjectbetter-600x274.png 600w, https:\/\/opus.ch\/wp-content\/uploads\/2018\/12\/blogstudentsubjectbetter-768x351.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<h3>Removing associations<\/h3>\n<p>The ultimate simplification is removing associations altogether. When the association is purely informative for example, or when it is used only in a given context we may better remove it. For example, the application for a publishing company may records reviewers of a book during the publishing process. However, once the book is printed and added to the store of the company, there is no need for the association to the reviewers anymore.<\/p>\n<h3>Wrap Up \/ Final Thoughts<\/h3>\n<p>The entity pattern defines domain entities which can be modeled and translated to code. It divides methods into commands and queries with clearly defined semantics. Entities maintain a lifecycle, and we can minimize their associations in multiple ways.<\/p>\n<p>The next pattern in the series will be value objects. Generally, they should be preferred over entities when modeling. Let&#8217;s see why in the next post.<\/p>\n[Eva04] Eric Evans: Domain-Driven Design \u2013 Tackling Complexity in the Heart of Software (<a href=\"https:\/\/domainlanguage.com\/\" target=\"_blank\" rel=\"noopener\">homepage<\/a>)[\/vc_column_text][\/vc_column][\/vc_row]\n<\/div>","protected":false},"excerpt":{"rendered":"<p>[vc_row][vc_column][vc_column_text]The first pattern of domain driven design (DDD) I would like to take a closer look at is the \u201cEntity\u201d tactical design pattern. We all know the term \u201centity\u201d and may have a certain picture in our mind when we read it. I was first introduced to it while learning about entity relationship diagrams that&hellip;<\/p>\n","protected":false},"author":2,"featured_media":415,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,16],"tags":[],"post_series":[],"class_list":["post-389","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ddd-en","category-patterns-en","entry","has-media"],"_links":{"self":[{"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/posts\/389","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/comments?post=389"}],"version-history":[{"count":1,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/posts\/389\/revisions"}],"predecessor-version":[{"id":1119,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/posts\/389\/revisions\/1119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/media\/415"}],"wp:attachment":[{"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/media?parent=389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/categories?post=389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/tags?post=389"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/opus.ch\/en\/wp-json\/wp\/v2\/post_series?post=389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}