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.Text;
11 :
12 : namespace Cqrs.Events
13 : {
14 : /// <summary>
15 : /// Builds <see cref="EventData"/> from various input formats.
16 : /// </summary>
17 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
18 : public abstract class EventBuilder<TAuthenticationToken> : IEventBuilder<TAuthenticationToken>
19 1 : {
20 : #region Implementation of IEventBuilder<TAuthenticationToken>
21 :
22 : /// <summary>
23 : /// Create an <see cref="EventData">framework event</see> with the provided <paramref name="eventData"/>.
24 : /// </summary>
25 : /// <param name="type">The name of the <see cref="Type"/> of the target object the serialised data is.</param>
26 : /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to add to the <see cref="EventData"/>.</param>
27 1 : public virtual EventData CreateFrameworkEvent(string type, IEvent<TAuthenticationToken> eventData)
28 : {
29 : return new EventData
30 : {
31 : EventId = Guid.NewGuid(),
32 : EventType = type,
33 : Data = SerialiseEventDataToString(eventData)
34 : };
35 : }
36 :
37 : /// <summary>
38 : /// Create an <see cref="EventData">framework event</see> with the provided <paramref name="eventData"/>.
39 : /// </summary>
40 : /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to add to the <see cref="EventData"/>.</param>
41 1 : public virtual EventData CreateFrameworkEvent(IEvent<TAuthenticationToken> eventData)
42 : {
43 : return CreateFrameworkEvent(eventData.GetType().AssemblyQualifiedName, eventData);
44 : }
45 :
46 : #endregion
47 :
48 : /// <summary>
49 : /// Serialise the provided <paramref name="eventData"/> into a <see cref="byte"/> <see cref="Array"/>.
50 : /// </summary>
51 : /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to serialise.</param>
52 1 : protected virtual byte[] SerialiseEventData(IEvent<TAuthenticationToken> eventData)
53 : {
54 : return new UTF8Encoding().GetBytes(SerialiseEventDataToString(eventData));
55 : }
56 :
57 : /// <summary>
58 : /// Serialise the provided <paramref name="eventData"/> into a <see cref="string"/>.
59 : /// </summary>
60 : /// <param name="eventData">The <see cref="IEvent{TAuthenticationToken}"/> to serialise.</param>
61 1 : protected abstract string SerialiseEventDataToString(IEvent<TAuthenticationToken> eventData);
62 : }
63 : }
|