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;
10 : using System.Collections.Generic;
11 : using Akka.Actor;
12 : using cdmdotnet.Logging;
13 : using Cqrs.Authentication;
14 : using Cqrs.Events;
15 :
16 : namespace Cqrs.Akka.Events
17 : {
18 : /// <summary>
19 : /// Executes event handler methods.
20 : /// </summary>
21 : /// <typeparam name="TAuthenticationToken"></typeparam>
22 : public abstract class AkkaEventHandler<TAuthenticationToken>
23 : : ReceiveActor // PersistentActor
24 1 : {
25 : /// <summary>
26 : /// Gets or sets the <see cref="ILogger"/>.
27 : /// </summary>
28 : protected ILogger Logger { get; set; }
29 :
30 : /// <summary>
31 : /// Gets or sets the <see cref="ICorrelationIdHelper"/>.
32 : /// </summary>
33 : protected ICorrelationIdHelper CorrelationIdHelper { get; private set; }
34 :
35 : /// <summary>
36 : /// Gets or sets the <see cref="IAuthenticationTokenHelper{TAuthenticationToken}"/>.
37 : /// </summary>
38 : protected IAuthenticationTokenHelper<TAuthenticationToken> AuthenticationTokenHelper { get; private set; }
39 :
40 : /// <summary>
41 : /// Instantiates a new instance of <see cref="AkkaEventHandler{TAuthenticationToken}"/>.
42 : /// </summary>
43 1 : protected AkkaEventHandler(ILogger logger, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
44 : {
45 : Logger = logger;
46 : CorrelationIdHelper = correlationIdHelper;
47 : AuthenticationTokenHelper = authenticationTokenHelper;
48 : }
49 :
50 : /// <summary>
51 : /// Execute the provided <paramref name="handler"/> passing it the <paramref name="event"/>,
52 : /// then calls <see cref="ActorRefImplicitSenderExtensions.Tell"/>.
53 : /// </summary>
54 1 : protected virtual void Execute<TEvent>(Action<TEvent> handler, TEvent @event)
55 : where TEvent : IEvent<TAuthenticationToken>
56 : {
57 : try
58 : {
59 : AuthenticationTokenHelper.SetAuthenticationToken(@event.AuthenticationToken);
60 : CorrelationIdHelper.SetCorrelationId(@event.CorrelationId);
61 : handler(@event);
62 :
63 : Sender.Tell(true, Self);
64 : }
65 : catch(Exception exception)
66 : {
67 : Logger.LogError("Executing an Akka.net request failed.", exception: exception, metaData: new Dictionary<string, object> { { "Type", GetType() }, { "Event", @event} });
68 : Sender.Tell(false, Self);
69 : throw;
70 : }
71 : }
72 : }
73 : }
|