Blog.

Some notes on the DDD/CQRS Course

Marco Franssen

Marco Franssen /

2 min read306 words

Cover Image for Some notes on the DDD/CQRS Course

In this blog post I work out some of the notes I made in Greg Young’s DDD/CQRS course in Krakow Poland.

In Domain Driven Design there are some important things to think about. In DDD we make a difference in the following components.

  • Aggregate Roots
  • Entities
  • Value Objects

An Aggregate root is a set of multiple things that belong to each other. The aggregate should have a name that describes the whole.

To come to a domain driven design you should take the following steps:

  1. Denormalization
  2. Transactions –> try to avoid
  3. Domain service consistency –> try to avoid
  4. Use soft links to learn it the right way

When applying the above you should mention the bullets below when applying them:

  • Remove the bidirectional relations
  • Define your aggregates
  • Make sure operations only affect one aggregate at a time
  • Put methods where the state is encapsulated / where the data is changed (objects should change their own state)
  • A command may only mutate state and has always a void return value
  • A Query may only expose state and can’t be a void return value
  • Avoid distributed transactions to have good scalability

Even without transactions you can provide consistency in your systems. We can do this by merging. You can do this with the following code-snippet. Notice the recursive call in the catch clause.

public class MergingHandler<T> : Consumes<T>
{
    public MergingHandler(Consumes<T> next)
    {
    }

    public void Consume(T message)
    {
        try
        {
            var commit = eventStore.GetEventsSinceVersion(message.AggregateId, message.ExpectedVersion);
            next.Handle(message);

            foreach(var e in commit)
            {
                foreach (vat attempted in UnitOfWork.Current.PeekAll())
                {
                    if (ConflictsWith(attempted, e)) {
                        throw new RealConcurrencyException();
                    }
                }
            }
            UnitOfWork.Current.Commit();
        }
        catch(ConcurrencyException e) {
            this.Consume(message);
        }
    }
}
You have disabled cookies. To leave me a comment please allow cookies at functionality level.

More Stories

Cover Image for Lessons learned when applying DDD CQRS

Lessons learned when applying DDD CQRS

Marco Franssen

Marco Franssen /

While I was trying to apply DDD in combination with CQRS I learned myself some good lessons. I was trying to apply this in an agile approach, using the project method SCRUM. I started the project by translating all use-cases to smaller user stories. Based on this user stories I defined a first sprint. In the first sprint I didn't had or make a design for the complete domain. So I wasn't completely sure if I made the right decision for the 'aggregate root / bounded contexts' needed for the firs…

Cover Image for Injecting dependencies in MVC3 with Windsor

Injecting dependencies in MVC3 with Windsor

Marco Franssen

Marco Franssen /

In this blog post I will explain how to inject your dependencies into your controllers in a MVC 3 application with Castle Windsor. First of all you need to create a WindsorInstaller. This class is responsible for the configuration of your IoC container. In the above class the first line registers all your controllers with lifestyle configured to Transient. Depending on the lifestyle you choose Windsor will clean all resources. Other lifestyles are Singleton, Pooled, PerWebRequest, PerThread.…