-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessageAdapter.cs
More file actions
141 lines (131 loc) · 6.07 KB
/
MessageAdapter.cs
File metadata and controls
141 lines (131 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Concurrency;
using RapidField.SolidInstruments.Serialization;
using System;
using System.Diagnostics;
namespace RapidField.SolidInstruments.Messaging
{
/// <summary>
/// Facilitates conversion of general-format messages to and from implementation-specific messages.
/// </summary>
/// <typeparam name="TAdaptedMessage">
/// The type of implementation-specific adapted messages.
/// </typeparam>
/// <remarks>
/// <see cref="MessageAdapter{TAdaptedMessage}" /> is the default implementation of
/// <see cref="IMessageAdapter{TAdaptedMessage}" />.
/// </remarks>
public abstract class MessageAdapter<TAdaptedMessage> : IMessageAdapter<TAdaptedMessage>
where TAdaptedMessage : class
{
/// <summary>
/// Initializes a new instance of the <see cref="MessageAdapter{TAdaptedMessage}" /> class.
/// </summary>
protected MessageAdapter()
: this(DefaultMessageSerializationFormat)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="MessageAdapter{TAdaptedMessage}" /> class.
/// </summary>
/// <param name="messageSerializationFormat">
/// Gets the format that is used to serialize and deserialize messages. The default value is
/// <see cref="SerializationFormat.Binary" />.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="messageSerializationFormat" /> is equal to <see cref="SerializationFormat.Unspecified" />.
/// </exception>
protected MessageAdapter(SerializationFormat messageSerializationFormat)
{
MessageSerializationFormat = messageSerializationFormat.RejectIf().IsEqualToValue(SerializationFormat.Unspecified, nameof(messageSerializationFormat));
}
/// <summary>
/// Converts the specified general-format message to an implementation-specific message.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message.
/// </typeparam>
/// <param name="message">
/// A general-format message to convert.
/// </param>
/// <returns>
/// The implementation-specific message.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="message" /> is <see langword="null" />.
/// </exception>
public TAdaptedMessage ConvertForward<TMessage>(TMessage message)
where TMessage : class, IMessageBase => ConvertForward(message.RejectIf().IsNull(nameof(message)), new DynamicSerializer<TMessage>(MessageSerializationFormat));
/// <summary>
/// Converts the specified implementation-specific message to a general-format message.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message.
/// </typeparam>
/// <param name="message">
/// An implementation-specific message to convert.
/// </param>
/// <returns>
/// The general-format message.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="message" /> is <see langword="null" />.
/// </exception>
public TMessage ConvertReverse<TMessage>(TAdaptedMessage message)
where TMessage : class, IMessageBase => ConvertReverse(message.RejectIf().IsNull(nameof(message)), new DynamicSerializer<TMessage>(MessageSerializationFormat));
/// <summary>
/// Converts the specified general-format message to an implementation-specific message.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message.
/// </typeparam>
/// <param name="message">
/// A general-format message to convert.
/// </param>
/// <param name="serializer">
/// A serializer that is used to serialize the message.
/// </param>
/// <returns>
/// The implementation-specific message.
/// </returns>
protected abstract TAdaptedMessage ConvertForward<TMessage>(TMessage message, ISerializer<TMessage> serializer)
where TMessage : class, IMessageBase;
/// <summary>
/// Converts the specified implementation-specific message to a general-format message.
/// </summary>
/// <typeparam name="TMessage">
/// The type of the message.
/// </typeparam>
/// <param name="message">
/// An implementation-specific message to convert.
/// </param>
/// <param name="serializer">
/// A serializer that is used to serialize the message.
/// </param>
/// <returns>
/// The general-format message.
/// </returns>
protected abstract TMessage ConvertReverse<TMessage>(TAdaptedMessage message, ISerializer<TMessage> serializer)
where TMessage : class, IMessageBase;
/// <summary>
/// Gets the type of implementation-specific adapted messages.
/// </summary>
public Type AdaptedMessageType => typeof(TAdaptedMessage);
/// <summary>
/// Gets the format that is used to serialize and deserialize messages.
/// </summary>
public SerializationFormat MessageSerializationFormat
{
get;
}
/// <summary>
/// Represents the default format that is used to serialize and deserialize messages.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const SerializationFormat DefaultMessageSerializationFormat = SerializationFormat.Binary;
}
}