LCOV - code coverage report
Current view: top level - Cqrs/Domain - SagaUnitOfWork.cs Hit Total Coverage
Test: doc-coverage.info Lines: 4 5 80.0 %
Date: 2017-07-26

          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.Collections.Generic;
      11             : using Cqrs.Domain.Exceptions;
      12             : 
      13             : namespace Cqrs.Domain
      14             : {
      15             :         /// <summary>
      16             :         /// This is a Unit of Work. This shouldn't normally be used as a singleton.
      17             :         /// </summary>
      18             :         public class SagaUnitOfWork<TAuthenticationToken> : ISagaUnitOfWork<TAuthenticationToken>
      19           1 :         {
      20             :                 private ISagaRepository<TAuthenticationToken> Repository { get; set; }
      21             : 
      22             :                 private Dictionary<Guid, ISagaDescriptor<TAuthenticationToken>> TrackedSagas { get; set; }
      23             : 
      24           0 :                 public SagaUnitOfWork(ISagaRepository<TAuthenticationToken> repository)
      25             :                 {
      26             :                         if(repository == null)
      27             :                                 throw new ArgumentNullException("repository");
      28             : 
      29             :                         Repository = repository;
      30             :                         TrackedSagas = new Dictionary<Guid, ISagaDescriptor<TAuthenticationToken>>();
      31             :                 }
      32             : 
      33             :                 /// <summary>
      34             :                 /// Add an item into the <see cref="IUnitOfWork{TAuthenticationToken}"/> ready to be committed.
      35             :                 /// </summary>
      36           1 :                 public void Add<TSaga>(TSaga saga)
      37             :                         where TSaga : ISaga<TAuthenticationToken>
      38             :                 {
      39             :                         if (!IsTracked(saga.Id))
      40             :                         {
      41             :                                 var sagaDescriptor = new SagaDescriptor<TSaga, TAuthenticationToken>
      42             :                                 {
      43             :                                         Saga = saga,
      44             :                                         Version = saga.Version
      45             :                                 };
      46             :                                 TrackedSagas.Add(saga.Id, sagaDescriptor);
      47             :                         }
      48             :                         else if (((TrackedSagas[saga.Id]).Saga) != (ISaga<TAuthenticationToken>)saga)
      49             :                                 throw new ConcurrencyException(saga.Id);
      50             :                 }
      51             : 
      52             :                 /// <summary>
      53             :                 /// Get an item from the <see cref="IUnitOfWork{TAuthenticationToken}"/> if it has already been loaded or get it from the <see cref="IRepository{TAuthenticationToken}"/>.
      54             :                 /// </summary>
      55           1 :                 public TSaga Get<TSaga>(Guid id, int? expectedVersion = null)
      56             :                         where TSaga : ISaga<TAuthenticationToken>
      57             :                 {
      58             :                         if(IsTracked(id))
      59             :                         {
      60             :                                 var trackedSaga = (TSaga)TrackedSagas[id].Saga;
      61             :                                 if (expectedVersion != null && trackedSaga.Version != expectedVersion)
      62             :                                         throw new ConcurrencyException(trackedSaga.Id);
      63             :                                 return trackedSaga;
      64             :                         }
      65             : 
      66             :                         var saga = Repository.Get<TSaga>(id);
      67             :                         if (expectedVersion != null && saga.Version != expectedVersion)
      68             :                                 throw new ConcurrencyException(id);
      69             :                         Add(saga);
      70             : 
      71             :                         return saga;
      72             :                 }
      73             : 
      74             :                 private bool IsTracked(Guid id)
      75             :                 {
      76             :                         return TrackedSagas.ContainsKey(id);
      77             :                 }
      78             : 
      79             :                 /// <summary>
      80             :                 /// Commit any changed <see cref="Saga{TAuthenticationToken}"/> added to this <see cref="IUnitOfWork{TAuthenticationToken}"/> via <see cref="Add{T}"/>
      81             :                 /// into the <see cref="IRepository{TAuthenticationToken}"/>
      82             :                 /// </summary>
      83           1 :                 public void Commit()
      84             :                 {
      85             :                         foreach (ISagaDescriptor<TAuthenticationToken> descriptor in TrackedSagas.Values)
      86             :                         {
      87             :                                 Repository.Save(descriptor.Saga, descriptor.Version);
      88             :                         }
      89             :                         TrackedSagas.Clear();
      90             :                 }
      91             :         }
      92             : }

Generated by: LCOV version 1.10