Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet 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 0 : public AkkaEventBusProxy(IDependencyResolver dependencyResolver, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
26 : {
27 : CorrelationIdHelper = correlationIdHelper;
28 : AuthenticationTokenHelper = authenticationTokenHelper;
29 : EventHandlerResolver = ((IAkkaAggregateResolver)dependencyResolver).ResolveActor<BusActor>();
30 : }
31 :
32 : protected IActorRef EventHandlerResolver { get; private set; }
33 :
34 : protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
35 :
36 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
37 :
38 : #region Implementation of IEventPublisher<TAuthenticationToken>
39 :
40 0 : public virtual void Publish<TEvent>(TEvent @event)
41 : where TEvent : IEvent<TAuthenticationToken>
42 : {
43 : // We only set these two properties as they are not going to be available across the thread/task
44 : if (@event.AuthenticationToken == null || @event.AuthenticationToken.Equals(default(TAuthenticationToken)))
45 : @event.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
46 : @event.CorrelationId = CorrelationIdHelper.GetCorrelationId();
47 :
48 : bool result = EventHandlerResolver.Ask<bool>(@event).Result;
49 : }
50 :
51 0 : public virtual void Publish<TEvent>(IEnumerable<TEvent> events)
52 : where TEvent : IEvent<TAuthenticationToken>
53 : {
54 : foreach (TEvent @event in events)
55 : Publish(@event);
56 : }
57 :
58 : #endregion
59 :
60 : public class BusActor
61 : : ReceiveActor
62 0 : {
63 0 : public BusActor(IAkkaEventPublisher<TAuthenticationToken> eventHandlerResolver, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
64 : {
65 : EventHandlerResolver = eventHandlerResolver;
66 : CorrelationIdHelper = correlationIdHelper;
67 : AuthenticationTokenHelper = authenticationTokenHelper;
68 : Receive<IEvent<TAuthenticationToken>>(@event => ExecuteReceive(@event));
69 : }
70 :
71 : protected IAkkaEventPublisher<TAuthenticationToken> EventHandlerResolver { get; private set; }
72 :
73 : protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
74 :
75 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
76 :
77 0 : protected virtual void ExecuteReceive(IEvent<TAuthenticationToken> @event)
78 : {
79 : try
80 : {
81 : AuthenticationTokenHelper.SetAuthenticationToken(@event.AuthenticationToken);
82 : CorrelationIdHelper.SetCorrelationId(@event.CorrelationId);
83 : EventHandlerResolver.Publish(@event);
84 :
85 : Sender.Tell(true);
86 : }
87 : catch
88 : {
89 : Sender.Tell(false);
90 : throw;
91 : }
92 : }
93 : }
94 : }
95 : }
|