Documentation Coverage Report
Current view: top level - Cqrs.EventStore - EventFactory.cs Hit Total Coverage
Version: 4.0 Artefacts: 9 9 100.0 %
Date: 2019-11-24 03:15:41

          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.Text;
      12             : using Cqrs.Events;
      13             : using EventStore.ClientAPI;
      14             : using Newtonsoft.Json;
      15             : using Newtonsoft.Json.Converters;
      16             : using EventData=EventStore.ClientAPI.EventData;
      17             : 
      18             : namespace Cqrs.EventStore
      19             : {
      20             :         /// <summary>
      21             :         /// A factory implementing <see cref="IEventDeserialiser{TAuthenticationToken}"/> and <see cref="IEventBuilder{TAuthenticationToken}"/>
      22             :         /// </summary>
      23             :         /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
      24             :         public class EventFactory<TAuthenticationToken> : IEventBuilder<TAuthenticationToken>, IEventDeserialiser<TAuthenticationToken>
      25           1 :         {
      26             :                 #region Implementation of IEventDeserialiser
      27             : 
      28             :                 /// <summary>
      29             :                 /// Deserialise the provided <paramref name="eventData"/> into an <see cref="IEvent{TAuthenticationToken}"/>.
      30             :                 /// </summary>
      31             :                 /// <param name="eventData">The <see cref="RecordedEvent"/> to Deserialise.</param>
      32           1 :                 public IEvent<TAuthenticationToken> Deserialise(RecordedEvent eventData)
      33             :                 {
      34             :                         JsonSerializerSettings jsonSerialiserSettings = GetSerialisationSettings();
      35             : 
      36             :                         switch (eventData.EventType)
      37             :                         {
      38             :                                 case "Client Connected":
      39             :                                         return JsonConvert.DeserializeObject<SimpleEvent<TAuthenticationToken>>(new UTF8Encoding().GetString(eventData.Data), jsonSerialiserSettings);
      40             :                                 default:
      41             :                                         return (IEvent<TAuthenticationToken>)JsonConvert.DeserializeObject(new UTF8Encoding().GetString(eventData.Data), Type.GetType(eventData.EventType));
      42             :                         }
      43             :                 }
      44             : 
      45             :                 /// <summary>
      46             :                 /// Deserialise the provided <paramref name="notification"/> into an <see cref="IEvent{TAuthenticationToken}"/>.
      47             :                 /// </summary>
      48             :                 /// <param name="notification">The <see cref="ResolvedEvent"/> to Deserialise.</param>
      49           1 :                 public IEvent<TAuthenticationToken> Deserialise(ResolvedEvent notification)
      50             :                 {
      51             :                         return Deserialise(notification.Event);
      52             :                 }
      53             : 
      54             : #pragma warning disable CS0419 // Ambiguous reference in cref attribute
      55             :                                                           /// <summary>
      56             :                                                           /// Gets the <see cref="JsonSerializerSettings"/> used while Deserialising.
      57             :                                                           /// </summary>
      58             : #pragma warning restore CS0419 // Ambiguous reference in cref attribute
      59           1 :                 public JsonSerializerSettings GetSerialisationSettings()
      60             :                 {
      61             :                         return new JsonSerializerSettings
      62             :                         {
      63             :                                 Formatting = Formatting.None,
      64             :                                 MissingMemberHandling = MissingMemberHandling.Ignore,
      65             :                                 DateParseHandling = DateParseHandling.DateTimeOffset,
      66             :                                 DateTimeZoneHandling = DateTimeZoneHandling.Utc,
      67             :                                 Converters = new List<JsonConverter> { new StringEnumConverter() },
      68             :                         };
      69             :                 }
      70             : 
      71             :                 #endregion
      72             : 
      73             :                 #region Implementation of IEventBuilder
      74             : 
      75             :                 /// <summary>
      76             :                 /// Create an <see cref="EventData">framework event</see> with the provided <paramref name="eventData"/>.
      77             :                 /// </summary>
      78             :                 /// <param name="type">The name of the <see cref="Type"/> of the target object the serialised data is.</param>
      79             :                 /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to add to the <see cref="EventData"/>.</param>
      80           1 :                 public EventData CreateFrameworkEvent(string type, IEvent<TAuthenticationToken> eventData)
      81             :                 {
      82             :                         JsonSerializerSettings jsonSerialiserSettings = GetSerialisationSettings();
      83             : 
      84             :                         return new EventData
      85             :                         (
      86             :                                 Guid.NewGuid(),
      87             :                                 type,
      88             :                                 true,
      89             :                                 new UTF8Encoding().GetBytes(JsonConvert.SerializeObject(eventData, jsonSerialiserSettings)),
      90             :                                 new byte[0]
      91             :                         );
      92             :                 }
      93             : 
      94             :                 /// <summary>
      95             :                 /// Create an <see cref="EventData">framework event</see> with the provided <paramref name="eventData"/>.
      96             :                 /// </summary>
      97             :                 /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to add to the <see cref="EventData"/>.</param>
      98           1 :                 public EventData CreateFrameworkEvent(IEvent<TAuthenticationToken> eventData)
      99             :                 {
     100             :                         JsonSerializerSettings jsonSerialiserSettings = GetSerialisationSettings();
     101             : 
     102             :                         return new EventData
     103             :                         (
     104             :                                 Guid.NewGuid(),
     105             :                                 eventData.GetType().AssemblyQualifiedName,
     106             :                                 true,
     107             :                                 new UTF8Encoding().GetBytes(JsonConvert.SerializeObject(eventData, jsonSerialiserSettings)),
     108             :                                 new byte[0]
     109             :                         );
     110             :                 }
     111             : 
     112             :                 /// <summary>
     113             :                 /// Create an <see cref="EventData">framework event</see> from the provided <paramref name="eventDataBody"/>.
     114             :                 /// </summary>
     115             :                 /// <param name="eventDataBody">A JSON string of serialised data.</param>
     116           1 :                 public EventData CreateFrameworkEvent(string eventDataBody)
     117             :                 {
     118             :                         return CreateFrameworkEvent
     119             :                         (
     120             :                                 new SimpleEvent<TAuthenticationToken> { Id = Guid.NewGuid(), Message = eventDataBody, TimeStamp = DateTimeOffset.Now, Version = 1 }
     121             :                         );
     122             :                 }
     123             : 
     124             :                 /// <summary>
     125             :                 /// Create an <see cref="EventData">framework event</see> from the provided <paramref name="eventDataBody"/>.
     126             :                 /// </summary>
     127             :                 /// <param name="type">The name of the <see cref="Type"/> of the target object the serialised data is.</param>
     128             :                 /// <param name="eventDataBody">A JSON string of serialised data.</param>
     129           1 :                 public EventData CreateFrameworkEvent(string type, string eventDataBody)
     130             :                 {
     131             :                         return CreateFrameworkEvent
     132             :                         (
     133             :                                 type,
     134             :                                 new SimpleEvent<TAuthenticationToken> { Id = Guid.NewGuid(), Message = eventDataBody, TimeStamp = DateTimeOffset.Now, Version = 1 }
     135             :                         );
     136             :                 }
     137             : 
     138             :                 /// <summary>
     139             :                 /// Create an <see cref="EventData"/> that notifies people a client has connected.
     140             :                 /// </summary>
     141             :                 /// <param name="clientName">The name of the client that has connected.</param>
     142           1 :                 public EventData CreateClientConnectedEvent(string clientName)
     143             :                 {
     144             :                         return CreateFrameworkEvent
     145             :                         (
     146             :                                 "Client Connected",
     147             :                                 string.Format("{0} has connected.", clientName)
     148             :                         );
     149             :                 }
     150             : 
     151             :                 #endregion
     152             :         }
     153             : }

Generated by: LCOV version 1.13