-
Notifications
You must be signed in to change notification settings - Fork 59
Home
Thanks for checking out Aggregates.NET - this is a slightly opinionated DDD library meant to help devs connect EventStore and NServicebus with minimal effort and time. It contains definitions for Aggregate Roots, Entities, and Value Objects which you use in your NServicebus message handlers to process domain commands. It also contains a specifications implementation based off of LinqSpecs to help model your business rules and validate roots and entities.
You can find VERY simple example projects under the "samples" folder here or if you want to get more complicated check out my other example project complete with all DDD layers here https://github.com/volak/DDD.Enterprise.Example. For Aggregates.NET specific code check out the domain projects located here.
Aggregates.NET is meant to be used in reaction to a domain command in your system. Lets look at a sample handle method from NServiceBus
public async Task Handle(Commands.Create command, IMessageHandlerContext ctx)
{
var item = await _uow.Repository<Item>().Get(command.ItemId);
var serial = await item.Entity<SerialNumber>().New(command.SerialNumberId);
serial.Create(command.SerialNumber, command.Quantity, command.Effective, command.ItemId);
}
In my example domain, Item is an aggregate root with an entity SerialNumber. Lets go through this line by line:
####var item = await _uow.Repository<Item>().Get(command.ItemId);
This will retrieve the aggregate root Item with id ItemId from the event store. In Aggregates.NET you can use IDs of whatever type you wish. Normally though I would suggest Guid
, String
, or Int
. If one of the last two, you must be sure they are unique to the entire bucket.
Important to note is you can mix id types from aggregate to aggregate and even among entities inside 1 aggregate!
####var serial = await item.Entity<SerialNumber>().New(command.SerialNumberId);
To get or create the entity from the aggregate root, you just need to call .Entity<[EntityType]>
the entity repository will be returned to you which you can use to create a new entity - .New
- or retreive an existing one - .Get
.
Note that entities are NOT loaded when the aggregate is retrieved. Aggregates.NET will look at the entire event stream and only hydrate the aggregate with the events corresponding to its ID. This is different from most other frameworks so be careful.
####serial.Create(command.SerialNumber, command.Quantity ...;
This is a method call on our entity. Inside the entity, if the call is valid an event will be raised to reflect the changed state of the serial entity.
To read more, please see the wiki pages which will explain the other entities in more detail
- If you have found a bug or need help understanding something please create a issue.
- You can also post on Stack Overflow, use the tag CQRS or NServicebus and put "Aggregates.NET" in the title.