-
Notifications
You must be signed in to change notification settings - Fork 39
Aggregate roots
An aggregate root is an important kind of object that can work independently of other objects. It may contain other objects, and those contained objects are owned by the root. The root and everything it contains is called an aggregate.
All interaction with the aggregate goes through the aggregate root, thus allowing the root to function as a facade to whatever is going on beneath it, providing a nice encapsulated way of modeling things.
This way, an aggregate can & must function as a consistency barrier, making sure that its integrity is sustained i.e. its invariants are preserved.
Creating an aggregate root with Cirqus is easy - you just create a class that inherits from AggregateRoot
, like so:
public class MyRoot : AggregateRoot
{
}
The AggregateRoot
base class will bring a couple of useful things with it:
- An
Id
property - since all aggregate roots must be uniquely addressable, they will have a globally unique ID. This is corroborated by the fact that theId
property is aGuid
. - An
Emit
method - the root must only change itself by using the Emit Apply Pattern, and to do that the root must use theEmit
method.
and then, internally, it keeps track of some stuff regarding events and sequence numbers.
Creating an instance of an aggregate root is easy: Send a command to it!
In the Execute
method of a command, when you load an aggregate root instance that does not already exist (i.e. has had events emitted with its aggregate root ID), you will get a freshly created aggregate root instance.
In other words: You create a new instance by coming up with a Guid.NewGuid()
and start addressing the root with commands.