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 Akka.Actor;
11 : using cdmdotnet.Logging;
12 : using Cqrs.Akka.Commands;
13 : using Cqrs.Akka.Domain;
14 : using Cqrs.Akka.Events;
15 : using Cqrs.Akka.Tests.Unit.Commands;
16 : using Cqrs.Commands;
17 : using Cqrs.Events;
18 : using Cqrs.Authentication;
19 : using Cqrs.Domain;
20 :
21 : namespace Cqrs.Akka.Tests.Unit.Events.Handlers
22 : {
23 : /// <summary>
24 : /// Handles the <see cref="HelloWorldRepliedTo"/> and sends a <see cref="EndConversationCommand"/>.
25 : /// </summary>
26 : public class HelloWorldRepliedToSendEndConversationCommandEventHandler
27 : : IEventHandler<Guid, HelloWorldRepliedTo>
28 1 : {
29 : /// <summary>
30 : /// Instantiates the <see cref="HelloWorldRepliedToSendEndConversationCommandEventHandler"/> class registering any <see cref="ReceiveActor.Receive{T}(System.Func{T,System.Threading.Tasks.Task})"/> required.
31 : /// </summary>
32 1 : public HelloWorldRepliedToSendEndConversationCommandEventHandler(IAkkaAggregateResolver aggregateResolver)
33 : {
34 : AggregateResolver = aggregateResolver;
35 : }
36 :
37 : /// <summary>
38 : /// Resolves Akka.Net actor based <see cref="IAggregateRoot{TAuthenticationToken}"/>
39 : /// </summary>
40 : protected IAkkaAggregateResolver AggregateResolver { get; private set; }
41 :
42 : #region Implementation of IMessageHandler<in HelloWorldRepliedTo>
43 :
44 : /// <summary>
45 : /// Responds to the provided <paramref name="event"/> passing the <paramref name="event"/> to an Akka.Net actor.
46 : /// </summary>
47 : /// <param name="event">The <see cref="HelloWorldRepliedTo"/> to respond to or "handle"</param>
48 1 : public void Handle(HelloWorldRepliedTo @event)
49 : {
50 : IActorRef item = AggregateResolver.ResolveActor<Actor>();
51 : // bool result = global::Akka.Actor.Futures.Ask<bool>(item, @event).Result;
52 : ActorRefImplicitSenderExtensions.Tell(item, @event);
53 : }
54 :
55 : #endregion
56 :
57 : /// <summary>
58 : /// An Akka.Net based <see cref="IEventHandler"/> that handles the <see cref="HelloWorldRepliedTo"/>.
59 : /// </summary>
60 : public class Actor
61 : : AkkaEventHandler<Guid>
62 1 : {
63 : /// <summary>
64 : /// Publish any <see cref="ICommand{TAuthenticationToken}"/> instances that you want to send with this.
65 : /// </summary>
66 : protected ICommandPublisher<Guid> CommandBus { get; private set; }
67 :
68 : #region Implementation of IMessageHandler<in HelloWorldRepliedTo>
69 :
70 : /// <summary>
71 : /// Responds to the provided <paramref name="message"/>.
72 : /// </summary>
73 : /// <param name="message">The <see cref="HelloWorldRepliedTo"/> to respond to or "handle"</param>
74 1 : public void Handle(HelloWorldRepliedTo message)
75 : {
76 : CommandBus.Publish(new EndConversationCommand { Id = message.Id });
77 : AkkaUnitTests.Step3Reached[message.CorrelationId] = true;
78 : }
79 :
80 : #endregion
81 :
82 : /// <summary>
83 : /// Instantiates a new instance of <see cref="Actor"/>.
84 : /// </summary>
85 1 : public Actor(ILogger logger, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<Guid> authenticationTokenHelper, IAkkaCommandPublisher<Guid> commandBus)
86 : : base(logger, correlationIdHelper, authenticationTokenHelper)
87 : {
88 : CommandBus = commandBus;
89 : Receive<HelloWorldRepliedTo>(@event => Execute(Handle, @event));
90 : }
91 : }
92 : }
93 : }
|