¿Quién es responsable de la mutación de la entidad cuando se genera un evento de dominio? DDD
He estado aprendiendo sobreCQRS/ES. Mirando pequeños ejemplos de proyectos que a menudo veoeventos que mutan el estado de la entidad. Por ejemplo, si miramos elOrder
raíz agregada:
public class Order : AggregateRoot {
private void Apply(OrderLineAddedEvent @event) {
var existingLine = this.OrderLines.FirstOrDefault(
i => i.ProductId == @event.ProductId);
if(existingLine != null) {
existingLine.AddToQuantity(@event.Quantity);
return;
}
this.OrderLines.Add(new OrderLine(@event.ProductId, @event.ProductTitle, @event.PricePerUnit, @event.Quantity));
}
public ICollection<OrderLine> OrderLines { get; private set; }
public void AddOrderLine(/*parameters*/) {
this.Apply(new OrderLineAddedEvent(/*parameters*/));
}
public Order() {
this.OrderLines = new List<OrderLine>();
}
public Order(IEnumerable<IEvent> history) {
foreach(IEvent @event in history) {
this.ApplyChange(@event, false);
}
}
}
public abstract class AggregateRoot {
public Queue<IEvent> UncommittedEvents { get; protected set; }
protected abstract void Apply(IEvent @event);
public void CommitEvents() {
this.UncommittedEvents.Clear();
}
protected void ApplyChange(IEvent @event, Boolean isNew) {
Apply(@event);
if(isNew) this.UncommittedEvents.Enqueue(@event);
}
}
cuandoOrderLineAddedEvent
se aplica mutaOrder
agregando una nueva línea de pedido. Pero no entiendo estas cosas:
Order
? ¿Cómo implemento esto técnicamente? ¿Debo usar un bus de servicio para transmitir eventos?