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