Documentation Coverage Report
Current view: top level - Cqrs/Events - InProcessEventStore.cs Hit Total Coverage
Version: 2.2 Artefacts: 13 13 100.0 %
Date: 2018-08-07 15:04:50

          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.Linq;
      12             : using Cqrs.Domain;
      13             : using Cqrs.Messages;
      14             : 
      15             : namespace Cqrs.Events
      16             : {
      17             :         /// <summary>
      18             :         /// An <see cref="IEventStore{TAuthenticationToken}"/> that uses a local (non-static) <see cref="IDictionary{TKey,TValue}"/>.
      19             :         /// This does not manage memory in any way and will continue to grow. Mostly suitable for running tests or short lived processes.
      20             :         /// </summary>
      21             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      22             :         public class InProcessEventStore<TAuthenticationToken>
      23             :                 : IEventStore<TAuthenticationToken>
      24           1 :         {
      25             :                 /// <summary>
      26             :                 /// Gets or sets the in-memory storage <see cref="IDictionary{TKey,TValue}"/>.
      27             :                 /// </summary>
      28             :                 protected IDictionary<Guid, IList<IEvent<TAuthenticationToken>>> InMemoryDb { get; private set; }
      29             : 
      30             :                 /// <summary>
      31             :                 /// Instantiate a new instance of the <see cref="InProcessEventStore{TAuthenticationToken}"/> class.
      32             :                 /// </summary>
      33           1 :                 public InProcessEventStore()
      34             :                 {
      35             :                         InMemoryDb = new Dictionary<Guid, IList<IEvent<TAuthenticationToken>>>();
      36             :                 }
      37             : 
      38             :                 /// <summary>
      39             :                 /// Saves the provided <paramref name="event"/>.
      40             :                 /// </summary>
      41             :                 /// <param name="aggregateRootType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
      42             :                 /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to be saved.</param>
      43           1 :                 public virtual void Save(Type aggregateRootType, IEvent<TAuthenticationToken> @event)
      44             :                 {
      45             :                         IList<IEvent<TAuthenticationToken>> list;
      46             :                         InMemoryDb.TryGetValue(@event.GetIdentity(), out list);
      47             :                         if (list == null)
      48             :                         {
      49             :                                 list = new List<IEvent<TAuthenticationToken>>();
      50             :                                 InMemoryDb.Add(@event.GetIdentity(), list);
      51             :                         }
      52             :                         list.Add(@event);
      53             :                 }
      54             : 
      55             :                 /// <summary>
      56             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <typeparamref name="T">aggregate root</typeparamref> with the ID matching the provided <paramref name="aggregateId"/>.
      57             :                 /// </summary>
      58             :                 /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
      59             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
      60             :                 /// <param name="useLastEventOnly">Loads only the last event<see cref="IEvent{TAuthenticationToken}"/>.</param>
      61             :                 /// <param name="fromVersion">Load events starting from this version</param>
      62           1 :                 public virtual IEnumerable<IEvent<TAuthenticationToken>> Get<T>(Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
      63             :                 {
      64             :                         return Get(typeof(T), aggregateId, useLastEventOnly, fromVersion);
      65             :                 }
      66             : 
      67             :                 /// <summary>
      68             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <see cref="IAggregateRoot{TAuthenticationToken}"/> of type <paramref name="aggregateType"/> with the ID matching the provided <paramref name="aggregateId"/>.
      69             :                 /// </summary>
      70             :                 /// <param name="aggregateType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
      71             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
      72             :                 /// <param name="useLastEventOnly">Loads only the last event<see cref="IEvent{TAuthenticationToken}"/>.</param>
      73             :                 /// <param name="fromVersion">Load events starting from this version</param>
      74           1 :                 public virtual IEnumerable<IEvent<TAuthenticationToken>> Get(Type aggregateType, Guid aggregateId, bool useLastEventOnly = false, int fromVersion = -1)
      75             :                 {
      76             :                         IList<IEvent<TAuthenticationToken>> events;
      77             :                         InMemoryDb.TryGetValue(aggregateId, out events);
      78             :                         return events != null
      79             :                                 ? events.Where(x => x.Version > fromVersion)
      80             :                                 : new List<IEvent<TAuthenticationToken>>();
      81             :                 }
      82             : 
      83             :                 /// <summary>
      84             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <see cref="IAggregateRoot{TAuthenticationToken}"/> of type <paramref name="aggregateRootType"/> with the ID matching the provided <paramref name="aggregateId"/> up to and including the provided <paramref name="version"/>.
      85             :                 /// </summary>
      86             :                 /// <param name="aggregateRootType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
      87             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
      88             :                 /// <param name="version">Load events up-to and including from this version</param>
      89           1 :                 public virtual IEnumerable<IEvent<TAuthenticationToken>> GetToVersion(Type aggregateRootType, Guid aggregateId, int version)
      90             :                 {
      91             :                         IList<IEvent<TAuthenticationToken>> events;
      92             :                         InMemoryDb.TryGetValue(aggregateId, out events);
      93             :                         return events != null
      94             :                                 ? events.Where(x => x.Version <= version)
      95             :                                 : new List<IEvent<TAuthenticationToken>>();
      96             :                 }
      97             : 
      98             :                 /// <summary>
      99             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <typeparamref name="T">aggregate root</typeparamref> with the ID matching the provided <paramref name="aggregateId"/> up to and including the provided <paramref name="version"/>.
     100             :                 /// </summary>
     101             :                 /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
     102             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
     103             :                 /// <param name="version">Load events up-to and including from this version</param>
     104           1 :                 public virtual IEnumerable<IEvent<TAuthenticationToken>> GetToVersion<T>(Guid aggregateId, int version)
     105             :                 {
     106             :                         return GetToVersion(typeof(T), aggregateId, version);
     107             :                 }
     108             : 
     109             :                 /// <summary>
     110             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <see cref="IAggregateRoot{TAuthenticationToken}"/> of type <paramref name="aggregateRootType"/> with the ID matching the provided <paramref name="aggregateId"/> up to and including the provided <paramref name="versionedDate"/>.
     111             :                 /// </summary>
     112             :                 /// <param name="aggregateRootType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
     113             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
     114             :                 /// <param name="versionedDate">Load events up-to and including from this <see cref="DateTime"/></param>
     115           1 :                 public virtual IEnumerable<IEvent<TAuthenticationToken>> GetToDate(Type aggregateRootType, Guid aggregateId, DateTime versionedDate)
     116             :                 {
     117             :                         IList<IEvent<TAuthenticationToken>> events;
     118             :                         InMemoryDb.TryGetValue(aggregateId, out events);
     119             :                         return events != null
     120             :                                 ? events.Where(x => x.TimeStamp <= versionedDate)
     121             :                                 : new List<IEvent<TAuthenticationToken>>();
     122             :                 }
     123             : 
     124             :                 /// <summary>
     125             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <typeparamref name="T">aggregate root</typeparamref> with the ID matching the provided <paramref name="aggregateId"/> up to and including the provided <paramref name="versionedDate"/>.
     126             :                 /// </summary>
     127             :                 /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
     128             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
     129             :                 /// <param name="versionedDate">Load events up-to and including from this <see cref="DateTime"/></param>
     130           1 :                 public virtual IEnumerable<IEvent<TAuthenticationToken>> GetToDate<T>(Guid aggregateId, DateTime versionedDate)
     131             :                 {
     132             :                         return GetToDate(typeof(T), aggregateId, versionedDate);
     133             :                 }
     134             : 
     135             :                 /// <summary>
     136             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <see cref="IAggregateRoot{TAuthenticationToken}"/> of type <paramref name="aggregateRootType"/> with the ID matching the provided <paramref name="aggregateId"/> from and including the provided <paramref name="fromVersionedDate"/> up to and including the provided <paramref name="toVersionedDate"/>.
     137             :                 /// </summary>
     138             :                 /// <param name="aggregateRootType"> <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</param>
     139             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
     140             :                 /// <param name="fromVersionedDate">Load events from and including from this <see cref="DateTime"/></param>
     141             :                 /// <param name="toVersionedDate">Load events up-to and including from this <see cref="DateTime"/></param>
     142           1 :                 public IEnumerable<IEvent<TAuthenticationToken>> GetBetweenDates(Type aggregateRootType, Guid aggregateId, DateTime fromVersionedDate,
     143             :                         DateTime toVersionedDate)
     144             :                 {
     145             :                         IList<IEvent<TAuthenticationToken>> events;
     146             :                         InMemoryDb.TryGetValue(aggregateId, out events);
     147             :                         return events != null
     148             :                                 ? events.Where(eventData => eventData.TimeStamp >= fromVersionedDate && eventData.TimeStamp <= toVersionedDate)
     149             :                                 : new List<IEvent<TAuthenticationToken>>();
     150             :                 }
     151             : 
     152             :                 /// <summary>
     153             :                 /// Gets a collection of <see cref="IEvent{TAuthenticationToken}"/> for the <typeparamref name="T">aggregate root</typeparamref> with the ID matching the provided <paramref name="aggregateId"/> from and including the provided <paramref name="fromVersionedDate"/> up to and including the provided <paramref name="toVersionedDate"/>.
     154             :                 /// </summary>
     155             :                 /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
     156             :                 /// <param name="aggregateId">The <see cref="IAggregateRoot{TAuthenticationToken}.Id"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</param>
     157             :                 /// <param name="fromVersionedDate">Load events from and including from this <see cref="DateTime"/></param>
     158             :                 /// <param name="toVersionedDate">Load events up-to and including from this <see cref="DateTime"/></param>
     159           1 :                 public IEnumerable<IEvent<TAuthenticationToken>> GetBetweenDates<T>(Guid aggregateId, DateTime fromVersionedDate, DateTime toVersionedDate)
     160             :                 {
     161             :                         return GetBetweenDates(typeof(T), aggregateId, fromVersionedDate, toVersionedDate);
     162             :                 }
     163             : 
     164             :                 /// <summary>
     165             :                 /// Get all <see cref="IEvent{TAuthenticationToken}"/> instances for the given <paramref name="correlationId"/>.
     166             :                 /// </summary>
     167             :                 /// <param name="correlationId">The <see cref="IMessage.CorrelationId"/> of the <see cref="IEvent{TAuthenticationToken}"/> instances to retrieve.</param>
     168           1 :                 public virtual IEnumerable<EventData> Get(Guid correlationId)
     169             :                 {
     170             :                         return Enumerable.Empty<EventData>();
     171             :                 }
     172             : 
     173             :                 /// <summary>
     174             :                 /// Saves the provided <paramref name="event"/>.
     175             :                 /// </summary>
     176             :                 /// <typeparam name="T">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/> the <see cref="IEvent{TAuthenticationToken}"/> was raised in.</typeparam>
     177             :                 /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> to be saved.</param>
     178           1 :                 public virtual void Save<T>(IEvent<TAuthenticationToken> @event)
     179             :                 {
     180             :                         Save(typeof(T), @event);
     181             :                 }
     182             :         }
     183             : }

Generated by: LCOV version 1.12