<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Sapiens Works</title>
    <description>Maintainable code is a business advantage</description>
    <link>http://blog.sapiensworks.com</link>
    <atom:link href="http://blog.sapiensworks.com/feed.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Using DDD, CQRS, Event Sourcing With A Vague, Fast Changing Domain</title>
        <description>&lt;p&gt;If you’re a startup, there’s a high chance that your product/service is new and disruptive. This means you (or the stakeholders) have just a vague idea of what you want and what can be called Domain is just a mishmash of things that change every day. Is it a good idea to use DDD, CQRS or Event Sourcing in this case?&lt;/p&gt;

&lt;p&gt;Yes, it is! It will simplify your work greatly, &lt;em&gt;if&lt;/em&gt; you really understand DDD/CQRS/ES . At this point, you might be puzzled how can DDD be a good choice when the domain is fuzzy and there’s no domain expert. Not a big problem, because it’s all about understanding what the stakeholder want to accomplish, step by step.&lt;/p&gt;

&lt;p&gt;Now truth be told, especially in cases like this, you’d be happy if you can get even a half-assed idea, because nobody really knows what they want, it’s all about vague, raw concepts and maybe, very crude business processes. But regardless, you can latch on the most important thing: language.&lt;/p&gt;

&lt;p&gt;Sure, the domain language is volatile, things will change daily and many times drastically, but the good news is you need to live in the present, not the future. Present concepts, present rules are what matter, we don’t care much about ‘what if’. So yeah, basically an iron YAGNI.&lt;/p&gt;

&lt;p&gt;In order to keep things sane and productive, first step is to see the whole application as a group of (high level)functionalities. For example, for Twitter would be stuff like: register, login, follow, tweet, retweet etc, basically all the app features that can be used by the users(I’m including API here too). So, start identifying &lt;strong&gt;application functionality&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Next step is, when modelling, to treat each functionality as independent. What I mean here is that the resulting model for one functionality should be decoupled from other models. Applying CQRS, it means that &lt;strong&gt;command models&lt;/strong&gt; are &lt;em&gt;not reusable&lt;/em&gt;, they are &lt;em&gt;unique and specific&lt;/em&gt; to each functionality. From DDD point of view, it means we don’t reuse Aggregates. This way, each functionality is decoupled and they can evolve differently, according to needs.&lt;/p&gt;

&lt;p&gt;What about Event Sourcing? If domain changes a lot, then the events will change too, right? Is it worth it? Of course it is! First of all, &lt;strong&gt;domain events&lt;/strong&gt; are notifications of changes that already happened. Their structure may change if details change (but the data itself remains), but when it comes to business rules the changes affect the future, not the past.&lt;/p&gt;

&lt;p&gt;Let me give you an example. Chances are you have a degree/certification in something. In order to get that you had to pass an exam that verified that you have certain knowledge. Let’s say 10 years have passed. The syllabus changed and the exam now requires any exam taker to know the new stuff. So, anyone passing the exam now will be according to the new rules and content. But what about your 10 years old degree? It stays the same, you don’t need to change it, the new rules don’t apply to you (the past).&lt;/p&gt;

&lt;p&gt;The events are the degrees obtained after the exam (according to at-the-moment business rules). Once you have them,they don’t go back to being re-validated when business rules(exam) change. They are old data and yes, their details can change and you add/remove those new fields to make it technical compatible to current version. But in the end, they are just data. When things change, our model change but the existing data stays the same. We should also acknowledge that the event class structure as an implementation of a domain event is not a synonym (but a destination) for the event data stored in the db.&lt;/p&gt;

&lt;p&gt;However, this require a certain approach i.e your aggregates should basically be &lt;strong&gt;events factories&lt;/strong&gt;. A business object should contain only rules and a transient state that wouldn’t be persisted. Simply put, your business objects don’t contain business data with rules, only the business rules required to create events.&lt;/p&gt;

&lt;p&gt;With ES and CQRS, a specific domain state can be restored as a &lt;em&gt;readonly model&lt;/em&gt; by replaying events (which basically means assigning data).&lt;/p&gt;

&lt;p&gt;By doing that, you keep things maintainable. The domain model implementation is mainly rules (organized as specific one-use aggregates, value objects and services), while persistence gets only the valid relevant business data. The important thing here is that persistence contains the &lt;em&gt;whole domain state&lt;/em&gt; expressed as events streams and projections.&lt;/p&gt;

&lt;h2 id=&quot;simple-example&quot;&gt;Simple Example&lt;/h2&gt;

&lt;p&gt;Let’s assume you’re building the next Facebook and right now the registration functionality requires only username and password. You’ll end up with an &lt;code class=&quot;highlighter-rouge&quot;&gt;UserRegistered&lt;/code&gt; event.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserRegistered&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Username&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PasswordHash&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Two weeks pass and you decide that all new users needs to provide an email address too. This means that you need to change the aggregate to enforce the new rule. Also, the event (class) itself needs to be changed (add property Email). Any new user will be required to provide an email, everything is good. What about the previous users?&lt;/p&gt;

&lt;p&gt;This is not a case where we can ‘upgrade’ somehow existing user info, so basically the app UI will present a popup after login, telling them to provide a valid email. This means adding another event &lt;code class=&quot;highlighter-rouge&quot;&gt;EmailUpdated&lt;/code&gt; or similar and now, the domain has the emails for the ‘old’ users as well. That’s why we look at the business state (persisted relevant business data expressed as events) as a whole. It can be updated by means of different aggregates and events. The projected queryable model is also updated.&lt;/p&gt;

&lt;p&gt;Regarding technical implementation, while the event structure has changed, the existing data is still the same and this means you need to map the old data to new structure (which is the job of the Event Store). As I’ve said before, old events aren’t re-validated by new rules so the question now is, what happens when we replay events and the projected read model.&lt;/p&gt;

&lt;p&gt;I’ll start with the latter. Using a RDBMS means you need to update one or more tables definition in this case you add a new column &lt;code class=&quot;highlighter-rouge&quot;&gt;Email&lt;/code&gt; which is nullable. The read model needs to reflect the fact that not all existing users have an email. This is a simple scenario and here things are as CRUD as they can get.&lt;/p&gt;

&lt;p&gt;If a db schema changes very much or you’re using a nosql db, maybe the easiest method is to regenerate the whole read model.&lt;/p&gt;

&lt;p&gt;About restoring an object state by replaying events that contain old data, first of all is important to remember that the restored state is immutable and the events are just sources of data. No business rules come into play, an &lt;code class=&quot;highlighter-rouge&quot;&gt;Apply&lt;/code&gt; function just ‘maps’ information from an event to object state. Again, this function should be aware that Email can be empty.&lt;/p&gt;

&lt;p&gt;Basically, when the domain changes, we update the model in our app, but because we have and want to keep the old data, the model still needs to account for artifacts of old rules as they affect the current domain state. In our example, email wasn’t needed and that generated specific data, then email was added and the model now correctly requires email for new users while it also deals with the implications of the old rule i.e users without an email.&lt;/p&gt;

&lt;p&gt;As a domain evolves, the rules keep changing, but the existing business data doesn’t. Therefore everytime we deal with a business rule change, we have to consider its residual implications from a business point of view first(i.e non-technical) and technical second.&lt;/p&gt;

&lt;p&gt;In our example, regardless of how many times the rules change, it may always be 1 user who registered at the beginning and never returned. So, you either have the concept of an abandoned account that needs to be deleted or, as long as the app exists you need to remember that some users didn’t provide an email (business pov) and the Email property/column can be null/empty (implementation).&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Every business changes in time, the only difference is the pace. When dealing with a new, vague, volatile domain the pace is very fast but in the end you’d have the same issues you’d have when the domain changes at a slower pace. It’s harder because you have to grow yourself into a Domain Expert but from an application design and implementation point of view, things will be very similar. In the end, what makes the app maintainable is how we’ve applied the principles behind DDD/ES/CQRS. Recipes, libraries and frameworks are just tools that should be used according to the above-mentioned principles.&lt;/p&gt;
</description>
        <pubDate>Mon, 30 Oct 2017 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2017/10/30/Is-DDD-ES-CQRS-changing-domain</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2017/10/30/Is-DDD-ES-CQRS-changing-domain</guid>
      </item>
    
      <item>
        <title>How To Handle Business Rules in Domain Driven Design</title>
        <description>&lt;p&gt;The main point of DDD is to identify relevant domain behaviour, which means getting to know plenty of domain rules. Now, the tricky part is we have domain rules at different levels and all of them are important. Let’s start with the simplest ones.&lt;/p&gt;

&lt;h2 id=&quot;value-objects-vo&quot;&gt;Value objects (VO)&lt;/h2&gt;

&lt;p&gt;We’re using VOs mainly for their business semantics but a lot of them encapsulate simple business constraints. A VO implementation &lt;strong&gt;guarantees&lt;/strong&gt; that value is valid from a business point of view. And there can be many rules involving that value, however, at this level it’s more of basic rules like: “quantity &amp;gt; 0”. In some cases you might more complex validation rules and in that case, a Factory even a Domain Service are better suited to encapsulate those rules.&lt;/p&gt;

&lt;p&gt;For example, I might have a &lt;code class=&quot;highlighter-rouge&quot;&gt;TransactionDate&lt;/code&gt;that has 2 rules: it can’t be a weekend day and it can’t be an official holiday. It’s easy to enforce the first rule, &lt;code class=&quot;highlighter-rouge&quot;&gt;DateTime&lt;/code&gt; has properties for that, but hte second rule clearly is out of the VO scope. And since calculating holidays is not domain behaviour, the best thing is to have a Factory that will always create that VO.&lt;/p&gt;

&lt;p&gt;If you’re thinking: “But what about restoring it from the database?”, I’d argue that using a CQRS approach means VO are always created and stored, while a Read Model (of that VO) is restored. At least this is what I consider to be the most maintainable approach.&lt;/p&gt;

&lt;p&gt;To view things in a simple manner a VO = specific data + very close related business constraints.&lt;/p&gt;

&lt;h2 id=&quot;aggregates-agg&quot;&gt;Aggregates (Agg)&lt;/h2&gt;

&lt;p&gt;An aggregate by its definition is a group of ‘lower level’ concepts that need to always consistent. Technically speaking, an Agg is literally a group of value objects + Agg level domain rules. Note that these are rules that are &lt;strong&gt;required&lt;/strong&gt; to maintain the aggregate consistency i.e rules that keep things valid and prevent the Agg entering an invalid state.&lt;/p&gt;

&lt;p&gt;I will digress a bit here, because from the domain rules point of view the Agg contains validity constraints, while from a consistency point of view the Agg defines a consistent area in our business model, which technically speaking is a Unit of Work. From another point of view, the Agg works only with VOs (these are the Agg’s ‘data’) which themselves are a mix of data and business rules.&lt;/p&gt;

&lt;p&gt;So, which rules belong to the Agg? The ones which involve the role and the place of the VOs &lt;em&gt;inside&lt;/em&gt; the Agg. Stuff like: “all Agg components are required”. Never rules that involve the Agg itself as part of a bigger operation.&lt;/p&gt;

&lt;p&gt;Actually, the hardest and the most important part of identifying an Agg is to identify the relevant VOs, the Agg rules themselves are quite easy to spot. And if your business rule target the Agg as a whole then it’s an ‘outside of the Agg’ rule that should be part of a Domain Service.&lt;/p&gt;

&lt;h2 id=&quot;domain-services&quot;&gt;Domain Services&lt;/h2&gt;

&lt;p&gt;Most of the obvious business rules which don’t validate simple values will fit here. You know that “username must be unique” rule? That’s the one! Or “the user can post at most 15 videos” etc. A lot of developers mistakenly put this as part of the Agg which itself is improperly modelled, resulting in a long term mess waiting to happen.&lt;/p&gt;

&lt;p&gt;These should be part of a Domain Service and depending on the rule, it can be technically trivial or not, to implement it.&lt;/p&gt;

&lt;h3 id=&quot;business-impact&quot;&gt;Business Impact&lt;/h3&gt;

&lt;p&gt;Yeah, we have to talk about that ‘cuz the rules can have an… impact… on how simple or complex or maintainable your code will be. And that translates into how quickly you will deliver functionality aka productivity, how much technical debt you’ll going to acquire and how expensive (in money or work-hours) enforcing a business rule will be.&lt;/p&gt;

&lt;p&gt;Here it’s something you have to talk to the Domain Expert and the project owner. In the end everything boils down to what’s cheaper for the business: to enforce a rule 100% or to accept occasional violations.&lt;/p&gt;

&lt;p&gt;Let’s take the “user can post max 15 videos” rule. The simple implementation is to read your business state (read model for CQRS) and find out how many videos that user has. The only problem is concurrency (and not eventual consistency). What happens if the user presses the button twice fast or something.&lt;/p&gt;

&lt;p&gt;It’s a fact of life that everything is (perceived) delayed. Even with the fastest hardware and there’s still going to be a delay from the moment you sent that query until you get the results. 1 microsecond before or after someone changes things. It’s inevitable.&lt;/p&gt;

&lt;p&gt;And that matters when you’re dealing with high concurrency. Because at one point the business rule will be violated. The question is what happens next. Sure, you can come up with some complicated solution to enforce it, but is the Client willing to pay the price? Maybe is cheaper to ignore those edge cases with very little probability to happen and possible 0$ business impact. Or that rule needs to be enforced at all costs, no matter what.&lt;/p&gt;

&lt;p&gt;What if the rule is very important but it’s a line of business app used by at most 5 people with close to 0 chances to become a high concurrency app? We can implement a very simple solution which takes advantage of that and we’re very aware that won’t be scalable past a specific number (that we hope it won’t be achieved). As always, it depends, but you have to be aware of both the technical and business context. And there’s no single best solution that works for every situation.&lt;/p&gt;

&lt;p&gt;Luckily, very few applications need 100% percent enforced rules and are highly concurrent. And for those cases, you need &lt;em&gt;very specific&lt;/em&gt; solutions.&lt;/p&gt;

&lt;p&gt;In implementing rules, we look for the simplest solution and try to evaluate the probability and impact of the edge cases.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Business rules can be found everywhere in the domain and it’s our objective as domain modellers to assign those rules to the relevant patterns. And probably you’ll be surprised to find out that most rules are part of VOs and Domain Services. That’s why properly understanding all the patterns is the key to success: all are equally important and we should let the Domain tell us what belongs to where, instead of playing favourites (I’m looking at you ‘Aggregate’ classes full of primitive type properties).&lt;/p&gt;
</description>
        <pubDate>Wed, 23 Aug 2017 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2017/08/23/Handling-Business-Rules-DDD</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2017/08/23/Handling-Business-Rules-DDD</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Domain Relationships Explained</title>
        <description>&lt;p&gt;One of the most trickiest things to understand in DDD is the domain relationship between 2 concepts. Most of the time when people think relationship they use the programmer mindset and they look for: &lt;code class=&quot;highlighter-rouge&quot;&gt;has-a&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;is-a&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;parent-child&lt;/code&gt; or (worse) &lt;code class=&quot;highlighter-rouge&quot;&gt;one-to-many&lt;/code&gt; etc  relationships. Which is valid when you write code but &lt;strong&gt;absolutely wrong&lt;/strong&gt; when you do domain modelling.&lt;/p&gt;

&lt;p&gt;What we’re looking for is a &lt;strong&gt;Domain Relationship&lt;/strong&gt; i.e how the Domain sees the relationship from a business point of view or, to be more precise, what is the level of association/partnership between the concepts and what business rules are involved in maintaining the relationship. And usually there are 2 types: &lt;strong&gt;strong/always&lt;/strong&gt; and &lt;strong&gt;weak/optional&lt;/strong&gt;.&lt;/p&gt;

&lt;h1 id=&quot;strong-always-domain-relationships&quot;&gt;Strong (always) domain relationships&lt;/h1&gt;

&lt;p&gt;This one is the easiest: it simply means that in a given business case, a concept &lt;strong&gt;always&lt;/strong&gt; needs the other. Examples: Customer and Order, Author and Tutorial etc. Contrary to how many devs think, it’s never “Customer has orders”, it’s always an action about an &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; which might involve or not the &lt;code class=&quot;highlighter-rouge&quot;&gt;Customer&lt;/code&gt;. When the customer places an order, we need to create that order and we’re going to use a specific &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; aggregate. But orders don’t just appear from thin air, a customer is &lt;em&gt;always&lt;/em&gt; involved so we can’t create a new order without mentioning the customer.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; concept requires at its creation a &lt;code class=&quot;highlighter-rouge&quot;&gt;Customer&lt;/code&gt; so, in our Order model, we need to find the representation of the Customer that will be used by the Order aggregate. In most cases, we only need the simplest model that represents the &lt;em&gt;existence&lt;/em&gt; of the Customer, so we’ll use its id. That id is a Value Object and will be a part of the Order aggregate. We don’t care about a Customer aggregate, because we don’t need one for this business case. The &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; is the main concept, &lt;code class=&quot;highlighter-rouge&quot;&gt;Customer&lt;/code&gt; is secondary.&lt;/p&gt;

&lt;p&gt;However, if we are in a “Cancel order” case, there is no &lt;code class=&quot;highlighter-rouge&quot;&gt;Customer-Order&lt;/code&gt; relationship to care about (there is an &lt;code class=&quot;highlighter-rouge&quot;&gt;Order-OrderStatus&lt;/code&gt; one, though). So, we can cancel the Order without involving/mentioning the Customer, but, as I’ve said, there’s a different relationship to take care of now.&lt;/p&gt;

&lt;p&gt;Let’s say that an Order &lt;em&gt;always&lt;/em&gt; needs to have a status. It might be tempting to include it as part of the aggregate but… if you look at it like the business does, it makes no sense for this &lt;em&gt;Value Object&lt;/em&gt; to be part of it. What makes sense is to consider the &lt;code class=&quot;highlighter-rouge&quot;&gt;Order Status&lt;/code&gt; as being a sort of metadata associated with an Order.&lt;/p&gt;

&lt;p&gt;When the Order is created, the only place where this relationship should be represented is the resulting &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderCreated&lt;/code&gt; event. That’s because a status like “Pending” is implicit, we don’t need any particular input or business constraint for it. But we still need to ‘announce’ that the order has that status, because it is part of the business state change.&lt;/p&gt;

&lt;p&gt;Now, when cancelling an order, we need to specify a different status for the order, however because we can have different outcomes based on the existing order status, we need to identify an aggregate just for that. This aggregate involves the &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/29/DDD-Entities-Value-Objects-Explained&quot;&gt;Entity&lt;/a&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; (via OrderId) and the &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/29/DDD-Entities-Value-Objects-Explained&quot;&gt;Value Object&lt;/a&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderStatus&lt;/code&gt; and while being a model specific to changing the order status (representing the relationship itself) the resulting change is still associated with the &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; concept.&lt;/p&gt;

&lt;p&gt;Using Event Sourcing, this means that the &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderCanceled&lt;/code&gt; event is considered to be part of the &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; and so, the entity’s id is the order’s id. But the aggregate is about the &lt;em&gt;relationship&lt;/em&gt; between &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderStatus&lt;/code&gt;, it’s not just an Order aggregate. If this sounds weird, I can only say that it will make sense with more experience.&lt;/p&gt;

&lt;h1 id=&quot;weak-optional-domain-relationships&quot;&gt;Weak (optional) domain relationships&lt;/h1&gt;

&lt;p&gt;Basically, 2 concepts &lt;em&gt;can&lt;/em&gt; be associated together. Think &lt;code class=&quot;highlighter-rouge&quot;&gt;Post&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Category&lt;/code&gt;. Both concepts are independent, but they can work together. In the business case  “Assign post to category” we need to create the association between them according to business rules. The relationship aggregate will involve both concepts and we get an interesting situation, because we end up with 2 entities but we need only one to act as the aggregate root. Which will it be?&lt;/p&gt;

&lt;p&gt;Clearly, we shouldn’t just flip a coin. We need to understand the concepts better as well as the purpose of their association. It turns out that &lt;code class=&quot;highlighter-rouge&quot;&gt;Category&lt;/code&gt; exists to organize posts, while a &lt;code class=&quot;highlighter-rouge&quot;&gt;Post&lt;/code&gt; really doesn’t care about categories. This relationship is more important (relevant) for the &lt;code class=&quot;highlighter-rouge&quot;&gt;Category&lt;/code&gt; so, we select it to act as the aggregate root. This means that in the resulting event, the entity id would be that of the &lt;code class=&quot;highlighter-rouge&quot;&gt;Category&lt;/code&gt; while &lt;code class=&quot;highlighter-rouge&quot;&gt;PostId&lt;/code&gt; will be just a field.&lt;/p&gt;

&lt;p&gt;Being a relationship, even if changes are considered to ‘belong’ to a specific concept, the aggregate is &lt;em&gt;always&lt;/em&gt; a model for the relationship.&lt;/p&gt;

&lt;p&gt;An interesting example is deleting something. If I soft delete a post, what I have is in fact a new relationship between the &lt;code class=&quot;highlighter-rouge&quot;&gt;Post&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Deleted&lt;/code&gt; status, the status is never part of the post itself (as a concept) but always some metadata associated with the post. Btw, the status is a Value Object even if its implementation is just an enum.&lt;/p&gt;

&lt;p&gt;Another example can be “Pay invoice”. Yet again, a relationship between an Entity (&lt;code class=&quot;highlighter-rouge&quot;&gt;Invoice&lt;/code&gt;) and a Value Object (&lt;code class=&quot;highlighter-rouge&quot;&gt;InvoiceStatus&lt;/code&gt;) . The status is metadata for the invoice and we need a relationship aggregate to change the status. From the domain point of view, something associated with the Invoice entity has changed.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;The hardest part about domain relationships is to properly identify them; many times, especially when an Entity and a Value Object are involved, we can think that the VO is part of the Entity’s aggregate, when in fact it’s a relationship that might need its own aggregate. But with more practice, spotting relationships will become second nature.&lt;/p&gt;

</description>
        <pubDate>Wed, 24 Aug 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/08/24/DDD-Relationships</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/08/24/DDD-Relationships</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Modelling with CQS</title>
        <description>&lt;p&gt;While CQS/CQRS are a bit newer than DDD, they work so well together, one simply &lt;em&gt;has&lt;/em&gt; to use it. And I don’t mean CQRS from an architectural point of view, but from a modelling point of view.&lt;/p&gt;

&lt;p&gt;Simply put, when we identify a &lt;strong&gt;business case&lt;/strong&gt; we ask ourselves: “Does this case has the &lt;strong&gt;intention&lt;/strong&gt; to change the business state”? We’re not talking about a particular state (objects, tables etc) but about business state as a whole (remember that in real world any change is implicitly persistent). If so, then it’s a &lt;strong&gt;command case&lt;/strong&gt; else it’s a &lt;strong&gt;query case&lt;/strong&gt; (this is CQS at work).&lt;/p&gt;

&lt;p&gt;Being in a query case means that you know you won’t be changing anything and that the models will be read-only (this is CQRS, because we end up with specialised read &lt;strong&gt;models&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;But in a command case, things are a bit different. Many concepts can be involved however, usually only &lt;strong&gt;one&lt;/strong&gt; will be represented by a &lt;strong&gt;command model&lt;/strong&gt;. The other concepts will be represented by &lt;strong&gt;read models&lt;/strong&gt;. We apply CQS for each concept and we end up with specialised command/query models for each, basically CQRS (not to be mistaken for a CQRS architecture, which is a different thing, but based on the same CQS principle).&lt;/p&gt;

&lt;p&gt;As an example, let’s say we have the &lt;code class=&quot;highlighter-rouge&quot;&gt;Create invoice&lt;/code&gt; business case. Clearly, the business wants to retain the new invoice, so our business case will &lt;em&gt;change&lt;/em&gt; the business state. We’ve identified a concept too, the &lt;code class=&quot;highlighter-rouge&quot;&gt;Invoice&lt;/code&gt; for which we need to find a &lt;strong&gt;relevant representation&lt;/strong&gt;. However, because we’re creating the invoice, it means we need a &lt;strong&gt;command representation&lt;/strong&gt; i.e an &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-1&quot;&gt;Aggregate&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That’s great, but there’s more concepts involved besides the &lt;code class=&quot;highlighter-rouge&quot;&gt;Invoice&lt;/code&gt;. We need to get the products and prices from an &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt;, we also need to calculate &lt;code class=&quot;highlighter-rouge&quot;&gt;Tax&lt;/code&gt; and so on. Each of these concepts have a model as well, but because we don’t need to change the business state for which those concepts are in charge, it means we’re going to have &lt;strong&gt;read models&lt;/strong&gt; representations. So, the &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; model will be some read-only structure which contains all the data needed by the invoice. We always try to identify the most relevant model for that specific case, and not a generic model good for everything.&lt;/p&gt;

&lt;p&gt;As a generic ‘rule’, consider the business case as being like a bounded context i.e it has a model which makes sense only inside it. Concepts are involved in many business cases, but we work only with their most relevant models and based on the action we need, either write or read, we end up with a command model or a query model for &lt;em&gt;each&lt;/em&gt; concept.&lt;/p&gt;

&lt;p&gt;So, in a command case, we can find both command and query models of different concepts while in a query case, we’re dealing only with read (query) models. Applying CQS early when modelling helps us to know upfront what types of models we’re looking for, which gives us a bit more clarity and a productivity boost.&lt;/p&gt;
</description>
        <pubDate>Mon, 22 Aug 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/08/22/DDD-Cqs-Cqrs</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/08/22/DDD-Cqs-Cqrs</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Application Services Explained</title>
        <description>&lt;p&gt;We have all those domain models, &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-1&quot;&gt;aggregates&lt;/a&gt; and &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/08/16/DDD-Domain-Services-Explained&quot;&gt;domain services&lt;/a&gt; but they are just parts of business cases, floating around. We need something to put things together, to orchestrate the fulfillment of a specific business case. We need a manager of sorts and in DDD that role is performed by an &lt;strong&gt;Application Service&lt;/strong&gt;.&lt;/p&gt;

&lt;h1 id=&quot;what-does-an-application-service-as-actually-do&quot;&gt;What does an Application Service (AS) actually do?&lt;/h1&gt;

&lt;p&gt;The simplest way to look at it is as the ‘hosting environment’ of a business case. It takes care of invoking the correct domain models and services, it mediates between Domain and Infrastructure and it shields any Domain model from the outside. Basically, in a DDD app, only the Application Service interacts directly with the Domain model. The rest of the application only sees the Application Service (interesting choice of name).&lt;/p&gt;

&lt;p&gt;Business functionality is expressed as business cases and the AS acts as a shell, encapsulating one business case (it should anyway, in practice we can host a simple process i.e more than one business case, but that’s more of a pragmatic compromise). This means the UI/API or some higher layer talks to the AS responsible for that requested functionality and the AS gets to work. At most, the calling layer receives some result which shouldn’t be nor contain a domain model i.e entities or value objects.&lt;/p&gt;

&lt;p&gt;It’s important to note that an AS doesn’t have any business behaviour. It knows only &lt;strong&gt;what&lt;/strong&gt; model to use but not &lt;strong&gt;how&lt;/strong&gt; that model works. So, the AS knows which Aggregate to invoke, it knows about some Domain Services that might be required, but that’s it.&lt;/p&gt;

&lt;h1 id=&quot;an-example&quot;&gt;An example&lt;/h1&gt;

&lt;p&gt;Let’s continue the &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-2&quot;&gt;money transfer example&lt;/a&gt;. But now, we have the business rule that says the transfer can be done only if the account balance has enough money. So we’re introducing 2 Domain Services: &lt;code class=&quot;highlighter-rouge&quot;&gt;Calculate account balance&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Can account be debitted&lt;/code&gt; both implemented as functions of a &lt;code class=&quot;highlighter-rouge&quot;&gt;IDomainServices&lt;/code&gt; abstraction. But we also need an &lt;code class=&quot;highlighter-rouge&quot;&gt;AccountBalance&lt;/code&gt; Value Object.&lt;/p&gt;

&lt;p&gt;For obvious reasons, the code is heavily simplified. Also, this is just one of the possible implementations. It’s a matter of style in the end, as long as the code is clear and respects the domain model you can go wild with the implementation.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AccountBalance&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/* Domain Services implementation */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IDomainServices&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;AccountBalance&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CalculateAccountBalance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CanAccountBeDebitted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountBalance&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;balance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Debit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/* abstraction representing the query model, implementation resides in Infrastructure */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IDomainQueries&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;GetAccountNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/* Event store abstraction, basically our 'repository' */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IEventStore&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//command object, data structure&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TransferMoney&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Guid&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AccountFrom&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AccountTo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Amount&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/* Application Service!!! */&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TransferManager&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IEventStore&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDomainServices&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_svc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDomainQueries&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;readonly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ICommandResultMediator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TransferManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEventStore&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IDomainServices&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;svc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IDomainQueries&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ICommandResultMediator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_store&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_svc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;svc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_query&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_result&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TransferMoney&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//interacting with the Infrastructure&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;accFrom&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_query&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;GetAccountNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;//Setup value objects&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;//invoking Domain Services&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;balance&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_svc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CalculateAccountBalance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;accFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_svc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;CanAccountBeDebitted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;balance&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;//return some error message using a mediator&lt;/span&gt;
                &lt;span class=&quot;c1&quot;&gt;//this approach works well inside monoliths where everything happens in the same process &lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;_result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;AddResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;CommandResult&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;//using the Aggregate and getting the business state change expressed as an event&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;evnt&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Transfer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;cm&quot;&gt;/* args */&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;c1&quot;&gt;//storing the event&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;_store&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;evnt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            
            &lt;span class=&quot;c1&quot;&gt;//publish event if you want&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This particular approach uses a message driven architecture as well as Event Sourcing. We see how the AS orchestrates the business case; the domain model isn’t aware of the command object, the event store or the result mediator. One thing that might be confusing is the branching, it almost looks like the AS needs to know the domain rules of what happens when an account balance is too low, however it doesn’t :) . That’s one of the “tell me if I can continue with the business case” type of situations and the AS uses a Domain Service to get an answer for that. Again, it’s a matter of knowing &lt;strong&gt;who&lt;/strong&gt; is responsible for something, not &lt;strong&gt;how&lt;/strong&gt; something needs to be done. The AS &lt;em&gt;never&lt;/em&gt; micro-manages.&lt;/p&gt;

&lt;p&gt;If you want to keep things even simpler, you can call this service directly from the UI/API layer, and make &lt;code class=&quot;highlighter-rouge&quot;&gt;Execute()&lt;/code&gt; return a &lt;code class=&quot;highlighter-rouge&quot;&gt;CommandResult&lt;/code&gt;. We can implement things as complex or as simple we want, as always, it depends on many factors.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;The Application Service is the host of a business case and acts as a mediator between the app and the domain model. As long as you keep the domain behaviour inside the dedicated patterns and none leaks into the AS, you’re good to go. Implementations vary and there’s not a recipe of how a “good” AS looks like. Just remember its role and respect the domain model, the actual code is up to you.&lt;/p&gt;

</description>
        <pubDate>Fri, 19 Aug 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/08/19/DDD-Application-Services-Explained</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/08/19/DDD-Application-Services-Explained</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Domain Services Explained</title>
        <description>&lt;p&gt;Domain services are a bit confusing at first, when you don’t know exactly what a an Application Service is. Also, many developers try to cram a lot of business rules in their &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-1&quot;&gt;Aggregates&lt;/a&gt;, when a Domain Service (DS) would be more appropriate. But let’s start with the beginning….&lt;/p&gt;

&lt;h1 id=&quot;why-do-we-need-domain-services&quot;&gt;Why do we need Domain Services?&lt;/h1&gt;

&lt;p&gt;Some business rules don’t make sense to be part of an Aggregate. For example, you want to check an account balance before debiting an account. Or you want to see if a certain operation is allowed by the business rules (not to be mistaken for user authorization). Or you want to perform a simple calculation according to domain rules. Basically, any business rule required to move forward a business case, which doesn’t belong to an aggregate should be a Domain Service. This is the DDD term for &lt;strong&gt;business behaviour&lt;/strong&gt; outside an aggregate or a value object.&lt;/p&gt;

&lt;h1 id=&quot;how-to-identify-a-domain-service-ds&quot;&gt;How to identify a Domain Service (DS)?&lt;/h1&gt;

&lt;p&gt;The easiest way is to simply check if the rules are having to do with business constraints required to maintain an aggregate invariants, including its value objects. If they’re not, they’re a DS, even if it seems related to that aggregate somehow. Many times you need to generate a value object that will be part of the aggregate. For ex: you need a &lt;code class=&quot;highlighter-rouge&quot;&gt;Tax Calculator&lt;/code&gt; behaviour which will generate a &lt;code class=&quot;highlighter-rouge&quot;&gt;Taxes&lt;/code&gt; value object which is a part of the &lt;code class=&quot;highlighter-rouge&quot;&gt;Invoice&lt;/code&gt; Aggregate.&lt;/p&gt;

&lt;p&gt;In other cases, you might need to allow an operation. Remember the &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-2&quot;&gt;transfer money example&lt;/a&gt;? Wha if we have a rule that says that you can’t debit the account id the amount is lower than the balance? In this scenario we’re actually dealing with 2 Domain Services: one is used to calculate the &lt;code class=&quot;highlighter-rouge&quot;&gt;AccountBalance&lt;/code&gt; Value Object (VO) the other encapsulates the “amount must be greater than the balance” rule.&lt;/p&gt;

&lt;h1 id=&quot;what-about-the-anaemic-domain-anti-pattern&quot;&gt;What about the Anaemic Domain anti-pattern?&lt;/h1&gt;

&lt;p&gt;In a business case we can use multiple DS and this might look like the Anaemic Domain. But it’s not, the reason being that Aggregates should be as small as possible, not because someone said so, but because they should contain &lt;strong&gt;only the relevant behaviour&lt;/strong&gt; for that model. Not &lt;em&gt;every&lt;/em&gt; related behaviour. If something is ‘outside’ an Aggregate, then it’s probably is a Domain Service.&lt;/p&gt;

&lt;h1 id=&quot;implementation-tips&quot;&gt;Implementation tips&lt;/h1&gt;

&lt;p&gt;A DS is just a name signaling business behaviour. It doesn’t mean you have to implement it in one way or another. In many cases you can &lt;strong&gt;implement&lt;/strong&gt; all the domain services from that &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/08/12/DDD-Bounded-Contexts-Explained&quot;&gt;Bounded Context&lt;/a&gt; as (static) functions in one class, while in other cases you might want one class per DS. As with everything, it depends.&lt;/p&gt;

&lt;p&gt;A DS should be visible and consumed inside that Bounded Context only! Yes, you can define interfaces that can be used by your application service, they’re great for testing, however their concrete implementation should be in the same BC as the business cases where it’s used.&lt;/p&gt;

&lt;h2 id=&quot;external-services&quot;&gt;External services&lt;/h2&gt;

&lt;p&gt;Let’s say we need to calculate tax but the domain expert says they’re using some website to do it (and luckily for you, it provides an API and a client library). We need that functionality, but it’s implementation is not part of our Domain. This is what I call an External Service. The easiest way to use it is to define an abstraction (interface part of your BC) that will act as if it’s a Domain Service, however its implementation will be part of the Infrastructure and it will act as a wrapper around the client library. This is how we keep things decoupled. If later the calculation will be performed in-house, just implement a new class inside that BC and reconfigure the DI Container. Simple stuff.&lt;/p&gt;

&lt;p&gt;What if we need a service which is part of our app but part of another BC? For a monolith, you can cut corners and use it directly (assuming you weighted in the consequences). For a distributed app, I’d suggest the External Service approach. In the end, for the BC it’s something outside its boundaries, it doesn’t matter where the actual functionality is implemented as long as it’s not inside the boundaries. For the BC’s point of view, it’s an external service.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;We have Domain Services simply because we want to keep a concept’s model relevant, so any behaviour which doesn’t &lt;em&gt;naturally&lt;/em&gt; fit that model needs to be expressed somehow. Also DS shouldn’t care about state, they represent domain behaviour only (their implementation should be stateless).&lt;/p&gt;
</description>
        <pubDate>Tue, 16 Aug 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/08/16/DDD-Domain-Services-Explained</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/08/16/DDD-Domain-Services-Explained</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Bounded Contexts Explained</title>
        <description>&lt;p&gt;In DDD, we organize the Domain into Bounded Contexts(BC), with at least 1 BC. An important pattern, the BC is a bit tricky to “get” and to explain, but I assure you that you &lt;em&gt;need&lt;/em&gt; it for proper DDD and it simplifies things on the long run.&lt;/p&gt;

&lt;h1 id=&quot;what-is-a-bounded-context&quot;&gt;What is a Bounded Context?&lt;/h1&gt;

&lt;p&gt;What if I told you that you’ve been using the bounded context principle for a while, without knowing it? You know those UI, Business and Database/Persistence layers? Those are literally bounded contexts, an implementation of the &lt;strong&gt;separation of concerns&lt;/strong&gt; principle. A complex Domain can be considered a bunch of functionalities, but we like to group them into ‘specialized’ modules, where each module is responsible for a high level concern.&lt;/p&gt;

&lt;p&gt;If the Domain would be a company, the BCs would be its departments: accounting, HR, marketing, production etc. Each with its own responsibility, different but working together. And since DDD is about being &lt;strong&gt;Domain&lt;/strong&gt; driven, we want to design our application similarly to how the real domain(business) is organized.&lt;/p&gt;

&lt;h1 id=&quot;why-do-we-need-a-bounded-context&quot;&gt;Why do we need a Bounded Context?&lt;/h1&gt;

&lt;p&gt;It’s more than just mimicking the business’ structure. The keyword here is &lt;strong&gt;bounded&lt;/strong&gt;, that is, each BC has boundaries, limits that isolate the model from other BCs(if there are more than one). Actually, the boundaries &lt;em&gt;are&lt;/em&gt; the main benefit of a BC because they ‘contain’ a specific model, allowing us to focus on it. We can say that the main utility of BC is that it allows &lt;strong&gt;better decoupling&lt;/strong&gt;, which is the most important trait of maintainability. Basically, we group together related models while keeping them separated from other models.&lt;/p&gt;

&lt;p&gt;And like everything else in DDD, we aren’t the ones designing or deciding BCs, we &lt;em&gt;identify&lt;/em&gt; them. The business already knows what responsibilities and limits each department has, we’re just putting that information together.&lt;/p&gt;

&lt;h1 id=&quot;how-we-identify-a-bc&quot;&gt;How we identify a BC?&lt;/h1&gt;

&lt;p&gt;Well, we pay attention to the language. The ubiquitous language is not just a DDD buzzword. It means that the business language has the same meaning in a certain context. &lt;code class=&quot;highlighter-rouge&quot;&gt;Invoice&lt;/code&gt; means something to Accounting, but it can be just the name of a class for a programmer. Just because the name is identical doesn’t mean it’s the same concept. And Marketing can say &lt;code class=&quot;highlighter-rouge&quot;&gt;Customer&lt;/code&gt; and Accounting can say &lt;code class=&quot;highlighter-rouge&quot;&gt;Payer&lt;/code&gt; but they can both mean the same thing for the business as a whole.&lt;/p&gt;

&lt;p&gt;The important part here is that names are by themselves irrelevant. We need to understand the &lt;em&gt;concept&lt;/em&gt;’s definition that makes sense in that context in order to place a business case in a certain BC. Many times is quite easy, &lt;code class=&quot;highlighter-rouge&quot;&gt;Create an order&lt;/code&gt; is part of &lt;code class=&quot;highlighter-rouge&quot;&gt;ECommerce&lt;/code&gt; but sending the order’s confirmation email is clearly part of a different BC, something like &lt;code class=&quot;highlighter-rouge&quot;&gt;Marketing&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;Sales&lt;/code&gt;. &lt;em&gt;How&lt;/em&gt; you send the email it’s an implementation/infrastructure detail and we’re not interested in that (at this point).&lt;/p&gt;

&lt;h1 id=&quot;how-do-the-bcs-communicate&quot;&gt;How do the BCs communicate?&lt;/h1&gt;

&lt;p&gt;Since the model of a BC makes sense only inside that BC we can’t just use (refer) other BC’s model directly. But in many cases we need information available in another BC. How can we get it, without breaking the boundaries?&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;Domain Events&lt;/strong&gt;. They represent changes of the global business state, but they do have another, very useful technical trait: they are messages, simple data structures. They are no longer a &lt;em&gt;model&lt;/em&gt; , they are simply information and they don’t belong to a BC, they are domain wide valid.&lt;/p&gt;

&lt;p&gt;If one BC is interested in data from another BC, it will just subscribe to the relevant events and then it will use that data to maintain its very own &lt;strong&gt;read model&lt;/strong&gt; (yes, that’s CQRS in action). And since messages are data and not part of a model, boundaries are not violated. All this should be part of the BC’s own infrastructure.&lt;/p&gt;

&lt;p&gt;But what about invoking a business case which is part of a different BC? When you need that, it means you’re dealing with a business process, a sequence of business cases, however, even if a business case belongs to a BC, the process itself doesn’t and we’re going to have a &lt;strong&gt;process manager&lt;/strong&gt; a.k.a saga in charge, which is a technical implementation detail. When we identify a process, we care about which business cases are part of it, the manager itself doesn’t exist as part of the domain.&lt;/p&gt;

&lt;p&gt;A business case shouldn’t invoke directly the next business case i.e it shouldn’t act as a part-time project manager; in order to achieve decoupling and to maintain boundaries, business cases don’t know anything about other cases, it’s the process manager’s job to know and to invoke the next case.&lt;/p&gt;

&lt;h2 id=&quot;in-special-situations&quot;&gt;In special situations…&lt;/h2&gt;

&lt;p&gt;..you might want to couple one BC to another, because it’s the easiest way. It can be a valid, good enough solution, but you have to be careful. The trick here is to find the right compromise between decoupling and easy implementation.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;Bounded Context&lt;/strong&gt; concept is very important for the maintainability of the app. It allows us to deal with relevant models that don’t overlap. It’s important to note that this is a logical grouping criteria and we can implement everything inside a monolith. But if the app needs to be scalable and we go distributed, then we may want to implement one BC per app/process/server.&lt;/p&gt;

</description>
        <pubDate>Fri, 12 Aug 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/08/12/DDD-Bounded-Contexts-Explained</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/08/12/DDD-Bounded-Contexts-Explained</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Entities and Value Objects Explained</title>
        <description>&lt;p&gt;One of the staples of DDD &lt;em&gt;mindset&lt;/em&gt; is the partitioning of &lt;strong&gt;business concepts&lt;/strong&gt; into: &lt;em&gt;Entities&lt;/em&gt; and &lt;em&gt;Value Objects&lt;/em&gt; . Notice, that I’ve said &lt;em&gt;concepts&lt;/em&gt; not objects. The difference is that an object &lt;em&gt;might be&lt;/em&gt; an implementation of a model of a concept. It’s not about how we write code, it’s about how we ‘label’ a business aspect, in a more developer friendly manner.&lt;/p&gt;

&lt;h1 id=&quot;why-do-we-need-these-tactical-patterns&quot;&gt;Why do we need these tactical patterns?&lt;/h1&gt;

&lt;p&gt;One practical advantage of DDD is that it tells us how to organize domain information in a way that we can implement it easier. Once we know we’re dealing with an &lt;em&gt;Entity&lt;/em&gt; or a &lt;em&gt;Value Object&lt;/em&gt;, we get some hints about how to continue modelling or how an implementation looks like.&lt;/p&gt;

&lt;h1 id=&quot;entities-are-concepts-whose-instances-are-uniquely-identifiable&quot;&gt;Entities are concepts whose instances are uniquely identifiable&lt;/h1&gt;

&lt;p&gt;Actually, it’s a bit more: yes, they all have one &lt;em&gt;immutable, read-only&lt;/em&gt; aspect or detail (call it however you wish) that acts as an identifier, but the thing with an entity is that we can change everything related to it (except its id) and it remains the same instance. It sounds a bit abstract and dry, but things are usually simple. It’s quite easy to identify a concept that is an entity: the domain expert will refer to it as a single, identifiable item. &lt;em&gt;Customer, Invoice, Order, Article, Post&lt;/em&gt; etc are examples of common concepts which are entities. As long as the business care about tracking each instance of that concept, we’re dealing with an Entity.&lt;/p&gt;

&lt;p&gt;In many cases, you get the hint directly: the domain expert will mention a &lt;em&gt;unique number&lt;/em&gt; or &lt;em&gt;unique id&lt;/em&gt; or &lt;em&gt;serial number&lt;/em&gt; etc as being part of that concept definition. Even more, when we identify models for business cases which change the instance of that concept, we keep seeing the identification detail mentioned every time.&lt;/p&gt;

&lt;h2 id=&quot;what-being-an-entity-tells-us&quot;&gt;What being an Entity tells us&lt;/h2&gt;

&lt;p&gt;The most important hint is that an entity designates a ‘bigger’ concept, therefor we’re dealing with an &lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-1&quot;&gt;aggregate&lt;/a&gt; and we need to ‘dig’ deeper in order to find out its &lt;em&gt;composition and its business rules&lt;/em&gt;. 
Another thing is an entity can’t part of an aggregate of another entity. But we can have &lt;em&gt;references&lt;/em&gt; (not the best word) that is, an aggregate can ‘point’ to another entity. Maybe a better way of saying it is that when we have a &lt;strong&gt;domain relationship&lt;/strong&gt; between 2 entities and one ‘needs’ the other one, the expression of it is a reference a.k.a the id of the other entity.&lt;/p&gt;

&lt;p&gt;An example is an Order is always placed by a Customer, so the Order entity has a relationship with the Customer concept, in the sense that we need to represent the Customer as being part of the Order (Create) aggregate. But since Customer is an Entity, only its id will be part of the Order aggregate.&lt;/p&gt;

&lt;h1 id=&quot;value-objects-are-simple-or-composite-values-that-have-a-business-meaning&quot;&gt;Value objects are simple or composite values that have a business meaning&lt;/h1&gt;

&lt;p&gt;A reminder that early DDD was mixed with OOP, a better name for the &lt;em&gt;Value Object(VO)&lt;/em&gt; would be a Value Concept. But the name stuck so we keep saying Value Objects even if its implementation is not an object. VOs usually are not treated with the same respect as the Entities, however they are equally important. It’s not that hard to identify a VO, it’s a concept that doesn’t have an explicit identity detail. Actually, we can say that the identity of a VO is represented by all the values that VO has. Change something and it becomes another value.&lt;/p&gt;

&lt;p&gt;VO not only have values, but they incorporate business constraints that the values needs to respect. Basically, a VO is a ‘smaller’ concept, which is part of a (big concept) Entity.  Most VO are small enough, 1-2 values but we can have bigger VO which combine other VOs.&lt;/p&gt;

&lt;p&gt;Let’s take an example: A &lt;strong&gt;Price&lt;/strong&gt; is a concept which is represented by 2 values: Amount and Currency (itself a VO). It doesn’t have an explicit identity and most importantly, the business doesn’t differentiate between one $5 and another $5.&lt;/p&gt;

&lt;p&gt;Another example is an Order’s ordered product, a detail of the Order Entity. And no, I haven’t seen any business talking about ‘OrderLines’, but they talk about an order’s &lt;em&gt;Items&lt;/em&gt;. The VO &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderItem&lt;/code&gt; is a composite of 3 things: &lt;em&gt;Product&lt;/em&gt;, &lt;em&gt;Quantity&lt;/em&gt; and &lt;em&gt;Price&lt;/em&gt;. As a concept, &lt;code class=&quot;highlighter-rouge&quot;&gt;Product&lt;/code&gt; is an entity and a VO can’t contain Entities so what we’ll have is a &lt;em&gt;ProductId&lt;/em&gt; (or SKU) which itself is a VO. And it might seem that &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderItem&lt;/code&gt; has an identification detail, the &lt;em&gt;product&lt;/em&gt;, used to identify a specific item from the order and making the item look like an entity, but the important thing is that &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderItem&lt;/code&gt; is a component of the Order, a minor concept which isn’t used directly outside the Order. A business can’t identify an &lt;code class=&quot;highlighter-rouge&quot;&gt;OrderItem&lt;/code&gt; only by its ‘id’, it always needs the &lt;code class=&quot;highlighter-rouge&quot;&gt;Order&lt;/code&gt; itself to act as a context for that ‘id’. That’s what makes it a VO, a true entity doesn’t need another entity to be identifiable.&lt;/p&gt;

&lt;p&gt;Sometimes is tricky to identify the nature of a concept, it might look like an entity, however if it represents a minor concept which make sense only as part of another Entity, then we’re dealing with a VO.&lt;/p&gt;

&lt;h2 id=&quot;what-a-vo-tells-us&quot;&gt;What a VO tells us&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Business Constraints - a VO communicates business semantics and usually this means those values need to respect business rules. A VO is &lt;strong&gt;always&lt;/strong&gt; valid.&lt;/li&gt;
  &lt;li&gt;Immutability - due to its nature, a VO never changes. It can’t change or else it becomes another value. So, set its value(s) once and that’s it.&lt;/li&gt;
  &lt;li&gt;Usually a component of an aggregate - Business functionality deals with Entities, it doesn’t care about a stand-alone VO instance. Ideally, all components of an aggregate are VOs.&lt;/li&gt;
  &lt;li&gt;Semantics &amp;gt; Structure - &lt;code class=&quot;highlighter-rouge&quot;&gt;Price&lt;/code&gt; has an amount and a currency. &lt;code class=&quot;highlighter-rouge&quot;&gt;Discount&lt;/code&gt; has an amount and a currency. Are they the same concept? No, and the business cares about that. Even if 2 VOs have identical structure and business constraints they aren’t the same and they should be implemented as different things. DRY doesn’t apply here, never reuse VOs!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These hints make it easy for us to implement a VO and personally I go for an object with business rules enforced in the constructor plus getters. We can have methods but those methods should never change the VO, you can only create new VOs.&lt;/p&gt;

&lt;p&gt;In some cases, the VO implementation can be just a simple enum (think OrderStatus: Pending - InProgress - Completed), no need to complicate our life.&lt;/p&gt;

&lt;h1 id=&quot;when-cqrs-is-involved&quot;&gt;When CQRS is involved&lt;/h1&gt;

&lt;p&gt;While the nature of a concept is the same, regardless if it’s a Command or a Query, these patterns make sense only for the Command model. A query model is just some (read-only)data which can span multiple concepts. While we can use some VO for queries, it’s mainly a DRY convenience.&lt;/p&gt;

&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h1&gt;

&lt;p&gt;It helps to organize the business concepts using these tactical patterns, as long as we identify things &lt;em&gt;properly&lt;/em&gt;. The most common mistake I see is devs who point to a data structure full of primitive types and say:”I have this Entity”. Do not confuse the nature of a concept with a possible implementation of one of its command models.&lt;/p&gt;

</description>
        <pubDate>Fri, 29 Jul 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/07/29/DDD-Entities-Value-Objects-Explained</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/07/29/DDD-Entities-Value-Objects-Explained</guid>
      </item>
    
      <item>
        <title>DDD Decoded - Don't Fear Eventual Consistency</title>
        <description>&lt;p&gt;Inevitably when talking DDD, someone asks: “It’s all great with those aggregates, events and stuff, but how do I query things?”. Inevitably, the answer is:”Use CQRS” and inevitably, a new issue appears: “You have to account for Eventual Consistency (EC)”, a necessary evil.&lt;/p&gt;

&lt;p&gt;But I’m going to say this: The only people worried about EC are CRUD programmers.&lt;/p&gt;

&lt;h1 id=&quot;lets-go-back-in-time-100-years&quot;&gt;Let’s go back in time 100 years&lt;/h1&gt;

&lt;p&gt;No computers, no CQRS, no EC. Or maybe…. Neah! John and Mary have a shared account at a local bank and it just happens that they decided to withdraw 100$ the same day. Unknowingly, they interact with different tellers, just a couple of minutes apart. Being an epoch without computers, for every operation needing to check an account’s balance the tellers need to talk to another clerk who’s in charge of maintaining the balances. At the moment &lt;em&gt;T&lt;/em&gt;, the first teller goes and gets the account balance (it’s 100$). Then he starts to fill in the required paperwork, then handles John 100$. In the mean time, at &lt;em&gt;T+2&lt;/em&gt;,the second teller (serving Mary) goes to the back-office clerk and gets the account’s balance. While he’s on the way back, teller #1 goes to tell the clerk that a transaction has occurred and the  account balance needs to be updated. Teller #2 returns to Mary, handles the paperwork and gives her 100$. Then he goes to the clerk and tells him to register the new operation, thus updating the account balance.&lt;/p&gt;

&lt;p&gt;John and Mary withdraw together 200$, from an account which only has 100$. When the clerk calculates the latest account balance, it gets a negative number.
 Dang! We have a problem! Does the bank blame it on CQRS and Eventual Consistency, heads will roll because nobody used a Unit of Work? Nope! The bank says: “Meh… yet another opportunity to make money! Start charging interest on the negative balance and notify the client they owe us 100$(+interest)”. Business as usual.&lt;/p&gt;

&lt;p&gt;As we can see, Eventual Consistency exists without computers or CQRS. Like rain, it’s a normal phenomenon in the real world. And businesses exist in the real world, so for them EC is just normality, even if a domain expert doesn’t know what the words “Eventual Consistency” mean. And being normality, the business has a solution for this problem.&lt;/p&gt;

&lt;p&gt;As programmers, we are &lt;em&gt;trained&lt;/em&gt; , almost indoctrinated to pray to the ACID god. We feel fragile if 2 changes aren’t wrapped in a transaction and EC is like the boogie man, we fear it and try to avoid it. But this is just the CRUD mindset, a one trick pony that doesn’t reflect a moderately complex domain.&lt;/p&gt;

&lt;p&gt;DDD is about changing our mindset from CRUD, data driven to business driven. And the funny thing is, not only that EC is pretty normal but it’s the default,too. Remember that we try to &lt;em&gt;identify aggregates&lt;/em&gt; i.e we’re actively looking for areas, islands that need to be &lt;strong&gt;immediately consistent&lt;/strong&gt; in an ocean of eventual consistency. Those are not in-your-face visible, we have to carefully ‘hunt’ them, in a way proving that only in those cases we need a unit of work.&lt;/p&gt;

&lt;p&gt;And maybe that’s one of the biggest things with DDD, the revelation that a model doesn’t really need to always be immediately consistent, like in the CRUD approach; some parts need it, others don’t. Our objective is to identify which needs what.&lt;/p&gt;

&lt;p&gt;So, don’t fear Eventual Consistency and try to go beyond the limited CRUD mindset. CQRS works great and usually the delay between sync-ing our event store to a read model is measured in milliseconds. In a world without computers, the delay is way bigger and if the business can survive that, it won’t care about a couple of milliseconds.&lt;/p&gt;

</description>
        <pubDate>Sat, 23 Jul 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/07/23/DDD-Eventual-Consistency</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/07/23/DDD-Eventual-Consistency</guid>
      </item>
    
      <item>
        <title>DDD Decoded - The Aggregate and Aggregate Root Explained (Part 3)</title>
        <description>&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-1&quot;&gt;Part 1: Theory&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-2&quot;&gt;Part 2: Modelling Example&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Coding time! I bet you’ve been waiting for this part for some time. Yes, once we have our model, it’s time to write the code. Let’s start with the Value Objects first.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TransferNumber&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Guid&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EntityId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TransferNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Guid&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entityId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//validate format based on domain rules&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;EntityId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entityId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AccountNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IEquatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
     
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;AccountNumber&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;c1&quot;&gt;//validate format based on domain rules&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;null&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;other&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Debit&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Must&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//business rule&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MustNotBeNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//ensure VO is valid            &lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Credit&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;


        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Credit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AccountNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;Must&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//business rule&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MustNotBeNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//ensure VO is valid            &lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Code should be self-explaining, although I bet you’re wondering about 2 things:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Why &lt;code class=&quot;highlighter-rouge&quot;&gt;TransferNumber&lt;/code&gt; gets a Guid?&lt;/li&gt;
  &lt;li&gt;Why there’s no VO implementation for the creation date?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Well, let me start with no. 2. The creation date doesn’t need encapsulation as there are no actual business constraints (in this example). Why complicate the implementation with another class?&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;TransferNumber&lt;/code&gt; acts as a natural id for the &lt;code class=&quot;highlighter-rouge&quot;&gt;Transfer&lt;/code&gt;, however, for technical purposes, I prefer to have a technical id too, hence the Guid. We’ll be using it in situations where we need to specify the entity, but we don’t need a full Value Object, for example in messages.&lt;/p&gt;

&lt;p&gt;And speaking of messages, here’s our domain &lt;strong&gt;change&lt;/strong&gt; , expressed as a Domain Event&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TransferedRegistered&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Guid&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EntityId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TransferNumber&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;decimal&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Amount&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DebitAccountNo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CreditAccountNo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;DateTimeOffset&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CreatedOn&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DateTimeOffset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A nice, flattened data structure, containing &lt;em&gt;valid&lt;/em&gt; data. Valid, because it’s the Aggregate Root (AR) who’s in charge of its generation. Now, I’m going to use a more “exotic” approach for the AR’s implementation.&lt;/p&gt;

&lt;div class=&quot;language-csharp highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
 &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Transfer&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TransferedRegistered&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TransferNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Debit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Credit&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MustNotBeNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MustNotBeNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;credit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MustNotBeNull&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

            &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;MustNotBe&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

            &lt;span class=&quot;kt&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;TransferedRegistered&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EntityId&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EntityId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TransferNumber&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Amount&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;DebitAccountNo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;debit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;CreditAccountNo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;credit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Number&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Wait… what?! Static class??? Static function???!!! Say what?! Yeah, it’s called a functional approach. I prefer a hybrid OOP-FP style to code DDD models, especially ARs. Why? Because it’s the simplest implementation. Remember that AR is a &lt;em&gt;role&lt;/em&gt; , and the function is the implementation. It makes little sense to make it a full class, when a function is sufficient.&lt;/p&gt;

&lt;p&gt;Now, sure, I may have another command case involving &lt;code class=&quot;highlighter-rouge&quot;&gt;Transfer&lt;/code&gt; but you see, each model is relevant to one case only and I want my code to reflect that. Basically,I want some boundaries between cases, so the class &lt;code class=&quot;highlighter-rouge&quot;&gt;Transfer&lt;/code&gt; is the concept and each static method is an AR enforcing a specific model. Things being static it means they &lt;strong&gt;shouldn’t share&lt;/strong&gt; any state. They’re grouped together only as a &lt;em&gt;convenience&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Our AR makes sure the business rules are respected and then it communicates the change by generating and returning a domain event.&lt;/p&gt;

&lt;p&gt;This is not the only way to implement our model, but I like it since it keeps things simple and our code is Event Sourcing friendly. In a future post, when we’ll talk about Application Services, we’re going to see how the AR is used by the service and what to do with the event.&lt;/p&gt;

</description>
        <pubDate>Thu, 14 Jul 2016 00:00:00 +0000</pubDate>
        <link>http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-3</link>
        <guid isPermaLink="true">http://blog.sapiensworks.com/post/2016/07/14/DDD-Aggregate-Decoded-3</guid>
      </item>
    
  </channel>
</rss>