Created
March 13, 2011 17:41
-
-
Save trbngr/868281 to your computer and use it in GitHub Desktop.
Using Entity inside AggregateRoot - NCQRS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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