title: How to apply the EventBus of CQRS with Akka.net and Akka.Persistence
body: <p>I'm interested how to apply CQRS and Event Sourcing with Akka.net. I already noticed <a href="http://getakka.net/docs/Persistence" rel="nofollow noreferrer" title="Akka.Persistence">Akka.Persistence</a> which delivers the ES part.</p>
  
  <p>As far as I understand the Command Handlers and AggergateRoot can be represented by the ReceivePersistentActor in a single class. I have a base class for that (not complete here)...</p>
  
  <pre><code>public abstract class AggregateRoot&lt;TState&gt; : ReceivePersistentActor
  {
      private readonly Func&lt;TState, bool&gt; shouldSafeSnapShot;
  
      protected AggregateRoot(TState state, Func&lt;TState, bool&gt; shouldSafeSnapShot)
      {
          if (state == null) throw new ArgumentNullException(nameof(state));
          if (shouldSafeSnapShot == null) throw new ArgumentNullException(nameof(shouldSafeSnapShot));
  
          var path = this.Self.Path;
          this.PersistenceId = $"{path.Parent.Name}/{path.Name}";
          this.State = state;
          this.shouldSafeSnapShot = shouldSafeSnapShot;
      }
  
      public sealed override string PersistenceId { get; }
  
      protected TState State { get; }
  
      protected void Emit&lt;TEvent&gt;(TEvent e, Action&lt;TEvent&gt; apply = null)
      {
          this.Persist(e,
              @event =&gt;
                  {
                      // update state
                      apply?.Invoke(@event);
  
                      // safe snapshot or so...
                      this.SaveSnapshotIfRequired();
  
                      // publish to event bus?
                      Context.System.EventStream.Publish(e);
                  });
      }
  
      private void SaveSnapshotIfRequired()
      {
          var state = this.State;
  
          if (this.shouldSafeSnapShot(state))
          {
              this.SaveSnapshot(state);
          }
      }
  }
  </code></pre>
  
  <p>Not I want to send the event over the EventBus and Akka seems to bring something within it's package that might fit to the way it is done with other CQRS frameworks (EventBus abstractions in <a href="https://github.com/pjvds/ncqrs/blob/master/Framework/src/Ncqrs/Eventing/ServiceModel/Bus/IEventBus.cs" rel="nofollow noreferrer">NCqrs</a>, <a href="https://github.com/Inceptum/Cqrs/blob/1.0.8/src/Inceptum.Cqrs/IEventPublisher.cs" rel="nofollow noreferrer">Inceptum</a>)  or in the simple sample of Gregory Young <a href="https://github.com/gregoryyoung/m-r/blob/master/SimpleCQRS/FakeBus.cs" rel="nofollow noreferrer">here</a>.</p>
  
  <pre><code>// publish to event bus?
  Context.System.EventStream.Publish(e);
  </code></pre>
  
  <p>However, it seems weird to me to know the implementation of the eventbus within the AggregateRoot domain object actor. I could put the responsibility to an actor child or inject something like an <em>IAbstractEventBus</em> interface that is used to switch implementations but I don't think it should be the responsibility of the AggregateRoot to publish the event after persisting and inform all subscribers, right!?
  But the ES part seems to be coupled to the Actor.</p>
  
  <p>What is the typical way to do that with Akka.net and Akka.Persistence? Any design ideas how to split this up?</p>
  
  <p>I stumbled up the <em>IEventAdapter</em> interface of Akka.net but I'm not sure this brings me to the right path (or dark side)...</p>
  
  <pre><code>public class SomeEventAdapter : IEventAdapter
  {
      public object ToJournal(object evt)
      {
          return evt; // should I add the event emitter here?
          // but is this already stored now? hmmm
      }
  }
  </code></pre>