-
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 NEventStore 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.
An example project complete with all DDD layers is located here https://github.com/volak/DDD.Enterprise.Example. For Aggregates.NET specific code check out the Inventory domain project 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 void Handle(Commands.Create command)
{
var item = _uow.Repository<Item>().Get(command.ItemId);
var serial = item.Entity<SerialNumber>().New(command.SerialNumberId);
serial.Create(command.SerialNumber, command.Quantity, command.Effective, command.ItemId);
_bus.Reply<IdResult>(e =>
{
e.Id = serial.Id;
});
}
In my example domain, Item is an aggregate root with an entity SerialNumber. Lets go through this line by line:
####var item = _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 = 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.
Finally, I reply with the created serial number's Id. This is purely a demonstration thing, some find it weird to reply to a command at all and its completely up to your project requirements.
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.