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 cdmdotnet.Logging;
10 : using Cqrs.Akka.Domain;
11 : using Cqrs.Authentication;
12 : using Cqrs.Domain;
13 : using Cqrs.Snapshots;
14 :
15 : namespace Cqrs.Akka.Snapshots
16 : {
17 : /// <summary>
18 : /// An <see cref="IAggregateRoot{TAuthenticationToken}"/> that supports <see cref="Snapshot">snapshots</see> for optimised rehydration.
19 : /// </summary>
20 : /// <typeparam name="TAuthenticationToken"></typeparam>
21 : /// <typeparam name="TSnapshot"></typeparam>
22 : public abstract class AkkaSnapshotAggregateRoot<TAuthenticationToken, TSnapshot>
23 : : AkkaAggregateRoot<TAuthenticationToken>
24 : where TSnapshot : Snapshot
25 1 : {
26 : /// <summary>
27 : /// Instantiates a new instance of <see cref="AkkaAggregateRoot{TAuthenticationToken}"/>
28 : /// </summary>
29 1 : protected AkkaSnapshotAggregateRoot(IUnitOfWork<TAuthenticationToken> unitOfWork, ILogger logger, IAkkaAggregateRepository<TAuthenticationToken> repository, ICorrelationIdHelper correlationIdHelper, IAuthenticationTokenHelper<TAuthenticationToken> authenticationTokenHelper)
30 : : base(unitOfWork, logger, repository, correlationIdHelper, authenticationTokenHelper)
31 : {
32 : }
33 :
34 : /// <summary>
35 : /// Calls <see cref="CreateSnapshot"/> and applies the <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of this instance to the <typeparamref name="TSnapshot"/> result.
36 : /// </summary>
37 1 : public virtual TSnapshot GetSnapshot()
38 : {
39 : TSnapshot snapshot = CreateSnapshot();
40 : snapshot.Id = Id;
41 : return snapshot;
42 : }
43 :
44 : /// <summary>
45 : /// Sets the <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of this instance from <see cref="Snapshot.Id"/> the provided <paramref name="snapshot"/>,
46 : /// sets the <see cref="IAggregateRoot{TAuthenticationToken}.Version"/> of this instance from <see cref="Snapshot.Version"/> the provided <paramref name="snapshot"/>,
47 : /// then calls <see cref="RestoreFromSnapshot"/>
48 : /// </summary>
49 : /// <param name="snapshot">The <typeparamref name="TSnapshot"/> to rehydrate this instance from.</param>
50 1 : public virtual void Restore(TSnapshot snapshot)
51 : {
52 : Id = snapshot.Id;
53 : Version = snapshot.Version;
54 : RestoreFromSnapshot(snapshot);
55 : }
56 :
57 : /// <summary>
58 : /// Create a <typeparamref name="TSnapshot"/> of the current state of this instance.
59 : /// </summary>
60 1 : protected abstract TSnapshot CreateSnapshot();
61 :
62 : /// <summary>
63 : /// Rehydrate this instance from the provided <paramref name="snapshot"/>.
64 : /// </summary>
65 : /// <param name="snapshot">The <typeparamref name="TSnapshot"/> to rehydrate this instance from.</param>
66 1 : protected abstract void RestoreFromSnapshot(TSnapshot snapshot);
67 : }
68 : }
|