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 : /// <summary>
55 : /// Gets the <see cref="JsonSerializerSettings"/> used while Deserialising.
56 : /// </summary>
57 1 : public JsonSerializerSettings GetSerialisationSettings()
58 : {
59 : return new JsonSerializerSettings
60 : {
61 : Formatting = Formatting.None,
62 : MissingMemberHandling = MissingMemberHandling.Ignore,
63 : DateParseHandling = DateParseHandling.DateTimeOffset,
64 : DateTimeZoneHandling = DateTimeZoneHandling.Utc,
65 : Converters = new List<JsonConverter> { new StringEnumConverter() },
66 : };
67 : }
68 :
69 : #endregion
70 :
71 : #region Implementation of IEventBuilder
72 :
73 : /// <summary>
74 : /// Create an <see cref="EventData">framework event</see> with the provided <paramref name="eventData"/>.
75 : /// </summary>
76 : /// <param name="type">The name of the <see cref="Type"/> of the target object the serialised data is.</param>
77 : /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to add to the <see cref="EventData"/>.</param>
78 1 : public EventData CreateFrameworkEvent(string type, IEvent<TAuthenticationToken> eventData)
79 : {
80 : JsonSerializerSettings jsonSerialiserSettings = GetSerialisationSettings();
81 :
82 : return new EventData
83 : (
84 : Guid.NewGuid(),
85 : type,
86 : true,
87 : new UTF8Encoding().GetBytes(JsonConvert.SerializeObject(eventData, jsonSerialiserSettings)),
88 : new byte[0]
89 : );
90 : }
91 :
92 : /// <summary>
93 : /// Create an <see cref="EventData">framework event</see> with the provided <paramref name="eventData"/>.
94 : /// </summary>
95 : /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to add to the <see cref="EventData"/>.</param>
96 1 : public EventData CreateFrameworkEvent(IEvent<TAuthenticationToken> eventData)
97 : {
98 : JsonSerializerSettings jsonSerialiserSettings = GetSerialisationSettings();
99 :
100 : return new EventData
101 : (
102 : Guid.NewGuid(),
103 : eventData.GetType().AssemblyQualifiedName,
104 : true,
105 : new UTF8Encoding().GetBytes(JsonConvert.SerializeObject(eventData, jsonSerialiserSettings)),
106 : new byte[0]
107 : );
108 : }
109 :
110 : /// <summary>
111 : /// Create an <see cref="EventData">framework event</see> from the provided <paramref name="eventDataBody"/>.
112 : /// </summary>
113 : /// <param name="eventDataBody">A JSON string of serialised data.</param>
114 1 : public EventData CreateFrameworkEvent(string eventDataBody)
115 : {
116 : return CreateFrameworkEvent
117 : (
118 : new SimpleEvent<TAuthenticationToken> { Id = Guid.NewGuid(), Message = eventDataBody, TimeStamp = DateTimeOffset.Now, Version = 1 }
119 : );
120 : }
121 :
122 : /// <summary>
123 : /// Create an <see cref="EventData">framework event</see> from the provided <paramref name="eventDataBody"/>.
124 : /// </summary>
125 : /// <param name="type">The name of the <see cref="Type"/> of the target object the serialised data is.</param>
126 : /// <param name="eventDataBody">A JSON string of serialised data.</param>
127 1 : public EventData CreateFrameworkEvent(string type, string eventDataBody)
128 : {
129 : return CreateFrameworkEvent
130 : (
131 : type,
132 : new SimpleEvent<TAuthenticationToken> { Id = Guid.NewGuid(), Message = eventDataBody, TimeStamp = DateTimeOffset.Now, Version = 1 }
133 : );
134 : }
135 :
136 : /// <summary>
137 : /// Create an <see cref="EventData"/> that notifies people a client has connected.
138 : /// </summary>
139 : /// <param name="clientName">The name of the client that has connected.</param>
140 1 : public EventData CreateClientConnectedEvent(string clientName)
141 : {
142 : return CreateFrameworkEvent
143 : (
144 : "Client Connected",
145 : string.Format("{0} has connected.", clientName)
146 : );
147 : }
148 :
149 : #endregion
150 : }
151 : }
|