Repository navigation
Expand file tree
/
Copy pathEventData.Infra.cs
More file actions
211 lines (185 loc) · 13.3 KB
/
Copy pathEventData.Infra.cs
File metadata and controls
211 lines (185 loc) · 13.3 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
namespace CoreEx.Events;
public partial class EventData
{
#region CreateEvent
/// <summary>
/// Creates a new event-oriented <see cref="EventData"/> instance with the specified <paramref name="entity"/> (subject) and <paramref name="action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <param name="entity">The entity (subject) name.</param>
/// <param name="action">The action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
public static EventData CreateEvent(string entity, string? action) => new EventData { Action = action }.WithEntity(entity);
/// <summary>
/// Creates a new event-oriented <see cref="EventData"/> instance with the specified <paramref name="entity"/> (subject) and the <paramref name="action"/> as the <see cref="Action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <param name="entity">The entity (subject) name.</param>
/// <param name="action">The <see cref="Enum"/> value that represents the action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
public static EventData CreateEvent(string entity, Enum action) => new EventData().WithEntity(entity).WithAction(action);
/// <summary>
/// Creates a new event-oriented <see cref="EventData"/> instance with the specified <typeparamref name="TEntity"/> (subject) and <paramref name="action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <typeparam name="TEntity">The entity type.</typeparam>
/// <param name="action">The action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
public static EventData CreateEvent<TEntity>(string? action) => new EventData { Action = action }.WithEntity<TEntity>();
/// <summary>
/// Creates a new event-oriented <see cref="EventData"/> instance with the specified <typeparamref name="TEntity"/> (subject) and the <paramref name="action"/> as the <see cref="Action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <typeparam name="TEntity">The entity type.</typeparam>
/// <param name="action">The <see cref="Enum"/> value that represents the action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
public static EventData CreateEvent<TEntity>(Enum action) => new EventData().WithEntity<TEntity>().WithAction(action);
/// <summary>
/// Creates a new event-oriented <see cref="EventData"/> instance <see cref="WithValue{T}(T, IEnumerable{string})">with</see> the specified <paramref name="value"/> and <paramref name="action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="value">The value.</param>
/// <param name="action">The action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <see cref="Entity"/> (subject) automatically defaults from the value's <see cref="SchemaAttribute.Name"/> or <see cref="Type"/> name.</remarks>
public static EventData CreateEventWith<T>(T? value, string? action)
{
var ed = new EventData().WithValue(value);
return action is null ? ed : ed.WithAction(action);
}
/// <summary>
/// Creates a new event-oriented <see cref="EventData"/> instance <see cref="WithValue{T}(T, IEnumerable{string})">with</see> the specified <paramref name="value"/> and the <paramref name="action"/> as the <see cref="Action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="value">The value.</param>
/// <param name="action">The <see cref="Enum"/> value that represents the action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <see cref="Entity"/> (subject) automatically defaults from the value's <see cref="SchemaAttribute.Name"/> or <see cref="Type"/> name.</remarks>
public static EventData CreateEventWith<T>(T? value, Enum action) => new EventData().WithValue(value).WithAction(action);
/// <summary>
/// Creates new event-oriented <see cref="EventData"/> instances <see cref="WithValue{T}(T, IEnumerable{string})">with</see> the specified <paramref name="values"/> and <paramref name="action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="values">The values.</param>
/// <param name="action">The action.</param>
/// <param name="configure">An optional action to configure each <see cref="EventData"/> instance.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <see cref="Entity"/> (subject) automatically defaults from the value's <see cref="SchemaAttribute.Name"/> or <see cref="Type"/> name.</remarks>
public static EventData[] CreateEventsWith<T>(IEnumerable<T?> values, string? action, Action<T, EventData>? configure = null)
{
var list = new List<EventData>();
foreach (var value in values)
{
if (value is not null)
{
var ed = new EventData().WithValue(value);
configure?.Invoke(value, ed);
list.Add(action is null ? ed : ed.WithAction(action));
}
}
return [.. list];
}
/// <summary>
/// Creates new event-oriented <see cref="EventData"/> instances <see cref="WithValue{T}(T, IEnumerable{string})">with</see> the specified <paramref name="values"/> and the <paramref name="action"/> as the <see cref="Action"/> (typically a verb describing an <i>event</i> in past tense).
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="values">The values.</param>
/// <param name="action">The <see cref="Enum"/> value that represents the action.</param>
/// <param name="configure">An optional action to configure each <see cref="EventData"/> instance.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <see cref="Entity"/> (subject) automatically defaults from the value's <see cref="SchemaAttribute.Name"/> or <see cref="Type"/> name.</remarks>
public static EventData[] CreateEventsWith<T>(IEnumerable<T?> values, Enum action, Action<T, EventData>? configure = null)
{
var list = new List<EventData>();
foreach (var value in values)
{
if (value is not null)
{
var ed = new EventData().WithValue(value);
configure?.Invoke(value, ed);
list.Add(action is null ? ed : ed.WithAction(action));
}
}
return [.. list];
}
#endregion
#region CreateCommand
/// <summary>
/// Creates a new command-oriented <see cref="EventData"/> instance with the specified <paramref name="entity"/> (subject) and <paramref name="command"/> <see cref="Action"/> being requested.
/// </summary>
/// <param name="targetDomainName">The target domain name.</param>
/// <param name="entity">The entity (subject) name.</param>
/// <param name="command">The command action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <paramref name="targetDomainName"/> represents the name of the domain that is the intended target of the command.</remarks>
public static EventData CreateCommand(string targetDomainName, string entity, string command) => new EventData { MessageType = MessageType.Command }.WithDomain(targetDomainName).WithEntity(entity).WithAction(command);
/// <summary>
/// Creates a new command-oriented <see cref="EventData"/> instance with the specified <paramref name="entity"/> (subject) and the <paramref name="command"/> <see cref="Action"/> being requested.
/// </summary>
/// <param name="targetDomainName">The target domain name.</param>
/// <param name="entity">The entity (subject) name.</param>
/// <param name="command">The <see cref="Enum"/> value that represents the command action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
public static EventData CreateCommand(string targetDomainName, string entity, Enum command) => new EventData { MessageType = MessageType.Command }.WithDomain(targetDomainName).WithEntity(entity).WithAction(command);
/// <summary>
/// Creates a new command-oriented <see cref="EventData"/> instance with the specified <typeparamref name="TEntity"/> (subject) and <paramref name="command"/> <see cref="Action"/> being requested.
/// </summary>
/// <typeparam name="TEntity">The entity type.</typeparam>
/// <param name="targetDomainName">The target domain name.</param>
/// <param name="command">The command action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <paramref name="targetDomainName"/> represents the name of the domain that is the intended target of the command.</remarks>
public static EventData CreateCommand<TEntity>(string targetDomainName, string command) => new EventData { MessageType = MessageType.Command }.WithDomain(targetDomainName).WithEntity<TEntity>().WithAction(command);
/// <summary>
/// Creates a new command-oriented <see cref="EventData"/> instance with the specified <typeparamref name="TEntity"/> (subject) and the <paramref name="command"/> <see cref="Action"/> being requested.
/// </summary>
/// <typeparam name="TEntity">The entity type.</typeparam>
/// <param name="targetDomainName">The target domain name.</param>
/// <param name="command">The <see cref="Enum"/> value that represents the command action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
public static EventData CreateCommand<TEntity>(string targetDomainName, Enum command) => new EventData { MessageType = MessageType.Command }.WithDomain(targetDomainName).WithEntity<TEntity>().WithAction(command);
/// <summary>
/// Creates a new command-oriented <see cref="EventData"/> instance <see cref="WithValue{T}(T, IEnumerable{string})">with</see> the specified <paramref name="value"/> and <paramref name="command"/> <see cref="Action"/> being requested.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="targetDomainName">The target domain name.</param>
/// <param name="value">The value.</param>
/// <param name="command">The command action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <see cref="Entity"/> (subject) automatically defaults from the value's <see cref="SchemaAttribute.Name"/> or <see cref="Type"/> name.</remarks>
public static EventData CreateCommandWith<T>(string targetDomainName, T value, string command) => new EventData { MessageType = MessageType.Command }.WithDomain(targetDomainName).WithValue(value).WithAction(command);
/// <summary>
/// Creates a new command-oriented <see cref="EventData"/> instance <see cref="WithValue{T}(T, IEnumerable{string})">with</see> the specified <paramref name="value"/> and the <paramref name="command"/> <see cref="Action"/> being requested.
/// </summary>
/// <typeparam name="T">The value <see cref="Type"/>.</typeparam>
/// <param name="targetDomainName">The target domain name.</param>
/// <param name="value">The value.</param>
/// <param name="command">The <see cref="Enum"/> value that represents the command action.</param>
/// <returns>The new <see cref="EventData"/>.</returns>
/// <remarks>The <see cref="Entity"/> (subject) automatically defaults from the value's <see cref="SchemaAttribute.Name"/> or <see cref="Type"/> name.</remarks>
public static EventData CreateCommandWith<T>(string targetDomainName, T value, Enum command) => new EventData { MessageType = MessageType.Command }.WithDomain(targetDomainName).WithValue(value).WithAction(command);
#endregion
#region ToObjectFromJson
/// <summary>
/// Converts (deserializes) the JSON-based <see cref="Data"/> to type of <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The resulting <see cref="Type"/>.</typeparam>
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
/// <returns>The resulting JSON deserialized value.</returns>
public T? ToObjectFromJson<T>(JsonSerializerOptions? jsonSerializerOptions = null)
{
if (Data is null || Data.IsEmpty)
return default;
return Data.ToObjectFromJson<T>(jsonSerializerOptions ?? JsonDefaults.SerializerOptions);
}
/// <summary>
/// Converts (deserializes) the JSON-based <see cref="Data"/> to type of <paramref name="type"/>.
/// </summary>
/// <param name="type">The resulting <see cref="Type"/>.</param>
/// <param name="jsonSerializerOptions">The optional <see cref="JsonSerializerOptions"/>.</param>
/// <returns>The resulting JSON deserialized value.</returns>
public object? ToObjectFromJson(Type type, JsonSerializerOptions? jsonSerializerOptions = null)
{
type.ThrowIfNull();
if (Data is null || Data.IsEmpty)
return default;
return JsonSerializer.Deserialize(Data, type, jsonSerializerOptions ?? JsonDefaults.SerializerOptions);
}
#endregion
}