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 Cqrs.Akka.Domain.Commands;
13 : using Cqrs.Commands;
14 : using Cqrs.Domain;
15 : using Cqrs.Events;
16 :
17 : namespace Cqrs.Akka.Domain
18 : {
19 : /// <summary>
20 : /// A remote proxy to an <see cref="ISaga{TAuthenticationToken}"/>.
21 : /// </summary>
22 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of authentication token.</typeparam>
23 : /// <typeparam name="TSaga">The <see cref="Type"/> of <see cref="ISaga{TAuthenticationToken}"/>.</typeparam>
24 : public class AkkaSagaProxy<TAuthenticationToken, TSaga>
25 : : IAkkaSagaProxy<TSaga>
26 : , ISaga<TAuthenticationToken>
27 : // TODO think about if this is necessary again.
28 : // where TSaga : ISaga<TAuthenticationToken>
29 1 : {
30 : /// <summary>
31 : /// Gets the <see cref="IActorRef"/>.
32 : /// </summary>
33 : public IActorRef ActorReference { get; internal set; }
34 :
35 : /// <summary>
36 : /// Gets the <typeparamref name="TSaga"/>.
37 : /// </summary>
38 : public TSaga Saga { get; protected set; }
39 :
40 : #region Implementation of ISaga<TAuthenticationToken>
41 :
42 : /// <summary>
43 : /// The identifier of the <see cref="ISaga{TAuthenticationToken}"/>.
44 : /// </summary>
45 : public virtual Guid Id
46 : {
47 : get { return ActorReference.Ask<Guid>(new GetAkkaSagaId()).Result; }
48 : }
49 :
50 : /// <summary>
51 : /// The current version of this <see cref="ISaga{TAuthenticationToken}"/>.
52 : /// </summary>
53 : public virtual int Version
54 : {
55 : get { return ActorReference.Ask<int>(new GetAkkaSagaVersion()).Result; }
56 : }
57 :
58 : /// <summary>
59 : /// Get all applied changes that haven't yet been committed.
60 : /// </summary>
61 1 : public virtual IEnumerable<ISagaEvent<TAuthenticationToken>> GetUncommittedChanges()
62 : {
63 : return ((ISaga<TAuthenticationToken>)Saga).GetUncommittedChanges();
64 : }
65 :
66 : /// <summary>
67 : /// Mark all applied changes as committed, increment <see cref="Version"/> and flush the internal collection of changes.
68 : /// </summary>
69 1 : public virtual void MarkChangesAsCommitted()
70 : {
71 : ((ISaga<TAuthenticationToken>)Saga).MarkChangesAsCommitted();
72 : }
73 :
74 : /// <summary>
75 : /// Apply all the <see cref="IEvent{TAuthenticationToken}">events</see> in <paramref name="history"/>
76 : /// using event replay to this instance.
77 : /// </summary>
78 1 : public virtual void LoadFromHistory(IEnumerable<ISagaEvent<TAuthenticationToken>> history)
79 : {
80 : ((ISaga<TAuthenticationToken>)Saga).LoadFromHistory(history);
81 : }
82 :
83 : /// <summary>
84 : /// Get all pending commands that haven't yet been published yet.
85 : /// </summary>
86 1 : public IEnumerable<ICommand<TAuthenticationToken>> GetUnpublishedCommands()
87 : {
88 : return ((ISaga<TAuthenticationToken>)Saga).GetUnpublishedCommands();
89 : }
90 :
91 : /// <summary>
92 : /// Mark all published commands as published and flush the internal collection of commands.
93 : /// </summary>
94 1 : public void MarkCommandsAsPublished()
95 : {
96 : ((ISaga<TAuthenticationToken>) Saga).MarkCommandsAsPublished();
97 : }
98 :
99 : #endregion
100 : }
101 : }
|