Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Linq;
11 : using Cqrs.Domain;
12 :
13 : namespace Cqrs.Snapshots
14 : {
15 : /// <summary>
16 : /// An <see cref="ISnapshotStrategy{TAuthenticationToken}"/> that takes a snapshot every 15 versions
17 : /// </summary>
18 : /// <typeparam name="TAuthenticationToken"></typeparam>
19 : public class DefaultSnapshotStrategy<TAuthenticationToken> : ISnapshotStrategy<TAuthenticationToken>
20 1 : {
21 : private const int SnapshotInterval = 15;
22 :
23 0 : public bool IsSnapshotable(Type aggregateType)
24 : {
25 : if (aggregateType.BaseType == null)
26 : return false;
27 : if (aggregateType.BaseType.IsGenericType && aggregateType.BaseType.GetGenericTypeDefinition() == typeof(SnapshotAggregateRoot<,>))
28 : return true;
29 : return IsSnapshotable(aggregateType.BaseType);
30 : }
31 :
32 0 : public bool ShouldMakeSnapShot(IAggregateRoot<TAuthenticationToken> aggregate)
33 : {
34 : if (!IsSnapshotable(aggregate.GetType()))
35 : return false;
36 : int i = aggregate.Version;
37 :
38 : int limit = aggregate.GetUncommittedChanges().Count();
39 : for (int j = 0; j < limit; j++)
40 : if (++i % SnapshotInterval == 0 && i != 0)
41 : return true;
42 : return false;
43 : }
44 : }
45 : }
|