Documentation Coverage Report
Current view: top level - Akka.Net/Cqrs.Akka/Events - AkkaEventBusProxy.cs Hit Total Coverage
Version: 2.2 Artefacts: 7 7 100.0 %
Date: 2018-08-07 15:04:50

          Line data    Source code
       1             : #region Copyright
       2             : // // -----------------------------------------------------------------------
       3             : // // <copyright company="Chinchilla Software Limited">
       4             : // //   Copyright Chinchilla Software Limited. All rights reserved.
       5             : // // </copyright>
       6             : // // -----------------------------------------------------------------------
       7             : #endregion
       8             : 
       9             : using System.Collections.Generic;
      10             : using Akka.Actor;
      11             : using cdmdotnet.Logging;
      12             : using Cqrs.Akka.Domain;
      13             : using Cqrs.Authentication;
      14             : using Cqrs.Configuration;
      15             : using Cqrs.Events;
      16             : 
      17             : namespace Cqrs.Akka.Events
      18             : {
      19             :         /// <summary>
      20             :         /// A <see cref="IEventPublisher{TAuthenticationToken}"/> that proxies <see cref="IEvent{TAuthenticationToken}"/> to the <see cref="IActorRef"/> which acts as a single point of all handler resolutions.
      21             :         /// </summary>
      22             :         public class AkkaEventBusProxy<TAuthenticationToken>
      23             :                 : IAkkaEventPublisherProxy<TAuthenticationToken>
      24           1 :         {
      25             :                 /// <summary>
      26             :                 /// Instantiates a new instance of <see cref="AkkaEventBusProxy{TAuthenticationToken}"/>.
      27             :                 /// </summary>
      28           1 :                 public AkkaEventBusProxy(IDependencyResolver dependencyResolver, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
      29             :                 {
      30             :                         CorrelationIdHelper = correlationIdHelper;
      31             :                         AuthenticationTokenHelper = authenticationTokenHelper;
      32             :                         EventHandlerResolver = ((IAkkaAggregateResolver)dependencyResolver).ResolveActor<BusActor>();
      33             :                 }
      34             : 
      35             :                 /// <summary>
      36             :                 /// Gets the <see cref="IActorRef">event handler resolver</see> that we send/proxy the event to.
      37             :                 /// </summary>
      38             :                 protected IActorRef EventHandlerResolver { get; private set; }
      39             : 
      40             :                 /// <summary>
      41             :                 /// Gets or sets the <see cref="ICorrelationIdHelper"/>.
      42             :                 /// </summary>
      43             :                 protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
      44             : 
      45             :                 /// <summary>
      46             :                 /// Gets or sets the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}">Authentication Token Helper</see>.
      47             :                 /// </summary>
      48             :                 protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
      49             : 
      50             :                 #region Implementation of IEventPublisher<TAuthenticationToken>
      51             : 
      52             :                 /// <summary>
      53             :                 /// Publishes the provided <paramref name="event"/> on the event bus.
      54             :                 /// </summary>
      55           1 :                 public virtual void Publish<TEvent>(TEvent @event)
      56             :                         where TEvent : IEvent<TAuthenticationToken>
      57             :                 {
      58             :                         // We only set these two properties as they are not going to be available across the thread/task
      59             :                         if (@event.AuthenticationToken == null || @event.AuthenticationToken.Equals(default(TAuthenticationToken)))
      60             :                                 @event.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
      61             :                         @event.CorrelationId = CorrelationIdHelper.GetCorrelationId();
      62             : 
      63             :                         bool result = EventHandlerResolver.Ask<bool>(@event).Result;
      64             :                 }
      65             : 
      66             :                 /// <summary>
      67             :                 /// Publishes the provided <paramref name="events"/> on the event bus.
      68             :                 /// </summary>
      69           1 :                 public virtual void Publish<TEvent>(IEnumerable<TEvent> events)
      70             :                         where TEvent : IEvent<TAuthenticationToken>
      71             :                 {
      72             :                         foreach (TEvent @event in events)
      73             :                                 Publish(@event);
      74             :                 }
      75             : 
      76             :                 #endregion
      77             : 
      78             :                 /// <summary>
      79             :                 /// Similar to a <see cref="IEventPublisher{TAuthenticationToken}"/>, passes events onto the <see cref="EventHandlerResolver"/>.
      80             :                 /// </summary>
      81             :                 public class BusActor
      82             :                         : ReceiveActor
      83           1 :                 {
      84             :                         /// <summary>
      85             :                         /// Instantiates a new instance of <see cref="BusActor"/>.
      86             :                         /// </summary>
      87           1 :                         public BusActor(IAkkaEventPublisher<TAuthenticationToken> eventHandlerResolver, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
      88             :                         {
      89             :                                 EventHandlerResolver = eventHandlerResolver;
      90             :                                 CorrelationIdHelper = correlationIdHelper;
      91             :                                 AuthenticationTokenHelper = authenticationTokenHelper;
      92             :                                 Receive<IEvent<TAuthenticationToken>>(@event => ExecuteReceive(@event));
      93             :                         }
      94             : 
      95             :                         /// <summary>
      96             :                         /// Gets or sets the <see cref="IAkkaEventPublisher{TAuthenticationToken}"/>.
      97             :                         /// </summary>
      98             :                         protected IAkkaEventPublisher<TAuthenticationToken> EventHandlerResolver { get; private set; }
      99             : 
     100             :                         /// <summary>
     101             :                         /// Gets or sets the <see cref="ICorrelationIdHelper"/>.
     102             :                         /// </summary>
     103             :                         protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
     104             : 
     105             :                         /// <summary>
     106             :                         /// Gets or sets the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}"/>.
     107             :                         /// </summary>
     108             :                         protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
     109             : 
     110             :                         /// <summary>
     111             :                         /// Passes the provided <paramref name="event"/> to <see cref="EventHandlerResolver"/> via <see cref="IEventPublisher{TAuthenticationToken}.Publish{TEvent}(TEvent)"/>
     112             :                         /// then calls <see cref="ActorRefImplicitSenderExtensions.Tell"/>.
     113             :                         /// </summary>
     114           1 :                         protected virtual void ExecuteReceive(IEvent<TAuthenticationToken> @event)
     115             :                         {
     116             :                                 try
     117             :                                 {
     118             :                                         AuthenticationTokenHelper.SetAuthenticationToken(@event.AuthenticationToken);
     119             :                                         CorrelationIdHelper.SetCorrelationId(@event.CorrelationId);
     120             :                                         EventHandlerResolver.Publish(@event);
     121             : 
     122             :                                         Sender.Tell(true);
     123             :                                 }
     124             :                                 catch
     125             :                                 {
     126             :                                         Sender.Tell(false);
     127             :                                         throw;
     128             :                                 }
     129             :                         }
     130             :                 }
     131             :         }
     132             : }

Generated by: LCOV version 1.12