Blog

Domain-Driven Design and Command-Query Separation example application

03 Dec, 2009

Ever since attending Greg Young’s Unshackle Your Domain talk at QCon ’08 in San Francisco and a later two-day training course given by Greg Young I’ve wanted to build a sample application that made use of the principles of Command-Query Responsibility Separation (CQRS).
However, other interesting things intervened and I never got around to doing this.
But every few months we have a one day internal training course at Xebia Software Development and after Sjors Grijpink and I proposed to give a training on DDD and CQRS we got some time to actually prepare and implement a CQRS example application.

The Xebia Lottery
The application is fairly simple (as it should be for a one day training course) but touches on all the major components needed for a full-blown CQRS implementation.
CQRS components
The source code for the training exercises can be found at github. There are three branches:

  1. exercise – the code used for doing the training exercises.
  2. solution – the same code as above but with the solutions added.
  3. master – the main development branch, currently used to implement more advanced features like snapshotters, command-event conflict resolution, historical tracing, etc.

The Xebia ITR CQRS presentation is also available.
All-in-all the training was a success and I think that the basic CQRS principles are the way forward when you need to build enterprise applications with a complicated domain logic or strong auditing or historical tracing requirements.

guest
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Silvester van der Bijl
12 years ago

Hi Erik,
Great post! Just took a quick tour of the GitHub project and it seems you put quite a lot of effort into this. Is the framework implementation also going to be released as part of some (open source) project?
Thanks again!
Silvester

Mark Nijhof
12 years ago

Hi Eric,
Very cool, it is good to get some more examples (leave it to the Dutch guys) out there especially using different implementations. I assume you saw my example and blog post on the same topic as well: http://elegantcode.com/2009/11/11/cqrs-la-greg-young/ (it is for the .Net world). I am intending to make it more into a framework where you should be able to use it as NHibernate (Hibernate for you guys 🙂 and have normal po(c|j)o’s.
-Mark

David Perfors
12 years ago

Hi Erik,
This looks great. I took a quick look and found the OnEvent method on the aggregate roots. I think that when you have a lot of different type of events that will be a massive method. I must admit that I don’t know another solution in Java, I am not doing that much with it any more :P…
David

Mark Nijhof
12 years ago

Hi Erik (sorry about the use of c),
It is absolutely a good idea to have different implementations to slve the same problem, then ‘hopefully’ the best one will survive. In my example I am not using an ORM just something using reflection of DTO’s on the reporting side. What I meant when I compared it with NHibernate was how you use NHibernate, so without a base class or any knowledge of persistence.
-Mark

trackback

[…] Domain-Driven Design and Command-Query Separation example application by Erik Rozendaal (in Java) […]

Colin
Colin
12 years ago

Eric,
Example application let me know a lot about CQRS. Thanks.
But, I have a question. All Entity must extend AggregateRoot, how to differentiate Entity and AggregateRoot? As you know “Aggregate is: Group of Entities & Value Objects; One entity within the aggregate is the aggregate root”.

colin
colin
12 years ago

Sorry about the use of c.

colin
colin
12 years ago

@Erik
I am glad to hear that.

Jonas Van Poucke
Jonas Van Poucke
12 years ago

Hi,
Have you seen http://www.gridshore.nl/2009/12/21/cqrs-made-easy-with-cqrs4j/
as an Open Source Framework (Apache 2 license)?
Maybe you could help get that one at production quality?
Jonas

Colin
Colin
12 years ago

Erik,
Recently, I was studying CQRS by your lottery codes.
I got a problem:How to deal with concurrency issues? For example, two users purchasing the same lottery at the same time : )

trackback

[…] is Erik Rozendaal who wrote a lottery example to help train his coworkers. Erik’s code is written in […]

jfly
jfly
9 years ago

Hi,Erik,your job is great! I’m not good in English:(, I can’t understand the 90 line in RepositoryImpl.java, why “actual” is ” result.getVersionedId().getVersion() – 1″, not “result.getVersionedId().getVersion() “?

Explore related posts