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 SagaUnitOfWork<TAuthenticationToken> : ISagaUnitOfWork<TAuthenticationToken>
23 1 : {
24 : private ISagaRepository<TAuthenticationToken> Repository { get; set; }
25 :
26 : private Dictionary<Guid, ISagaDescriptor<TAuthenticationToken>> TrackedSagas { get; set; }
27 :
28 : /// <summary>
29 : /// Instantiates a new instance of <see cref="SagaUnitOfWork{TAuthenticationToken}"/>
30 : /// </summary>
31 1 : public SagaUnitOfWork(ISagaRepository<TAuthenticationToken> repository)
32 : {
33 : if(repository == null)
34 : throw new ArgumentNullException("repository");
35 :
36 : Repository = repository;
37 : TrackedSagas = new Dictionary<Guid, ISagaDescriptor<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<TSaga>(TSaga saga)
44 : where TSaga : ISaga<TAuthenticationToken>
45 : {
46 : if (!IsTracked(saga.Id))
47 : {
48 : var sagaDescriptor = new SagaDescriptor<TSaga, TAuthenticationToken>
49 : {
50 : Saga = saga,
51 : Version = saga.Version
52 : };
53 : TrackedSagas.Add(saga.Id, sagaDescriptor);
54 : }
55 : else if (((TrackedSagas[saga.Id]).Saga) != (ISaga<TAuthenticationToken>)saga)
56 : throw new ConcurrencyException(saga.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="ISagaRepository{TAuthenticationToken}"/>.
61 : /// </summary>
62 1 : public TSaga Get<TSaga>(Guid id, int? expectedVersion = null)
63 : where TSaga : ISaga<TAuthenticationToken>
64 : {
65 : if(IsTracked(id))
66 : {
67 : var trackedSaga = (TSaga)TrackedSagas[id].Saga;
68 : if (expectedVersion != null && trackedSaga.Version != expectedVersion)
69 : throw new ConcurrencyException(trackedSaga.Id);
70 : return trackedSaga;
71 : }
72 :
73 : var saga = Repository.Get<TSaga>(id);
74 : if (expectedVersion != null && saga.Version != expectedVersion)
75 : throw new ConcurrencyException(id);
76 : Add(saga);
77 :
78 : return saga;
79 : }
80 :
81 : private bool IsTracked(Guid id)
82 : {
83 : return TrackedSagas.ContainsKey(id);
84 : }
85 :
86 : /// <summary>
87 : /// Commit any changed <see cref="Saga{TAuthenticationToken}"/> added to this <see cref="IUnitOfWork{TAuthenticationToken}"/> via <see cref="Add{T}"/>
88 : /// into the <see cref="ISagaRepository{TAuthenticationToken}"/>
89 : /// </summary>
90 1 : public void Commit()
91 : {
92 : foreach (ISagaDescriptor<TAuthenticationToken> descriptor in TrackedSagas.Values)
93 : {
94 : Repository.Save(descriptor.Saga, descriptor.Version);
95 : }
96 : TrackedSagas.Clear();
97 : }
98 : }
99 : }
|