Skip to content

Instantly share code, notes, and snippets.

@SzymonPobiega
Created August 7, 2010 11:34
Show Gist options
  • Save SzymonPobiega/512710 to your computer and use it in GitHub Desktop.
Save SzymonPobiega/512710 to your computer and use it in GitHub Desktop.
//Customer is an aggregate root
public class Customer : AggregateRootMappedByConvention
{
//Customer contains orders, each of which is uniquely identified hence is an entity
private readonly List<Order> _orders = new List<Order>();
public void CreateOrder(int id)
{
//Does not create an entity, only publishes event.
//The entity has localy unique id. Its uniqueness is ensured here.
ApplyEvent(new OrderCreatedEvent(id));
}
public void OnOrderCreated(OrderCreatedEvent evnt)
{
//Creates new entity and appends it to the collection. Aggregate root is passed
//as first arguments to Entity's constructor. More on that later...
_orders.Add(new Order(this, evnt.Id));
}
public void CreateOrderLine(int orderId, decimal value)
{
//Does not apply any events on AR level, only passes invocation further, to the Order entity
_orders.First(x => x.Id == orderId).CreateLine(value);
}
}
//An entity living inside aggregate
public class Order : Entity
{
//Entity has a localy unique id.
private readonly int _id;
//an entity can have its own internal structure consisting of value objects (int this case)
//or potentially other entities
private readonly List<OrderLine> _lines = new List<OrderLine>();
public Order(AggregateRoot parent, int id)
: base(parent)
{
//This will be changes when Convention/Attribute based event wire-up will
//be factored out of AR. You will inherit not directly from Entity, but
//from EntityMappedByConvention/Attributes instead.
RegisterHandler(new TypeAndCallbackThresholdedActionBasedDomainEventHandler(
OnOrderLineCreated, x => x is OrderLineCreatedEvent &&((OrderLineCreatedEvent)x).OrderId == _id, typeof(OrderLineCreatedEvent)));
_id = id;
}
public int Id
{
get { return _id; }
}
//A public method on entity looks exactly like method on AR
public void CreateLine(decimal value)
{
ApplyEvent(new OrderLineCreatedEvent(_id, value));
}
//Same for event handler
public void OnOrderLineCreated(SourcedEvent evnt)
{
_lines.Add(new OrderLine(((OrderLineCreatedEvent)evnt).Value));
}
}
public class OrderCreatedEvent : SourcedEvent
{
private readonly int _id;
public OrderCreatedEvent(int id)
{
_id = id;
}
public int Id
{
get { return _id; }
}
}
public class OrderLineCreatedEvent : SourcedEvent
{
private readonly int _orderId;
private readonly decimal _value;
public OrderLineCreatedEvent(int orderId, decimal value)
{
_orderId = orderId;
_value = value;
}
public decimal Value
{
get { return _value; }
}
public int OrderId
{
get { return _orderId; }
}
}
public class OrderLine
{
private readonly decimal _value;
public OrderLine(decimal value)
{
_value = value;
}
public decimal Value
{
get { return _value; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment