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 Cqrs.Commands;
11 : using Cqrs.Events;
12 : using Newtonsoft.Json;
13 :
14 : namespace Cqrs.Azure.ServiceBus
15 : {
16 : /// <summary>
17 : /// Serialises <see cref="IEvent{TAuthenticationToken}">events</see> and <see cref="ICommand{TAuthenticationToken}">commands</see>.
18 : /// </summary>
19 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
20 : public class MessageSerialiser<TAuthenticationToken> : IMessageSerialiser<TAuthenticationToken>
21 1 : {
22 : /// <summary>
23 : /// The default <see cref="JsonSerializerSettings"/> to use.
24 : /// </summary>
25 : public static JsonSerializerSettings DefaultSettings { get; private set; }
26 :
27 : static MessageSerialiser()
28 : {
29 : DefaultSettings = DefaultJsonSerializerSettings.DefaultSettings;
30 : }
31 :
32 : /// <summary>
33 : /// Serialise the provided <paramref name="event"/>.
34 : /// </summary>
35 : /// <typeparam name="TEvent">The <see cref="Type"/> of the <see cref="IEvent{TAuthenticationToken}"/> being serialised.</typeparam>
36 : /// <param name="event">The <see cref="IEvent{TAuthenticationToken}"/> being serialised.</param>
37 : /// <returns>A <see cref="string"/> representation of the provided <paramref name="event"/>.</returns>
38 1 : public virtual string SerialiseEvent<TEvent>(TEvent @event)
39 : where TEvent : IEvent<TAuthenticationToken>
40 : {
41 : return JsonConvert.SerializeObject(@event, GetSerialisationSettings());
42 : }
43 :
44 : /// <summary>
45 : /// Serialise the provided <paramref name="command"/>.
46 : /// </summary>
47 : /// <typeparam name="TCommand">The <see cref="Type"/> of the <see cref="ICommand{TAuthenticationToken}"/> being serialised.</typeparam>
48 : /// <param name="command">The <see cref="ICommand{TAuthenticationToken}"/> being serialised.</param>
49 : /// <returns>A <see cref="string"/> representation of the provided <paramref name="command"/>.</returns>
50 1 : public virtual string SerialiseCommand<TCommand>(TCommand command) where TCommand : ICommand<TAuthenticationToken>
51 : {
52 : return JsonConvert.SerializeObject(command, GetSerialisationSettings());
53 : }
54 :
55 : /// <summary>
56 : /// Deserialise the provided <paramref name="event"/> from its <see cref="string"/> representation.
57 : /// </summary>
58 : /// <param name="event">A <see cref="string"/> representation of an <see cref="IEvent{TAuthenticationToken}"/> to deserialise.</param>
59 1 : public virtual IEvent<TAuthenticationToken> DeserialiseEvent(string @event)
60 : {
61 : return JsonConvert.DeserializeObject<IEvent<TAuthenticationToken>>(@event, GetSerialisationSettings());
62 : }
63 :
64 : /// <summary>
65 : /// Deserialise the provided <paramref name="command"/> from its <see cref="string"/> representation.
66 : /// </summary>
67 : /// <param name="command">A <see cref="string"/> representation of an <see cref="ICommand{TAuthenticationToken}"/> to deserialise.</param>
68 1 : public virtual ICommand<TAuthenticationToken> DeserialiseCommand(string command)
69 : {
70 : return JsonConvert.DeserializeObject<ICommand<TAuthenticationToken>>(command, GetSerialisationSettings());
71 : }
72 :
73 : /// <summary>
74 : /// Returns <see cref="DefaultSettings"/>
75 : /// </summary>
76 : /// <returns><see cref="DefaultSettings"/></returns>
77 1 : protected virtual JsonSerializerSettings GetSerialisationSettings()
78 : {
79 : return DefaultSettings;
80 : }
81 : }
82 : }
|