<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adam Barreiro</title>
    <description>The latest articles on DEV Community by Adam Barreiro (@adambarreiro).</description>
    <link>https://dev.to/adambarreiro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F13496%2F5f6869ad-7ab6-4ae7-9dea-a3173b2cd8ba.jpg</url>
      <title>DEV Community: Adam Barreiro</title>
      <link>https://dev.to/adambarreiro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adambarreiro"/>
    <language>en</language>
    <item>
      <title>Domain-Driven Design: The cool parts (Part 2)</title>
      <dc:creator>Adam Barreiro</dc:creator>
      <pubDate>Wed, 10 Jun 2020 20:40:59 +0000</pubDate>
      <link>https://dev.to/adambarreiro/domain-driven-design-the-cool-parts-part-2-2p7d</link>
      <guid>https://dev.to/adambarreiro/domain-driven-design-the-cool-parts-part-2-2p7d</guid>
      <description>&lt;p&gt;In the &lt;a href="https://dev.to/adambarreiro/domain-driven-design-the-cool-parts-part-1-45p6"&gt;first part&lt;/a&gt; of this series we introduced the basic principles that serve as a core of the Domain-Driven Design: The ubiquitous language, the model and the layered architecture. In this post we'll review a first bunch of components used to model the domain: &lt;em&gt;Entities&lt;/em&gt;, &lt;em&gt;Value Objects&lt;/em&gt;, &lt;em&gt;Services&lt;/em&gt;, &lt;em&gt;Modules&lt;/em&gt; and &lt;em&gt;Aggregates&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Entities and Value Objects
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;Entity&lt;/strong&gt; is the most basic kind of component in the set of tools to model your domain, and basically it's an object that represents something that has an &lt;strong&gt;unique identity&lt;/strong&gt;, that is &lt;strong&gt;preserved&lt;/strong&gt; over time and thorough different &lt;strong&gt;representations&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, &lt;em&gt;you&lt;/em&gt; could be an instance of a "Citizen" entity. Why? Because it's probable that you have a document identifying you unequivocally in your country, it could be your passport number or your driver license. That ID is yours and can identify you despite the fact we can pick your &lt;em&gt;Citizen entity instance&lt;/em&gt;, transform it, save it into a database or send it over the Internet.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Value Object&lt;/strong&gt; in the other hand is something that is just the opposite of an Entity, it lacks of identity and its mere goal is to serve as transient data. The most obvious example is the Value Object "Color". Imagine that we have two instances of Color, both with "Red" value:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As they &lt;strong&gt;lack of an identity&lt;/strong&gt;, we can discard one because they're the same.&lt;/li&gt;
&lt;li&gt;As they lack of an identity, if you change the instance "Red" with another instance "Red", the result is the same. They're &lt;strong&gt;interchangeable&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;As they lack of an identity, they're &lt;strong&gt;immutable&lt;/strong&gt;. If an instance is "Red", there's no point in changing it to "Blue". Instead, "Red" should complete its lifecycle (being created, used and destroyed) and another Color instance with value "Blue" would be created.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With these properties, we can do a nice trick that could be interesting to save some resources. We could share one unique VO instance among many entities. For example, if we have 1000000000 users called "John", instead of creating the entity with that String, we could share a unique reference to the VO "Name" with value "John". This is just a dummy example, but you get the idea.&lt;/p&gt;

&lt;p&gt;Another important aspect of these two components is that they can have logic and operations (for example, validations). In fact, is the recommended approach. Having mere empty entities with only fields, getters and setters is a well-known antipattern called "&lt;a href="https://www.martinfowler.com/bliki/AnemicDomainModel.html"&gt;Anemic Domain Model&lt;/a&gt;".&lt;/p&gt;

&lt;p&gt;If you work with Java you'll probably know the JPA specification and some implementations such as Spring Data or Hibernate. Specially, the Spring ecosystem is very promoter of DDD, as it provides most of the kind of components explained here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Services
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Services&lt;/strong&gt; are operations that don't belong to an entity or value objects, but still have their own meaning and importance inside the model. They can also work creating some reusable logic for very granular domain entities. For example, think about an operation that performs the sign up of an student an assigns her to a specific classroom. This could be modelled as a service that interacts with two entities and describes a &lt;strong&gt;specific operation&lt;/strong&gt; inside a use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modules
&lt;/h2&gt;

&lt;p&gt;As their name suggest, &lt;strong&gt;modules&lt;/strong&gt; provide isolation to a specific group of entities, services and VOs from the rest of the model, so you can concentrate and focus separately in each part of it. Also it provides cohesion to the components grouped inside of them, and they shape the domain on a larger scale.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregates
&lt;/h2&gt;

&lt;p&gt;In most cases, we have a bunch of entities that are related one to another. For example, we could have the following entities: &lt;em&gt;Car&lt;/em&gt;, &lt;em&gt;Wheel&lt;/em&gt; and &lt;em&gt;Motor&lt;/em&gt;. If your model allows to manipulate these entities without any kind of constraint, you could end up managing &lt;em&gt;Wheels&lt;/em&gt; separately, or even a &lt;em&gt;Motor&lt;/em&gt;, without having any &lt;em&gt;Car&lt;/em&gt; instantiated. Does it make sense? Not at all:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You could end up with 500 wheels and no car.&lt;/li&gt;
&lt;li&gt;You could have many motors and one wheel, but still no car.&lt;/li&gt;
&lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do you see the weakness here? To solve this, you should manage &lt;strong&gt;only&lt;/strong&gt; a &lt;strong&gt;root&lt;/strong&gt; Entity (in this case, the &lt;em&gt;Car&lt;/em&gt;), that should contain &lt;em&gt;Wheels&lt;/em&gt; and a &lt;em&gt;Motor&lt;/em&gt;. If you need to acces the wheels of the car, it should be done through the Car entity, other kind of access to the entities should be &lt;strong&gt;completely restricted&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this example, the &lt;em&gt;Car&lt;/em&gt; is the &lt;strong&gt;aggregate&lt;/strong&gt;. Aggregates provide &lt;strong&gt;isolation&lt;/strong&gt; to the contained components and &lt;strong&gt;protects&lt;/strong&gt; them of accidental or wrong access. Also, we can make stronger and &lt;strong&gt;strict invariants&lt;/strong&gt; (or business rules). For example, if the only way to manage wheels of a car is through the Car aggregate, you can force and be sure that the car has only 4 wheels.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;p&gt;So far, we defined a pretty basic set of components that are essential for you to be able to create your domain model, but there are some gaps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How can you &lt;strong&gt;create&lt;/strong&gt; an entity from scratch? Of course, you have constructors, but they're not practical because they can get complicated when we don't have trivial entities. For example, how could you create an aggregate that contains another aggregate, from scratch? Things get complicated, huh? In following posts we'll review a new component that solves this problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How can you &lt;strong&gt;retrieve&lt;/strong&gt;, &lt;strong&gt;save&lt;/strong&gt; and &lt;strong&gt;manage&lt;/strong&gt; entities that are persisted in a database? If you use a relational database, you would need to execute some SQL, map the columns to the entities fields... ouch, that looks very clumsy, and everything we achieved by separating the model from the infrastructure is suddenly screwed up. Don't worry, there's another specific component to solve this.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay tuned for the next part!&lt;/p&gt;

</description>
      <category>ddd</category>
      <category>architecture</category>
    </item>
    <item>
      <title>Domain-Driven Design: The cool parts (Part 1)</title>
      <dc:creator>Adam Barreiro</dc:creator>
      <pubDate>Thu, 05 Mar 2020 17:31:09 +0000</pubDate>
      <link>https://dev.to/adambarreiro/domain-driven-design-the-cool-parts-part-1-45p6</link>
      <guid>https://dev.to/adambarreiro/domain-driven-design-the-cool-parts-part-1-45p6</guid>
      <description>&lt;p&gt;Some weeks ago I started to read the famous book written by Eric Evans, &lt;em&gt;"&lt;a href="https://dddcommunity.org/book/evans_2003/"&gt;Domain-Driven Design: Tackling Complexity in the Heart of Software&lt;/a&gt;"&lt;/em&gt;. I'm planning to write a few posts to synthesize it and put the concepts that I liked the most in an easy and friendly way. As the famous quote says, &lt;em&gt;the best way to learn is to teach&lt;/em&gt;, so I'm doing this as an excercise to retain and internalize knowledge, but hopefully it will also help someone that is reading the book as well. I also invite you to share your thoughts in the discussion section!&lt;/p&gt;

&lt;p&gt;In this first part I'll focus on explaining the Domain-Driven Design mindset, which is the core that needs to be understood by the people who want to apply the principles in their daily basis, and start programming using this approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  Work as one collaborative team
&lt;/h2&gt;

&lt;p&gt;First of all, the main requirement for anyone planning to work using DDD is to be willing to collaborate as one team and one brain. This means that there can't be a separation between &lt;strong&gt;domain experts&lt;/strong&gt; (this is, business experts, product owners… you name it) and &lt;strong&gt;developers&lt;/strong&gt; (engineers, architects…). This close relationship will make developers understand business and business understand developers. Think about it, we as developers are very comfortable talking about classes, patterns and architectural elements… but domain experts normally don't understand these concepts, as well as we don't usually tend to care about the business rules, requirements, corner use cases and so on.&lt;/p&gt;

&lt;p&gt;The idea is that together, domain experts and developers can come to a final situation in which &lt;strong&gt;they speak the same language&lt;/strong&gt;. Through several brainstormings, talks, reviews and refinements, the &lt;strong&gt;domain model&lt;/strong&gt; will start to emerge using this common language (aka &lt;em&gt;"ubiquitous language"&lt;/em&gt;, in words of Eric Evans). For example, a developer will understand what a "cargo shipment" is, or a "VIP customer", or a "health insurance", etc; for that company and their business colleagues. In addition, a domain expert will start to understand the relationships that developer is creating across classes, and what these classes do mean.&lt;/p&gt;

&lt;p&gt;With several iterations, domain experts and developers should refine the model, &lt;strong&gt;continuously learning&lt;/strong&gt;, filling gaps and fixing contradictions, working as one unit.&lt;/p&gt;

&lt;h2&gt;
  
  
  But… what is the model?
&lt;/h2&gt;

&lt;p&gt;The model is like the skeleton, structure or &lt;strong&gt;backbone that gives shape to the common language&lt;/strong&gt; that has been created by domain experts and developers. It can be done using different tools, like UML diagrams that represent classes, rules, operations, interactions, relationships… but we should take into account that the most important thing is the language and the expresiveness of the model. If something is not understandable through UML, use explanatory or auxiliary diagrams, use text, drawings… anything that supports and enriches the model and is supported by the language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lYPCuQ5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s6uo3abbu17v5pqma2wr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lYPCuQ5e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s6uo3abbu17v5pqma2wr.png" alt="You can even use MS Paint if that helps improving the model!"&gt;&lt;/a&gt;&lt;/p&gt;
You can even use MS Paint if that helps improving the model!



&lt;h2&gt;
  
  
  Implementing the model
&lt;/h2&gt;

&lt;p&gt;As we need to finally implement the model, the design of the solution has to be &lt;strong&gt;bounded&lt;/strong&gt; with it. This means, any change to the model should be reflected in the design (and supported by the language!) and viceversa.&lt;/p&gt;

&lt;p&gt;This implementation has to be doable, of course. Therefore, &lt;strong&gt;models must be practical&lt;/strong&gt; (no hundreds of thousands of classes, please), and if we want the solutions and systems to last, the developers and domain experts should continue working closely throughout all the project lifecycle.&lt;/p&gt;

&lt;p&gt;In terms of architecture, &lt;strong&gt;it's crucial to isolate the domain in its own layer&lt;/strong&gt;, no matter which type of architecture we choose. For example, it's common to feel the temptation of including business logic in UI. Wrong! Spreading the business logic across layers will make the solution hard to mantain and hard to understand. If we make a change to the business logic, we want to impact the less possible layers and components in our architecture.&lt;/p&gt;

&lt;p&gt;In his book, Eric Evans talks about the following layered architecture as reference, from top to bottom:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5X9EGNqG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gd1uqmqowz60mf21elz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5X9EGNqG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gd1uqmqowz60mf21elz2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;UI layer:&lt;/strong&gt; Shows info to the users and interprets commands.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application layer:&lt;/strong&gt; Defines jobs and coordinates tasks, but doesn't contain business logic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain layer:&lt;/strong&gt; Represents the business logic. Remember, &lt;strong&gt;isolated&lt;/strong&gt;!&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Infrastructure layer:&lt;/strong&gt; Provides technical capabilities for the upper layers. For example, database configurations, transactions, caches...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And one important aspect of the development process is that we tend to use frameworks a lot. So we need to take into account that we should use frameworks that allow to put the focus in the domain model, and most important, to isolate it.&lt;/p&gt;

&lt;h2&gt;
  
  
  And that's it for today!
&lt;/h2&gt;

&lt;p&gt;In following parts of this series about DDD I'll explore the implementation details, how we can express this model through several elements, such as entities, value objects, services, modules and more.&lt;/p&gt;

</description>
      <category>ddd</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
