Line data Source code
1 : using System;
2 : using Akka.Actor;
3 : using cdmdotnet.Logging;
4 : using Cqrs.Akka.Domain;
5 : using Cqrs.Akka.Tests.Unit.Commands;
6 : using Cqrs.Akka.Tests.Unit.Events;
7 : using Cqrs.Authentication;
8 : using Cqrs.Commands;
9 : using Cqrs.Configuration;
10 : using Cqrs.Domain;
11 : using Cqrs.Events;
12 :
13 : namespace Cqrs.Akka.Tests.Unit.Sagas
14 : {
15 : public class ConversationReportProcessManagerEventHandlers
16 : : IEventHandler<Guid, HelloWorldSaid>
17 : , IEventHandler<Guid, HelloWorldRepliedTo>
18 : , IEventHandler<Guid, ConversationEnded>
19 0 : {
20 : /// <summary>
21 : /// Instantiates the <see cref="ConversationReportProcessManagerEventHandlers"/> class registering any <see cref="ReceiveActor.Receive{T}(System.Func{T,System.Threading.Tasks.Task})"/> required.
22 : /// </summary>
23 1 : public ConversationReportProcessManagerEventHandlers(IAkkaSagaResolver sagaResolver)
24 : {
25 : SagaResolver = sagaResolver;
26 : }
27 :
28 : protected IAkkaSagaResolver SagaResolver { get; private set; }
29 :
30 : #region Implementation of IMessageHandler<in HelloWorldRepliedTo>
31 :
32 0 : protected virtual void HandleEvent(IEvent<Guid> message)
33 : {
34 : // Resolve and locate the instance of the Saga to pass the message to
35 : IActorRef item = SagaResolver.ResolveActor<ConversationReportProcessManager, Guid>(message.Id);
36 : // Pass the message to it (and wait?)
37 : bool result = item.Ask<bool>(message).Result;
38 : // item.Tell(message);
39 : }
40 :
41 0 : public void Handle(HelloWorldRepliedTo message)
42 : {
43 : HandleEvent(message);
44 : }
45 :
46 : #endregion
47 :
48 : #region Implementation of IMessageHandler<in HelloWorldSaid>
49 :
50 0 : public void Handle(HelloWorldSaid message)
51 : {
52 : HandleEvent(message);
53 : }
54 :
55 : #endregion
56 :
57 : #region Implementation of IMessageHandler<in ConversationEnded>
58 :
59 0 : public void Handle(ConversationEnded message)
60 : {
61 : HandleEvent(message);
62 : }
63 :
64 : #endregion
65 : }
66 :
67 : public class ConversationReportProcessManager : AkkaSaga<Guid>
68 0 : {
69 : public Guid Rsn
70 : {
71 : get { return Id; }
72 : private set { Id = value; }
73 : }
74 :
75 : public bool IsLogicallyDeleted {get; set;}
76 :
77 : protected IDependencyResolver DependencyResolver { get; private set; }
78 :
79 : private bool HelloWorldWasSaid { get; set; }
80 :
81 : private bool HelloWorldWasRepliedTo { get; set; }
82 :
83 : private bool ConversationWasEnded { get; set; }
84 :
85 : // ReSharper disable UnusedMember.Local
86 : /// <summary>
87 : /// A constructor for the <see cref="Cqrs.Domain.Factories.IAggregateFactory"/>
88 : /// </summary>
89 : private ConversationReportProcessManager()
90 : : base(null, null, null, null, null, null)
91 : {
92 : Receive<HelloWorldSaid>(@event => Execute(Handle, @event));
93 : Receive<HelloWorldRepliedTo>(@event => Execute(Handle, @event));
94 : Receive<ConversationEnded>(@event => Execute(Handle, @event));
95 : }
96 :
97 : /// <summary>
98 : /// A constructor for the <see cref="Cqrs.Domain.Factories.IAggregateFactory"/>
99 : /// </summary>
100 : private ConversationReportProcessManager(IDependencyResolver dependencyResolver, ILogger logger)
101 : : this()
102 : {
103 : DependencyResolver = dependencyResolver;
104 : Logger = logger;
105 : CommandPublisher = DependencyResolver.Resolve<ICommandPublisher<Guid>>();
106 : UnitOfWork = DependencyResolver.Resolve<ISagaUnitOfWork<Guid>>();
107 : Repository = DependencyResolver.Resolve<IAkkaSagaRepository<Guid>>();
108 : CorrelationIdHelper = DependencyResolver.Resolve<ICorrelationIdHelper>();
109 : AuthenticationTokenHelper = DependencyResolver.Resolve<IAuthenticationTokenHelper<Guid>>();
110 : }
111 : // ReSharper restore UnusedMember.Local
112 :
113 0 : public ConversationReportProcessManager(IDependencyResolver dependencyResolver, ILogger logger, Guid rsn)
114 : : this(dependencyResolver, logger)
115 : {
116 : Rsn = rsn;
117 : }
118 :
119 0 : public virtual void Handle(HelloWorldSaid @event)
120 : {
121 : ApplyChange(@event);
122 : GenerateCommand();
123 : }
124 :
125 0 : public virtual void Handle(HelloWorldRepliedTo @event)
126 : {
127 : ApplyChange(@event);
128 : GenerateCommand();
129 : }
130 :
131 0 : public virtual void Handle(ConversationEnded @event)
132 : {
133 : ApplyChange(@event);
134 : GenerateCommand();
135 : }
136 :
137 0 : protected virtual void GenerateCommand()
138 : {
139 : if (HelloWorldWasSaid && HelloWorldWasRepliedTo && ConversationWasEnded)
140 : {
141 : CommandPublisher.Publish
142 : (
143 : new UpdateCompletedConversationReportCommand()
144 : );
145 : }
146 : }
147 :
148 0 : public virtual void Apply(HelloWorldSaid @event)
149 : {
150 : HelloWorldWasSaid = true;
151 : }
152 :
153 0 : public virtual void Apply(HelloWorldRepliedTo @event)
154 : {
155 : HelloWorldWasRepliedTo = true;
156 : }
157 :
158 0 : public virtual void Apply(ConversationEnded @event)
159 : {
160 : ConversationWasEnded = true;
161 : }
162 : }
163 : }
|