Skip to content

Instantly share code, notes, and snippets.

@trbngr
Created March 13, 2011 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trbngr/868281 to your computer and use it in GitHub Desktop.
Save trbngr/868281 to your computer and use it in GitHub Desktop.
Using Entity inside AggregateRoot - NCQRS
public class Order : AggregateRoot
{
private readonly List<OrderItem> orderItems = new List<OrderItem>();
public void UpdateOrderItem(Guid orderItemId, int quantity)
{
var orderItem = orderItems.Where(q => q.EntityId ==
orderItemId).FirstOrDefault();
if (orderItem == null)
throw new Exception();
orderItem.UpdateQuanity(quantity);
}
}
class OrderItem : EntityMappedByConvention<Order>
{
private int _quantity;
private DateTime modifiedDate;
public OrderItem(Order parent, Guid entityId) : base(parent, entityId)
{
}
public void UpdateQuanity(int quantity)
{
//VALIDATE
var e = new OrderItemUpdatedEvent(quantity, DateTime.UtcNow);
ApplyEvent(e);
}
protected void OnOrderItemUpdated(OrderItemUpdatedEvent e)
{
_quantity = e.Quantity;
modifiedDate = e.ModifiedDate;
}
}
internal class OrderItemUpdatedEvent : SourcedEntityEvent
{
public int Quantity { get; set; }
public DateTime ModifiedDate { get; set; }
public OrderItemUpdatedEvent(int quantity, DateTime modifiedDate)
{
Quantity = quantity;
ModifiedDate = modifiedDate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment