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 : Initialise();
58 : }
59 :
60 : /// <summary>
61 : /// Initialise any properties
62 : /// </summary>
63 1 : protected virtual void Initialise()
64 : {
65 : }
66 :
67 : /// <summary>
68 : /// Get all applied changes that haven't yet been committed.
69 : /// </summary>
70 1 : public IEnumerable<IEvent<TAuthenticationToken>> GetUncommittedChanges()
71 : {
72 : return Changes;
73 : }
74 :
75 : /// <summary>
76 : /// Mark all applied changes as committed, increment <see cref="Version"/> and flush the <see cref="Changes">internal collection of changes</see>.
77 : /// </summary>
78 1 : public virtual void MarkChangesAsCommitted()
79 : {
80 : Lock.EnterWriteLock();
81 : try
82 : {
83 : Version = Version + Changes.Count;
84 : Changes = new ReadOnlyCollection<IEvent<TAuthenticationToken>>(new List<IEvent<TAuthenticationToken>>());
85 : }
86 : finally
87 : {
88 : Lock.ExitWriteLock();
89 : }
90 : }
91 :
92 : /// <summary>
93 : /// Apply all the <see cref="IEvent{TAuthenticationToken}">events</see> in <paramref name="history"/>
94 : /// using event replay to this instance.
95 : /// </summary>
96 1 : public virtual void LoadFromHistory(IEnumerable<IEvent<TAuthenticationToken>> history)
97 : {
98 : Type aggregateType = GetType();
99 : foreach (IEvent<TAuthenticationToken> @event in history.OrderBy(e => e.Version))
100 : {
101 : if (@event.Version != Version + 1)
102 : throw new EventsOutOfOrderException(@event.GetIdentity(), aggregateType, Version + 1, @event.Version);
103 : ApplyChange(@event, true);
104 : }
105 : }
106 :
107 : /// <summary>
108 : /// Call the "Apply" method with a signature matching the provided <paramref name="event"/> without using event replay to this instance.
109 : /// </summary>
110 : /// <remarks>
111 : /// This means a method named "Apply", with return type void and one parameter must exist to be applied.
112 : /// If no method exists, nothing is applied
113 : /// The parameter type must match exactly the <see cref="Type"/> of the provided <paramref name="event"/>.
114 : /// </remarks>
115 1 : protected virtual void ApplyChange(IEvent<TAuthenticationToken> @event)
116 : {
117 : ApplyChange(@event, false);
118 : }
119 :
120 : private void ApplyChange(IEvent<TAuthenticationToken> @event, bool isEventReplay)
121 : {
122 : ApplyChanges(new[] {@event}, isEventReplay);
123 : }
124 :
125 : /// <summary>
126 : /// Call the "Apply" method with a signature matching each <see cref="IEvent{TAuthenticationToken}"/> in the provided <paramref name="events"/> without using event replay to this instance.
127 : /// </summary>
128 : /// <remarks>
129 : /// This means a method named "Apply", with return type void and one parameter must exist to be applied.
130 : /// If no method exists, nothing is applied
131 : /// The parameter type must match exactly the <see cref="Type"/> of the <see cref="IEvent{TAuthenticationToken}"/> in the provided <paramref name="events"/>.
132 : /// </remarks>
133 1 : protected virtual void ApplyChanges(IEnumerable<IEvent<TAuthenticationToken>> events)
134 : {
135 : ApplyChanges(events, false);
136 : }
137 :
138 : private void ApplyChanges(IEnumerable<IEvent<TAuthenticationToken>> events, bool isEventReplay)
139 : {
140 : Lock.EnterWriteLock();
141 : IList<IEvent<TAuthenticationToken>> changes = new List<IEvent<TAuthenticationToken>>();
142 : try
143 : {
144 : try
145 : {
146 : dynamic dynamicThis = this.AsDynamic();
147 : foreach (IEvent<TAuthenticationToken> @event in events)
148 : {
149 : dynamicThis.Apply(@event);
150 : if (!isEventReplay)
151 : {
152 : changes.Add(@event);
153 : }
154 : else
155 : {
156 : Id = @event.GetIdentity();
157 : Version++;
158 : }
159 : }
160 : }
161 : finally
162 : {
163 : Changes = new ReadOnlyCollection<IEvent<TAuthenticationToken>>(Changes.Concat(changes).ToList());
164 : }
165 : }
166 : finally
167 : {
168 : Lock.ExitWriteLock();
169 : }
170 : }
171 : }
172 : }
|