title: Who is responsible for entity's mutation when domain event is raised? DDD
body: <p>I've been learning about <strong>CQRS</strong>/<strong>ES</strong>. Looking at small example projects I often see <em>events mutating the entity state</em>. For instance If we look at the <code>Order</code> <em>aggregate root</em>:</p>
  
  <pre><code>public class Order : AggregateRoot {
      private void Apply(OrderLineAddedEvent @event) {
          var existingLine = this.OrderLines.FirstOrDefault(
              i =&gt; 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&lt;OrderLine&gt; OrderLines { get; private set; }
  
      public void AddOrderLine(/*parameters*/) {
          this.Apply(new OrderLineAddedEvent(/*parameters*/));
      }
  
      public Order() {
          this.OrderLines = new List&lt;OrderLine&gt;();
      }
  
      public Order(IEnumerable&lt;IEvent&gt; history) {
          foreach(IEvent @event in history) {
              this.ApplyChange(@event, false);
          }
      }
  }
  
  public abstract class AggregateRoot  {
      public Queue&lt;IEvent&gt; 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);
      }
  }
  </code></pre>
  
  <p>when <code>OrderLineAddedEvent</code> is applied it mutates <code>Order</code> by adding new order line. But I don't understand these things:</p>
  
  <ul>
  <li>if it is a right approach how then the changes made are persisted? </li>
  <li>Or should I publish the event somehow to a corresponding handler from <code>Order</code>? How do I implement this technically? Should I use a service bus to transmit events?</li>
  </ul>