Repository navigation
Expand file tree
/
Copy pathCommandT.From.cs
More file actions
88 lines (76 loc) · 3.53 KB
/
Copy pathCommandT.From.cs
File metadata and controls
88 lines (76 loc) · 3.53 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
using System;
namespace ManagedCode.Communication.Commands;
public partial class Command<T>
{
/// <summary>
/// Creates a command carrying <paramref name="value" />, naming it after the payload's runtime type.
/// </summary>
/// <param name="value">The payload.</param>
/// <param name="commandId">
/// Identity of the command. Leave it unset — a time-ordered UUIDv7 is generated. Supply one only when the
/// identity comes from outside: an idempotency key sent by the caller, or a replayed message.
/// </param>
public static Command<T> Create(T value, Guid? commandId = null)
{
return Create(ResolveCommandType(value), value, commandId);
}
/// <summary>
/// Creates a command of the given type carrying <paramref name="value" />.
/// </summary>
/// <param name="commandType">Logical command type.</param>
/// <param name="value">The payload.</param>
/// <param name="commandId">
/// Identity of the command. Leave it unset — a time-ordered UUIDv7 is generated. Supply one only when the
/// identity comes from outside: an idempotency key sent by the caller, or a replayed message.
/// </param>
public static Command<T> Create(string commandType, T value, Guid? commandId = null)
{
if (string.IsNullOrWhiteSpace(commandType))
{
throw new ArgumentException("Command type must be provided.", nameof(commandType));
}
return new Command<T>(commandId ?? Guid.CreateVersion7(), commandType, value);
}
/// <summary>
/// Creates a command of the given type, obtaining the payload from <paramref name="valueFactory" />.
/// </summary>
/// <param name="commandType">Logical command type.</param>
/// <param name="valueFactory">Produces the payload.</param>
/// <param name="commandId">
/// Identity of the command. Leave it unset — a time-ordered UUIDv7 is generated. Supply one only when the
/// identity comes from outside: an idempotency key sent by the caller, or a replayed message.
/// </param>
public static Command<T> Create(string commandType, Func<T> valueFactory, Guid? commandId = null)
{
ArgumentNullException.ThrowIfNull(valueFactory);
return Create(commandType, valueFactory(), commandId);
}
/// <summary>
/// Creates a command from <paramref name="valueFactory" />, naming it after the payload's runtime type.
/// </summary>
/// <param name="valueFactory">Produces the payload.</param>
/// <param name="commandId">
/// Identity of the command. Leave it unset — a time-ordered UUIDv7 is generated. Supply one only when the
/// identity comes from outside: an idempotency key sent by the caller, or a replayed message.
/// </param>
public static Command<T> Create(Func<T> valueFactory, Guid? commandId = null)
{
ArgumentNullException.ThrowIfNull(valueFactory);
return Create(valueFactory(), commandId);
}
// From(...) mirrors the naming used by Result.From and Command.From.
/// <inheritdoc cref="Create(T,Guid?)" />
public static Command<T> From(T value, Guid? commandId = null)
{
return Create(value, commandId);
}
/// <inheritdoc cref="Create(string,T,Guid?)" />
public static Command<T> From(string commandType, T value, Guid? commandId = null)
{
return Create(commandType, value, commandId);
}
private static string ResolveCommandType(T value)
{
return value?.GetType().Name ?? typeof(T).Name;
}
}