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 Cqrs.Domain;
10 :
11 : namespace Cqrs.Snapshots
12 : {
13 : /// <summary>
14 : /// An <see cref="IAggregateRoot{TAuthenticationToken}"/> that supports <see cref="Snapshot">snapshots</see> for optimised rehydration.
15 : /// </summary>
16 : /// <typeparam name="TAuthenticationToken"></typeparam>
17 : /// <typeparam name="TSnapshot"></typeparam>
18 : public abstract class SnapshotAggregateRoot<TAuthenticationToken, TSnapshot>
19 : : AggregateRoot<TAuthenticationToken>
20 : where TSnapshot : Snapshot
21 1 : {
22 : /// <summary>
23 : /// Calls <see cref="CreateSnapshot"/> and applies the <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of this instance to the <typeparamref name="TSnapshot"/> result.
24 : /// </summary>
25 1 : public virtual TSnapshot GetSnapshot()
26 : {
27 : TSnapshot snapshot = CreateSnapshot();
28 : snapshot.Id = Id;
29 : return snapshot;
30 : }
31 :
32 : /// <summary>
33 : /// Sets the <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of this instance from <see cref="Snapshot.Id"/> the provided <paramref name="snapshot"/>,
34 : /// sets the <see cref="IAggregateRoot{TAuthenticationToken}.Version"/> of this instance from <see cref="Snapshot.Version"/> the provided <paramref name="snapshot"/>,
35 : /// then calls <see cref="RestoreFromSnapshot"/>
36 : /// </summary>
37 : /// <param name="snapshot">The <typeparamref name="TSnapshot"/> to rehydrate this instance from.</param>
38 1 : public virtual void Restore(TSnapshot snapshot)
39 : {
40 : Id = snapshot.Id;
41 : Version = snapshot.Version;
42 : RestoreFromSnapshot(snapshot);
43 : }
44 :
45 : /// <summary>
46 : /// Create a <typeparamref name="TSnapshot"/> of the current state of this instance.
47 : /// </summary>
48 1 : protected abstract TSnapshot CreateSnapshot();
49 :
50 : /// <summary>
51 : /// Rehydrate this instance from the provided <paramref name="snapshot"/>.
52 : /// </summary>
53 : /// <param name="snapshot">The <typeparamref name="TSnapshot"/> to rehydrate this instance from.</param>
54 1 : protected abstract void RestoreFromSnapshot(TSnapshot snapshot);
55 : }
56 : }
|