title: How to handle domain events that are raised by event handlers?
body: <p>I've implemented the following pattern by jbogard:</p>
  
  <p><a href="http://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern/">http://lostechies.com/jimmybogard/2014/05/13/a-better-domain-events-pattern/</a></p>
  
  <p>Imagine the following entity <code>Coupon</code> and event <code>CouponActivatedEvent</code>:</p>
  
  <pre><code>public class Coupon : DomainEntity
  {
      public virtual User User { get; private set; }
  
      // ...omitted...
  
      public void Activate(User user)
      {
          if (User != null)
              throw new InvalidOperationException("Coupon already activated");
  
          User = user;
  
          Events.Add(new CouponActivatedEvent(this));
      }
  }
  </code></pre>
  
  <p>The following event handler <code>CouponActivatedHandler</code>:</p>
  
  <pre><code>public class CouponActivatedHandler : IDomainEventHandler&lt;CouponActivatedEvent&gt;
  {
      public void Handle(CouponActivatedEvent e)
      {
          // user gets 5 credits because coupon was activated
          for (int i = 0; i &lt; 5; i++)
          {
              e.Coupon.User.AddCredit(); // raises UserReceivedCreditEvent and CreditCreatedEvent
          }
      }
  }
  </code></pre>
  
  <p>The following <code>SaveChanges</code> override on <code>DbContext</code> (Entity Framework 6), taken from jbogard's blog post:</p>
  
  <pre><code>public override int SaveChanges()
  {
      var domainEventEntities = ChangeTracker.Entries&lt;IDomainEntity&gt;()
          .Select(po =&gt; po.Entity)
          .Where(po =&gt; po.Events.Any())
          .ToArray();
  
      foreach (var entity in domainEventEntities)
      {
          var events = entity.Events.ToArray();
          entity.Events.Clear();
          foreach (var domainEvent in events)
          {
              _dispatcher.Dispatch(domainEvent);
          }
      }
  
      return base.SaveChanges();
  }
  </code></pre>
  
  <p>If we now activate a coupon, this will raise the <code>CouponActivatedEvent</code>. When calling <code>SaveChanges</code>, the handler will be executed, and <code>UserReceivedCreditEvent</code> and <code>CreditCreatedEvent</code> will be raised. They won't be handled though. Am I misunderstanding the pattern? Or is the <code>SaveChanges</code> override not appropriate?</p>
  
  <p>I've considered creating a loop that will repeat until no new events are raised before moving on to <code>base.SaveChanges();</code>... but I'm worried that I will create endless loops accidentally. Like so:</p>
  
  <pre><code>public override int SaveChanges()
  {
      do 
      {
          var domainEventEntities = ChangeTracker.Entries&lt;IDomainEntity&gt;()
              .Select(po =&gt; po.Entity)
              .Where(po =&gt; po.Events.Any())
              .ToArray();
  
          foreach (var entity in domainEventEntities)
          {
              var events = entity.Events.ToArray();
              entity.Events.Clear();
              foreach (var domainEvent in events)
              {
                  _dispatcher.Dispatch(domainEvent);
              }
          }
      }
      while (ChangeTracker.Entries&lt;IDomainEntity&gt;().Any(po =&gt; po.Entity.Events.Any()));
  
      return base.SaveChanges();
  }
  </code></pre>