Category Archives for DDD

cqrs event sourcing architecture

CQRS + Event Sourcing – Step by Step

A common issue I see is understanding the flow of commands, events and queries within a typical CQRS ES based system. The following post is designed to clear up what happens at each step. Hopefully, this will help you to reason about your code and what each part does.
Continue reading

Are You Making These 10 DDD Mistakes?

Making mistakes is part of programming. Spotting them early can save you time. I’ve started to notice a common set of ‘DDD Mistakes’ which many of us seem to make. And yes, I’ve made them all at some point in my career. This is by no means the definitive list – I’m sure there are more.

1. Allowing Persistence and Databases to Influence your Models

This is a common ‘mistake’ when following a DDD approach. Many of the tactical patterns like, Aggregate Roots, exist to simplify your models.  They achieve simplicity by isolating your solution from infrastructure concerns like databases. The real starting point of a DDD approach is always the domain experts. If you find yourself starting with a schema or data model, alarm bells should be ringing. Your final solution may end up using stored procs over a relational model. But a database should have no part of the early stages of modelling.

2 – Not Immersing Yourself With Domain Experts.

At the heart of Domain Modeling is the desire to bridge the communications gap. The better you understand the problem domain, the better your solution. Now when I say understand the problem I mean from the perspective of the domain expert.  So if you are modeling a circuit board testing system, spend time with the electronics engineers. If it is an aircraft refueling coordination system, spend time with the re-fueling coordinators (or whatever they are called).

3 – Ignoring the Language of the Domain Experts.

A key concept in DDD is something called the Ubiquitous Language. The idea is simple. First understand the language of the experts. Then infuse the language of the domain experts into all your code and discussions. Right down to the method, class and variable names.

Health Warning: The Ubiquitous Language is only ubiquitous within a bounded context. For example, the word ‘Client’ may have a different meaning in the accounts BC to the warehouse BC.

4 – Not Identifying Bounded Contexts

How do you solve a complex problem? A common approach is to break it down into smaller parts. Isolating and solving smaller problems make resolving the bigger ones more likely. This is one of the key benefits of a bounded context. Identifying them has a direct impact on the likelihood of building a successful solution.

5 – Maintaining Bounded Contexts Despite Deeper Domain Insights

When you have a found a context which seems to work, it’s tempting to stick with it. But this rigidity can become a problem. The process of domain discovery evolves over time. Your code should evolve with those discoveries. To do this, your code needs to be supple. You need to cover your code in the right kind of tests. This gives you the freedom to rework your code to reflect those discoveries. It allows you to take advantage of your growing understanding of the domain.

6 – Using Anemic Domain Models

This is a common symptom of a modelling process gone wrong. It is also a sign you are not doing ‘DDD’. But what is an anaemic domain model? It is domain classes full of public properties with getters and setters. They are classes with no behaviour of their own. These have become prevalent due to ORM’s mapping database schema’s to code. You may still have these kinds of classes in your system but they are not your domain objects. Rather look to encapsulate objects and provide behaviour.

7 – Assuming All Logic is Domain Logic

Again this is a common error. The assumption is that all logic is domain logic. You can tie yourself in knots trying to shoehorn everything into your domain models. The most obvious example is field validation on forms. Most field validation logic should live close to the client code. In a web application, it would be in the javascript and endpoint controller. For example, validating an email, or a required first name field. The confusion arises because we attempt to keep all validation logic in one place.  For example, we may check an age is in a particular range. This is field validation. Whereas ensuring a person is old enough to handle hazardous material, this is a domain concern. Other types of ‘logic’ may also not be best suited within a domain object. An example of this is a when an aggregate exists solely to house a process which should involve many aggregates. A better approach could be to use a process manager. This would handle the processing logic. While the aggregate processes the implementation logic.

8 – Over Using Interaction Tests

It is important to keep your code supple. This is particularly true at the early stages. But to allow for major re-factors you need a safety net. This net is a good suite of tests. They should be a help to the development process rather than a trip hazard. I’ve noticed interaction tests have a tendency to hinder re-factors. This is because interaction testing expects certain methods to have been called. It ensures these methods are called with particular parameters etc. Here’s the problem. If we change how the work is done but not the end result, the tests will fail. A much more robust mechanism of testing is by checking the final state. Given a certain scenario, when a specific input is received, a specific final state is expected. Key here is the test doesn’t care how that state was arrived at. This frees you to monkey with the innards and be confident the system still behaves correctly.

9 – Treating Security as Part of the Domain (unless it actually is)

Security is an important part of a lot of the systems we build today. Rarely, however, is it part of the domain. The result of a calculation of risk, doesn’t change if calculated by a superuser or not. So while security is an important part of the system, it shouldn’t play a part in the modelling of the domain. Unless it is actually part of the domain!

10 – Focusing on Infrastructure

I’ve already mentioned the common mistake of focusing on the database at the start. Another mistake is to focus on infrastructure concerns at the modelling stage. An example of this could be taking a data feed from a device. You should define the shape of the data for your system. Then use adapters to ensure whatever service feeds you data, is transformed into the correct format.

If all that wasn’t enough, here is a bonus one…

BONUS: 11 – Skipping the EventStorming Process

EventStorming is often overlooked by developers. It involves a different kind of skill set. It can be hard to get the right people in the room. And there are no guarantees you will come up with something useful. The reason I’ve added it is it is something well worth doing. If you want to speed up the design process. If you want the best chance of spotting those ‘seems’ in the domain as early as possible. If you want to get buy in. If you want involvement. Both crucial elements of a successful project. Then don’t miss out the EventStorm.

Health Warning: It may be DDD is not the right approach for the system you are building. In which case the advice above would not necessarily apply.

Making mistakes is part of programming. Spotting them early can save you time. I’ve started to notice a common set of ‘DDD Mistakes’ which many of us seem to make. And yes, I’ve made them all at some point in my career. This is by no means the definitive list – I’m sure there are more.Continue reading

Download this infographic.

Embed Our Infographic On Your Site!

How To Validate Commands in a CQRS Application

Any of these sound familiar?

Rubbish in rubbish out.

Never trust user input.

If you’ve ever built any sort of professional application you will have had to validate user input. Whether it is to ensure a valid email address or something more complicated. There are tried and tested ‘rules’ for doing this safely while being nice the user.

  1. Validate on the client side – makes for a better user experience
  2. And validated on the server – never trust user input

But here’s a question..

If you issue commands rather than send in models, where should the validation occur?

The most obvious answer is in the domain model.

But this poses a problem…Continue reading

Why Private C# Variables are Not as Private as You Thought

Why C# Private Variables are Not as Private as you Thought

C# private fields are not accessible outside the class. It’s C# 101 right?

Which means this code should not work…

Continue reading

Is a CQRS Command = to a GoF Command?

Oh the irony.

The DDD community advocates for unambiguous language. And yet even our own terms are heavily overloaded. Whether it’s on stackoverflow like here or on the DDD/CQRS google group like here.

So, just to be clear …

… a CQRS command is not the same as the Gang Of Four Command Pattern.

But what are the differences and how best to use them? This is what I’ll cover in this post.
Continue reading

Title Image

How to Send Emails the Right Way in a CQRS System

Where do you put code for sending emails? Sounds simple right? The funny thing is that if you’re adding it to a CQRS system it can be a little tricky.

Why?

It all depends on when you send them. Too early and other processes may fail and you end up sending your email half cocked. And don’t forget event replay. Could be a bit embarrassing re-sending all the emails since your app launched. I was a hairs whisker away from doing just that once!

So, in a CQRS ES system, where do you put the code to send the emails?Continue reading

4 Secrets to Inter-Aggregate Communication in an Event Sourced System

Ok, so you have two event sourced aggregate roots. You need to call a method in one from the other. How do you do it?

If you find your self asking this question, don’t worry, your not alone. It comes up a lot.

The short answer – it suggests there could be something wrong with your aggregate root (AR) boundaries.Continue reading

Aggregate Root - How to Build One for CQRS and Event Sourcing

Aggregate Root – How to Build One for CQRS and Event Sourcing

An aggregate root is at the heart of your domain. I am going to dissect a simple implementation of an Aggregate Root and reveal how it works in a CQRS and Event Sourced system. Before we dive in, we need to fly through some terms often used in DDD.Continue reading

Is a 100% Provable Audit Log Possible? – Hint, with Event Sourcing it Is and I’ll Show You How

If you’ve been developing for any length of time you’ve probably had to create an audit log. Here’s the question though, are any of these logs, a 100% provable audit of all changes in the system? I’m guessing not. With event sourcing, a 100% provable event log just happens to be a handy by product. So how does it work and how can you implement it?

Continue reading

Eventual consistency

4 Ways to Handle Eventual Consistency on the UI

Ever tried to hold a writhing slippery eel in your hands? In case you haven’t, its hard. Building a UI for a CQRS system needn’t be that tricky but you do need to contend with some interesting challenges:

  1. The commands ‘complete’ far faster than you may expect –  compared to a consistent application.
  2. The read model is ‘eventually consistent’.

But what is ‘eventual consistency’? ‘Eventual consistency’ is when a read of the data store may return stale results. That is a command has completed but the data store is not yet reflecting the change. This state in a typical line of business application can last for ten’s to the mid hundreds of milliseconds. The actual time is dependant on the specifics of the application. This presents a challenge for designing the user experience. Here are 4 possible approaches to solving this issue.Continue reading

>