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 Cqrs.Domain.Exceptions;
12 : using Cqrs.Events;
13 :
14 : namespace Cqrs.Domain
15 : {
16 : /// <summary>
17 : /// Provides a basic container to control when <see cref="IEvent{TAuthenticationToken}">events</see> are store in an <see cref="IEventStore{TAuthenticationToken}"/> and then published on an <see cref="IEventPublisher{TAuthenticationToken}"/>.
18 : /// </summary>
19 : /// <remarks>
20 : /// This shouldn't normally be used as a singleton.
21 : /// </remarks>
22 : public class UnitOfWork<TAuthenticationToken> : IUnitOfWork<TAuthenticationToken>
23 1 : {
24 : private IAggregateRepository<TAuthenticationToken> Repository { get; set; }
25 :
26 : private Dictionary<Guid, IAggregateDescriptor<TAuthenticationToken>> TrackedAggregates { get; set; }
27 :
28 : /// <summary>
29 : /// Instantiates a new instance of <see cref="UnitOfWork{TAuthenticationToken}"/>
30 : /// </summary>
31 1 : public UnitOfWork(IAggregateRepository<TAuthenticationToken> repository)
32 : {
33 : if(repository == null)
34 : throw new ArgumentNullException("repository");
35 :
36 : Repository = repository;
37 : TrackedAggregates = new Dictionary<Guid, IAggregateDescriptor<TAuthenticationToken>>();
38 : }
39 :
40 : /// <summary>
41 : /// Add an item into the <see cref="IUnitOfWork{TAuthenticationToken}"/> ready to be committed.
42 : /// </summary>
43 1 : public void Add<TAggregateRoot>(TAggregateRoot aggregate)
44 : where TAggregateRoot : IAggregateRoot<TAuthenticationToken>
45 : {
46 : if (!IsTracked(aggregate.Id))
47 : {
48 : var aggregateDescriptor = new AggregateDescriptor<TAggregateRoot, TAuthenticationToken>
49 : {
50 : Aggregate = aggregate,
51 : Version = aggregate.Version
52 : };
53 : TrackedAggregates.Add(aggregate.Id, aggregateDescriptor);
54 : }
55 : else if (((TrackedAggregates[aggregate.Id]).Aggregate) != (IAggregateRoot<TAuthenticationToken>)aggregate)
56 : throw new ConcurrencyException(aggregate.Id);
57 : }
58 :
59 : /// <summary>
60 : /// Get an item from the <see cref="IUnitOfWork{TAuthenticationToken}"/> if it has already been loaded or get it from the <see cref="IAggregateRepository{TAuthenticationToken}"/>.
61 : /// </summary>
62 1 : public TAggregateRoot Get<TAggregateRoot>(Guid id, int? expectedVersion = null)
63 : where TAggregateRoot : IAggregateRoot<TAuthenticationToken>
64 : {
65 : if(IsTracked(id))
66 : {
67 : var trackedAggregate = (TAggregateRoot)TrackedAggregates[id].Aggregate;
68 : if (expectedVersion != null && trackedAggregate.Version != expectedVersion)
69 : throw new ConcurrencyException(trackedAggregate.Id);
70 : return trackedAggregate;
71 : }
72 :
73 : var aggregate = Repository.Get<TAggregateRoot>(id);
74 : if (expectedVersion != null && aggregate.Version != expectedVersion)
75 : throw new ConcurrencyException(id);
76 : Add(aggregate);
77 :
78 : return aggregate;
79 : }
80 :
81 : private bool IsTracked(Guid id)
82 : {
83 : return TrackedAggregates.ContainsKey(id);
84 : }
85 :
86 : /// <summary>
87 : /// Commit any changed <see cref="AggregateRoot{TAuthenticationToken}"/> added to this <see cref="IUnitOfWork{TAuthenticationToken}"/> via <see cref="Add{T}"/>
88 : /// into the <see cref="IAggregateRepository{TAuthenticationToken}"/>
89 : /// </summary>
90 1 : public void Commit()
91 : {
92 : foreach (IAggregateDescriptor<TAuthenticationToken> descriptor in TrackedAggregates.Values)
93 : {
94 : Repository.Save(descriptor.Aggregate, descriptor.Version);
95 : }
96 : TrackedAggregates.Clear();
97 : }
98 : }
99 : }
|