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 System.Collections.ObjectModel;
12 : using System.Linq;
13 : using System.Runtime.Serialization;
14 : using System.Threading;
15 : using Cqrs.Domain.Exceptions;
16 : using Cqrs.Events;
17 : using Cqrs.Infrastructure;
18 :
19 : namespace Cqrs.Domain
20 : {
21 : /// <summary>
22 : /// A larger unit of encapsulation than just a class. Every transaction is scoped to a single aggregate. The lifetimes of the components of an aggregate are bounded by the lifetime of the entire aggregate.
23 : ///
24 : /// Concretely, an aggregate will handle commands, apply events, and have a state model encapsulated within it that allows it to implement the required command validation, thus upholding the invariants (business rules) of the aggregate.
25 : /// </summary>
26 : /// <remarks>
27 : /// Why is the use of GUID as IDs a good practice?
28 : ///
29 : /// Because they are (reasonably) globally unique, and can be generated either by the server or by the client.
30 : /// </remarks>
31 : [Serializable]
32 : public abstract class AggregateRoot<TAuthenticationToken> : IAggregateRoot<TAuthenticationToken>
33 1 : {
34 : private ReaderWriterLockSlim Lock { get; set; }
35 :
36 : private ICollection<IEvent<TAuthenticationToken>> Changes { get; set; }
37 :
38 : /// <summary>
39 : /// The identifier of this <see cref="IAggregateRoot{TAuthenticationToken}"/>.
40 : /// </summary>
41 : [DataMember]
42 : public Guid Id { get; protected set; }
43 :
44 : /// <summary>
45 : /// The current version of this <see cref="IAggregateRoot{TAuthenticationToken}"/>.
46 : /// </summary>
47 : [DataMember]
48 : public int Version { get; protected set; }
49 :
50 : /// <summary>
51 : /// Instantiates a new instance of <see cref="AggregateRoot{TAuthenticationToken}"/>.
52 : /// </summary>
53 1 : protected AggregateRoot()
54 : {
55 : Lock = new ReaderWriterLockSlim();
56 : Changes = new ReadOnlyCollection<IEvent<TAuthenticationToken>>(new List<IEvent<TAuthenticationToken>>());
57 : }
58 :
59 : /// <summary>
60 : /// Get all applied changes that haven't yet been committed.
61 : /// </summary>
62 1 : public IEnumerable<IEvent<TAuthenticationToken>> GetUncommittedChanges()
63 : {
64 : return Changes;
65 : }
66 :
67 : /// <summary>
68 : /// Mark all applied changes as committed, increment <see cref="Version"/> and flush the <see cref="Changes">internal collection of changes</see>.
69 : /// </summary>
70 1 : public virtual void MarkChangesAsCommitted()
71 : {
72 : Lock.EnterWriteLock();
73 : try
74 : {
75 : Version = Version + Changes.Count;
76 : Changes = new ReadOnlyCollection<IEvent<TAuthenticationToken>>(new List<IEvent<TAuthenticationToken>>());
77 : }
78 : finally
79 : {
80 : Lock.ExitWriteLock();
81 : }
82 : }
83 :
84 : /// <summary>
85 : /// Apply all the <see cref="IEvent{TAuthenticationToken}">events</see> in <paramref name="history"/>
86 : /// using event replay to this instance.
87 : /// </summary>
88 1 : public virtual void LoadFromHistory(IEnumerable<IEvent<TAuthenticationToken>> history)
89 : {
90 : Type aggregateType = GetType();
91 : foreach (IEvent<TAuthenticationToken> @event in history.OrderBy(e => e.Version))
92 : {
93 : if (@event.Version != Version + 1)
94 : throw new EventsOutOfOrderException(@event.Id, aggregateType, Version + 1, @event.Version);
95 : ApplyChange(@event, true);
96 : }
97 : }
98 :
99 : /// <summary>
100 : /// Call the "Apply" method with a signature matching the provided <paramref name="event"/> without using event replay to this instance.
101 : /// </summary>
102 : /// <remarks>
103 : /// This means a method named "Apply", with return type void and one parameter must exist to be applied.
104 : /// If no method exists, nothing is applied
105 : /// The parameter type must match exactly the <see cref="Type"/> of the provided <paramref name="event"/>.
106 : /// </remarks>
107 1 : protected virtual void ApplyChange(IEvent<TAuthenticationToken> @event)
108 : {
109 : ApplyChange(@event, false);
110 : }
111 :
112 : private void ApplyChange(IEvent<TAuthenticationToken> @event, bool isEventReplay)
113 : {
114 : Lock.EnterWriteLock();
115 : try
116 : {
117 : this.AsDynamic().Apply(@event);
118 : if (!isEventReplay)
119 : {
120 : Changes = new ReadOnlyCollection<IEvent<TAuthenticationToken>>(Changes.Concat(new[] { @event }).ToList());
121 : }
122 : else
123 : {
124 : Id = @event.Id;
125 : Version++;
126 : }
127 : }
128 : finally
129 : {
130 : Lock.ExitWriteLock();
131 : }
132 : }
133 : }
134 : }
|