Command Query Responsibility Segregation (CQRS) is a way of building software systems that separates the part that changes the state from the part that queries the state. Axon Framework is a Java framework that provides implementations of the most important building blocks, e.g. aggregates, command and event buses, as well as repositories to help developers apply the CQRS architectural pattern when building applications, Dadepo Aderemi writes in a series of blog posts explaining CQRS from a conceptual standpoint and exploring what building blocks the Axon Framework can provide by building a small CQRS demo application.
For Aderemi, Software Developer at Trifork in Amsterdam, the most important parts on the command side of CQRS includes:
- Commands, that captures the intent of what needs to happen. In Axon they are POJOs not required to implement any interface.
- Command handlers, acting on the commands sent. In Axon they can be created either by implementing an interface or by an annotation.
- Command bus, that routes commands to their respective command handlers. Axon comes with four implementations of which Aderemi uses a simple synchronous bus for dispatching commands. Another implementation is the Asynchronous bus processing commands asynchronously.
Aderemi notes that concepts from Domain-Driven Design (DDD) often are brought up in CQRS discussions and he believes that a basic understanding of DDD is beneficial. Two important concepts are Aggregate, a logical concept defining a collection of domain objects handled as an atomic and cohesive whole, and Aggregate root which refers to the entity in the aggregate containing the other objects and responsible for ensuring the aggregate is always in a consistent and cohesive state. Axon provides an AbstractAggregateRoot that a root can extend.
On the query side the most important parts include:
- Domain events, representing something that has happened in the past and created by changes in state in the domain, changes initiated by commands and their handlers.
- Event Bus, transporting the events to the query side. Axon comes with a couple of implementations of which Aderemi uses a simple implementation.
- Event Handlers, listens to events and uses the information to maintain a reflection of the state of the application on the query side. In Axon they are defined using an annotation.
Using Event sourcing, instead of storing the current state, all events changing the state are stored and current state is retrieved by applying all previous events to an initial state. Axon provides several implementations of an EventStore interface including both NoSQL and relational databases. The later versions of Aderemi’s demo application is implementing an event sourcing strategy persisting events to file.
Axon provides a testing infrastructure with an approach following the ideas of Behaviour-Driven Development (BDD) and Aderemi has included tests in his demo application. He notes that the testing infrastructure focuses on events published in reaction to commands sent, thus avoiding a dependency on the API implementation.
Aderemi has published demo application, exploringCQRSwithAxon, on GitHub. He is also preparing a concluding post with his view on CQRS as an architecture and how Axon can help in fulfilling this architecture.
Axon Framework, founded by Allard Buijze also working for Trifork, is an open source product with version 3 planned for Q1 2016.