LCOV - code coverage report
Current view: top level - Cqrs/Snapshots - SnapshotRepository.cs Hit Total Coverage
Test: doc-coverage.info Lines: 0 4 0.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 System.Linq;
      12             : using Cqrs.Domain;
      13             : using Cqrs.Domain.Factories;
      14             : using Cqrs.Events;
      15             : using Cqrs.Infrastructure;
      16             : 
      17             : namespace Cqrs.Snapshots
      18             : {
      19             :         public class SnapshotRepository<TAuthenticationToken> : IAggregateRepository<TAuthenticationToken>
      20           0 :         {
      21             :                 private ISnapshotStore SnapshotStore { get; set; }
      22             : 
      23             :                 private ISnapshotStrategy<TAuthenticationToken> SnapshotStrategy { get; set; }
      24             : 
      25             :                 private IAggregateRepository<TAuthenticationToken> Repository { get; set; }
      26             : 
      27             :                 private IEventStore<TAuthenticationToken> EventStore { get; set; }
      28             : 
      29             :                 private IAggregateFactory AggregateFactory { get; set; }
      30             : 
      31           0 :                 public SnapshotRepository(ISnapshotStore snapshotStore, ISnapshotStrategy<TAuthenticationToken> snapshotStrategy, IAggregateRepository<TAuthenticationToken> repository, IEventStore<TAuthenticationToken> eventStore, IAggregateFactory aggregateFactory)
      32             :                 {
      33             :                         SnapshotStore = snapshotStore;
      34             :                         SnapshotStrategy = snapshotStrategy;
      35             :                         Repository = repository;
      36             :                         EventStore = eventStore;
      37             :                         AggregateFactory = aggregateFactory;
      38             :                 }
      39             : 
      40           0 :                 public void Save<TAggregateRoot>(TAggregateRoot aggregate, int? exectedVersion = null)
      41             :                         where TAggregateRoot : IAggregateRoot<TAuthenticationToken>
      42             :                 {
      43             :                         TryMakeSnapshot(aggregate);
      44             :                         Repository.Save(aggregate, exectedVersion);
      45             :                 }
      46             : 
      47           0 :                 public TAggregateRoot Get<TAggregateRoot>(Guid aggregateId, IList<IEvent<TAuthenticationToken>> events = null)
      48             :                         where TAggregateRoot : IAggregateRoot<TAuthenticationToken>
      49             :                 {
      50             :                         var aggregate = AggregateFactory.Create<TAggregateRoot>();
      51             :                         int snapshotVersion = TryRestoreAggregateFromSnapshot(aggregateId, aggregate);
      52             :                         if (snapshotVersion == -1)
      53             :                         {
      54             :                                 return Repository.Get<TAggregateRoot>(aggregateId);
      55             :                         }
      56             :                         IEnumerable<IEvent<TAuthenticationToken>> theseEvents = events ?? EventStore.Get<TAggregateRoot>(aggregateId, false, snapshotVersion).Where(desc => desc.Version > snapshotVersion);
      57             :                         aggregate.LoadFromHistory(theseEvents);
      58             : 
      59             :                         return aggregate;
      60             :                 }
      61             : 
      62             :                 private int TryRestoreAggregateFromSnapshot<TAggregateRoot>(Guid id, TAggregateRoot aggregate)
      63             :                 {
      64             :                         int version = -1;
      65             :                         if (SnapshotStrategy.IsSnapshotable(typeof(TAggregateRoot)))
      66             :                         {
      67             :                                 Snapshot snapshot = SnapshotStore.Get(id);
      68             :                                 if (snapshot != null)
      69             :                                 {
      70             :                                         aggregate.AsDynamic().Restore(snapshot);
      71             :                                         version = snapshot.Version;
      72             :                                 }
      73             :                         }
      74             :                         return version;
      75             :                 }
      76             : 
      77             :                 private void TryMakeSnapshot(IAggregateRoot<TAuthenticationToken> aggregate)
      78             :                 {
      79             :                         if (!SnapshotStrategy.ShouldMakeSnapShot(aggregate))
      80             :                                 return;
      81             :                         dynamic snapshot = aggregate.AsDynamic().GetSnapshot().RealObject;
      82             :                         snapshot.Version = aggregate.Version + aggregate.GetUncommittedChanges().Count();
      83             :                         SnapshotStore.Save(snapshot);
      84             :                 }
      85             :         }
      86             : }

Generated by: LCOV version 1.10