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