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.Commands;
15 : using Cqrs.Configuration;
16 :
17 : namespace Cqrs.Akka.Commands
18 : {
19 : /// <summary>
20 : /// A <see cref="ICommandPublisher{TAuthenticationToken}"/> that proxies <see cref="ICommand{TAuthenticationToken}"/> to the <see cref="IActorRef"/> which acts as a single point of all handler resolutions.
21 : /// </summary>
22 : public class AkkaCommandBusProxy<TAuthenticationToken>
23 : : IAkkaCommandSenderProxy<TAuthenticationToken>
24 1 : {
25 0 : public AkkaCommandBusProxy(IDependencyResolver dependencyResolver, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
26 : {
27 : CorrelationIdHelper = correlationIdHelper;
28 : AuthenticationTokenHelper = authenticationTokenHelper;
29 : CommandHandlerResolver = ((IAkkaAggregateResolver)dependencyResolver).ResolveActor<BusActor>();
30 : }
31 :
32 : protected IActorRef CommandHandlerResolver { 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 ICommandSender<TAuthenticationToken>
39 :
40 1 : public virtual void Publish<TCommand>(TCommand command)
41 : where TCommand : ICommand<TAuthenticationToken>
42 : {
43 : // We only set these two properties as they are not going to be available across the thread/task
44 : if (command.AuthenticationToken == null || command.AuthenticationToken.Equals(default(TAuthenticationToken)))
45 : command.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
46 : command.CorrelationId = CorrelationIdHelper.GetCorrelationId();
47 :
48 : bool result = CommandHandlerResolver.Ask<bool>(command).Result;
49 : }
50 :
51 0 : public virtual void Send<TCommand>(TCommand command)
52 : where TCommand : ICommand<TAuthenticationToken>
53 : {
54 : Publish(command);
55 : }
56 :
57 1 : public virtual void Publish<TCommand>(IEnumerable<TCommand> commands)
58 : where TCommand : ICommand<TAuthenticationToken>
59 : {
60 : foreach (TCommand rawCommand in commands)
61 : {
62 : // Lambda scoping thing
63 : TCommand command = rawCommand;
64 : // We only set these two properties as they are not going to be available across the thread/task
65 : if (command.AuthenticationToken == null || command.AuthenticationToken.Equals(default(TAuthenticationToken)))
66 : command.AuthenticationToken = AuthenticationTokenHelper.GetAuthenticationToken();
67 : command.CorrelationId = CorrelationIdHelper.GetCorrelationId();
68 :
69 : bool result = CommandHandlerResolver.Ask<bool>(command).Result;
70 : }
71 : }
72 :
73 0 : public virtual void Send<TCommand>(IEnumerable<TCommand> commands)
74 : where TCommand : ICommand<TAuthenticationToken>
75 : {
76 : Publish(commands);
77 : }
78 :
79 : #endregion
80 :
81 : public class BusActor
82 : : ReceiveActor
83 0 : {
84 0 : public BusActor(IAkkaCommandSender<TAuthenticationToken> commandHandlerResolver, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
85 : {
86 : CommandHandlerResolver = commandHandlerResolver;
87 : CorrelationIdHelper = correlationIdHelper;
88 : AuthenticationTokenHelper = authenticationTokenHelper;
89 : Receive<ICommand<TAuthenticationToken>>(command => ExecuteReceive(command));
90 : }
91 :
92 : protected IAkkaCommandSender<TAuthenticationToken> CommandHandlerResolver { get; private set; }
93 :
94 : protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
95 :
96 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
97 :
98 0 : protected virtual void ExecuteReceive(ICommand<TAuthenticationToken> command)
99 : {
100 : try
101 : {
102 : AuthenticationTokenHelper.SetAuthenticationToken(command.AuthenticationToken);
103 : CorrelationIdHelper.SetCorrelationId(command.CorrelationId);
104 : CommandHandlerResolver.Send(command);
105 :
106 : Sender.Tell(true);
107 : }
108 : catch
109 : {
110 : Sender.Tell(false);
111 : throw;
112 : }
113 : }
114 : }
115 : }
116 : }
|