Documentation Coverage Report
Current view: top level - Cqrs.EventStore - ProjectionReader.cs Hit Total Coverage
Version: 2.2 Artefacts: 4 4 100.0 %
Date: 2017-09-22

          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 System.Text;
      13             : using EventStore.ClientAPI;
      14             : using Newtonsoft.Json;
      15             : 
      16             : namespace Cqrs.EventStore
      17             : {
      18             :         /// <summary>
      19             :         /// Reads projection streams from a Greg Young's Event sTore.
      20             :         /// </summary>
      21             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      22             :         public abstract class ProjectionReader<TAuthenticationToken>
      23           1 :         {
      24             :                 /// <summary>
      25             :                 /// The <see cref="IEventStoreConnection"/> used to read and write streams in the Greg Young Event Store.
      26             :                 /// </summary>
      27             :                 protected IEventStoreConnectionHelper EventStoreConnectionHelper { get; set; }
      28             : 
      29             :                 /// <summary>
      30             :                 /// The <see cref="IEventDeserialiser{TAuthenticationToken}"/> used to deserialise events.
      31             :                 /// </summary>
      32             :                 protected IEventDeserialiser<TAuthenticationToken> EventDeserialiser { get; set; }
      33             : 
      34             :                 /// <summary>
      35             :                 /// Instantiates a new instance of <see cref="ProjectionReader{TAuthenticationToken}"/>.
      36             :                 /// </summary>
      37           1 :                 protected ProjectionReader(IEventStoreConnectionHelper eventStoreConnectionHelper, IEventDeserialiser<TAuthenticationToken> eventDeserialiser)
      38             :                 {
      39             :                         EventStoreConnectionHelper = eventStoreConnectionHelper;
      40             :                         EventDeserialiser = eventDeserialiser;
      41             :                 }
      42             : 
      43             :                 /// <summary>
      44             :                 /// Get a collection of data objects from a stream with the provided <paramref name="streamName"/>.
      45             :                 /// </summary>
      46             :                 /// <param name="streamName">The name of the stream to read events from.</param>
      47           1 :                 protected IEnumerable<dynamic> GetDataByStreamName(string streamName)
      48             :                 {
      49             :                         StreamEventsSlice eventCollection;
      50             :                         using (IEventStoreConnection connection = EventStoreConnectionHelper.GetEventStoreConnection())
      51             :                         {
      52             :                                 eventCollection = connection.ReadStreamEventsBackwardAsync(streamName, StreamPosition.End, 1, false).Result;
      53             :                         }
      54             :                         var jsonSerialiserSettings = EventDeserialiser.GetSerialisationSettings();
      55             :                         var encoder = new UTF8Encoding();
      56             :                         return
      57             :                         (
      58             :                                 (
      59             :                                         (IEnumerable<dynamic>)eventCollection.Events
      60             :                                         .Select(e => JsonConvert.DeserializeObject(((dynamic)encoder.GetString(e.Event.Data)), jsonSerialiserSettings))
      61             :                                         .SingleOrDefault()
      62             :                                 )
      63             :                                         ??
      64             :                                 (
      65             :                                         Enumerable.Empty<dynamic>()
      66             :                                 )
      67             :                         )
      68             :                         .Select(x => x.Value);
      69             :                 }
      70             : 
      71             :                 /// <summary>
      72             :                 /// Get a collection of <typeparamref name="TData"/> from a stream with the provided <paramref name="streamName"/>.
      73             :                 /// </summary>
      74             :                 /// <param name="streamName">The name of the stream to read events from.</param>
      75           1 :                 protected IEnumerable<TData> GetDataByStreamName<TData>(string streamName)
      76             :                 {
      77             :                         IList<TData> data = GetDataByStreamName(streamName)
      78             :                                 .Select(e => JsonConvert.DeserializeObject<TData>(e.ToString()))
      79             :                                 .Cast<TData>()
      80             :                                 .ToList();
      81             :                         return data;
      82             :                 }
      83             :         }
      84             : }

Generated by: LCOV version 1.10