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 cdmdotnet.Logging;
11 : using Cqrs.Akka.Domain;
12 : using Cqrs.Akka.Tests.Unit.Commands;
13 : using Cqrs.Akka.Tests.Unit.Events;
14 : using Cqrs.Authentication;
15 : using Cqrs.Commands;
16 : using Cqrs.Configuration;
17 : using Cqrs.Domain;
18 :
19 : namespace Cqrs.Akka.Tests.Unit.Sagas
20 : {
21 : /// <summary>
22 : /// A <see cref="ISaga{TAuthenticationToken}"/> that acts as a process manager responding to several events and raising a command when a certain criteria is met.
23 : /// </summary>
24 : public class ConversationReportProcessManager : AkkaSaga<Guid>
25 1 : {
26 : /// <summary>
27 : /// The <see cref="ISaga{TAuthenticationToken}.Id"/>
28 : /// </summary>
29 : public Guid Rsn
30 : {
31 : get { return Id; }
32 : private set { Id = value; }
33 : }
34 :
35 : /// <summary>
36 : /// Indicates if this <see cref="ISaga{TAuthenticationToken}"/> has been deleted.
37 : /// </summary>
38 : public bool IsLogicallyDeleted { get; set; }
39 :
40 : /// <summary>
41 : /// The <see cref="IDependencyResolver"/> that resolves things.
42 : /// </summary>
43 : protected IDependencyResolver DependencyResolver { get; private set; }
44 :
45 : private bool HelloWorldWasSaid { get; set; }
46 :
47 : private bool HelloWorldWasRepliedTo { get; set; }
48 :
49 : private bool ConversationWasEnded { get; set; }
50 :
51 : // ReSharper disable UnusedMember.Local
52 : /// <summary>
53 : /// A constructor for the <see cref="Cqrs.Domain.Factories.IAggregateFactory"/>
54 : /// </summary>
55 : private ConversationReportProcessManager()
56 : : base(null, null, null, null, null, null)
57 : {
58 : Receive<HelloWorldSaid>(@event => Execute(Handle, @event));
59 : Receive<HelloWorldRepliedTo>(@event => Execute(Handle, @event));
60 : Receive<ConversationEnded>(@event => Execute(Handle, @event));
61 : }
62 :
63 : /// <summary>
64 : /// A constructor for the <see cref="Cqrs.Domain.Factories.IAggregateFactory"/>
65 : /// </summary>
66 : private ConversationReportProcessManager(IDependencyResolver dependencyResolver, ILogger logger)
67 : : this()
68 : {
69 : DependencyResolver = dependencyResolver;
70 : Logger = logger;
71 : CommandPublisher = DependencyResolver.Resolve<ICommandPublisher<Guid>>();
72 : UnitOfWork = DependencyResolver.Resolve<ISagaUnitOfWork<Guid>>();
73 : Repository = DependencyResolver.Resolve<IAkkaSagaRepository<Guid>>();
74 : CorrelationIdHelper = DependencyResolver.Resolve<ICorrelationIdHelper>();
75 : AuthenticationTokenHelper = DependencyResolver.Resolve<IAuthenticationTokenHelper<Guid>>();
76 : }
77 : // ReSharper restore UnusedMember.Local
78 :
79 : /// <summary>
80 : /// Instantiates a new instance of <see cref="ConversationReportProcessManager"/>.
81 : /// </summary>
82 1 : public ConversationReportProcessManager(IDependencyResolver dependencyResolver, ILogger logger, Guid rsn)
83 : : this(dependencyResolver, logger)
84 : {
85 : Rsn = rsn;
86 : }
87 :
88 : /// <summary>
89 : /// Responds to the provided <paramref name="event"/>.
90 : /// </summary>
91 : /// <param name="event">The <see cref="HelloWorldSaid"/> to respond to or "handle"</param>
92 1 : public virtual void Handle(HelloWorldSaid @event)
93 : {
94 : ApplyChange(@event);
95 : GenerateCommand();
96 : }
97 :
98 : /// <summary>
99 : /// Responds to the provided <paramref name="event"/>.
100 : /// </summary>
101 : /// <param name="event">The <see cref="HelloWorldRepliedTo"/> to respond to or "handle"</param>
102 1 : public virtual void Handle(HelloWorldRepliedTo @event)
103 : {
104 : ApplyChange(@event);
105 : GenerateCommand();
106 : }
107 :
108 : /// <summary>
109 : /// Responds to the provided <paramref name="event"/>.
110 : /// </summary>
111 : /// <param name="event">The <see cref="ConversationEnded"/> to respond to or "handle"</param>
112 1 : public virtual void Handle(ConversationEnded @event)
113 : {
114 : ApplyChange(@event);
115 : GenerateCommand();
116 : }
117 :
118 : /// <summary>
119 : /// Generates and publishes a <see cref="ICommand{TAuthenticationToken}"/>.
120 : /// </summary>
121 1 : protected virtual void GenerateCommand()
122 : {
123 : if (HelloWorldWasSaid && HelloWorldWasRepliedTo && ConversationWasEnded)
124 : {
125 : CommandPublisher.Publish
126 : (
127 : new UpdateCompletedConversationReportCommand()
128 : );
129 : }
130 : }
131 :
132 : /// <summary>
133 : /// Applies the <paramref name="event"/> to itself.
134 : /// </summary>
135 : /// <param name="event">The <see cref="HelloWorldSaid"/> to apply</param>
136 1 : public virtual void Apply(HelloWorldSaid @event)
137 : {
138 : HelloWorldWasSaid = true;
139 : }
140 :
141 : /// <summary>
142 : /// Applies the <paramref name="event"/> to itself.
143 : /// </summary>
144 : /// <param name="event">The <see cref="HelloWorldRepliedTo"/> to apply</param>
145 1 : public virtual void Apply(HelloWorldRepliedTo @event)
146 : {
147 : HelloWorldWasRepliedTo = true;
148 : }
149 :
150 : /// <summary>
151 : /// Applies the <paramref name="event"/> to itself.
152 : /// </summary>
153 : /// <param name="event">The <see cref="ConversationEnded"/> to apply</param>
154 1 : public virtual void Apply(ConversationEnded @event)
155 : {
156 : ConversationWasEnded = true;
157 : }
158 : }
159 : }
|