-
Notifications
You must be signed in to change notification settings - Fork 4
Creating a Model
Creating Models
In order for SeekU to work, your models must derive from SeekU.Domain.AggregateRoot. Once you've done that, the rest is handled for you.
A simple example:
public class TicketVendor : AggregateRoot
{
private int _numberOfAvailableTickets = 0;
public TicketVendor(){}
public TicketVendor(Guid id, int availableTickets)
{
ApplyEvent(new VendorCreatedEvent(id, availableTickets);
}
public void Apply(VendorCreatedEvent evt)
{
Id = evt.Id;
_numberOfAvailableTickets = evt.AvailableTickets;
}
}
A number of things are happening here.
First, there is an empty, public constructor. This is necessary because SeekU needs to be able to create an instance of your class in order to replay an even history.
Next, the other constructor has 2 arguments which, in this case, allows you to create your own instance of a TicketVendor.
Inside the constructor there's a call to the base class' ApplyEvent method. Notice that the vendor's ID and ticket count aren't set in the constructor. This is a fundamental concept to CQRS. If those values were set in the constructor, then it would be impossible to arrive back at that state in the future. Remember, when events are replayed it's via the empty public constructor.
Instead, the constructor fires an event. The event is immediately handled by the Apply method. There's a simple convention for handing the internal event. Simply create a method named Apply with a single argument that is the type of event being fired.
In other words
ApplyEvent(new MyEventFired());
Invokes the method
Apply(MyEventFired argumentName){ ... }
The argument name isn't important; the method name and argument type are.
That's all there is to creating your aggregates.