-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommand.cs
More file actions
98 lines (82 loc) · 2.91 KB
/
Command.cs
File metadata and controls
98 lines (82 loc) · 2.91 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
using System;
using System.Diagnostics;
using System.Text.Json.Serialization;
namespace ManagedCode.Communication.Commands;
[Serializable]
[DebuggerDisplay("CommandId: {CommandId}")]
public partial class Command : ICommand
{
[JsonConstructor]
protected Command()
{
CommandType = string.Empty;
}
protected Command(Guid commandId, string commandType)
{
CommandId = commandId;
CommandType = commandType;
Timestamp = DateTimeOffset.UtcNow;
}
[JsonPropertyName("commandId")]
[JsonPropertyOrder(1)]
public Guid CommandId { get; set; }
[JsonPropertyName("commandType")]
[JsonPropertyOrder(2)]
public string CommandType { get; set; }
[JsonPropertyName("timestamp")]
[JsonPropertyOrder(3)]
public DateTimeOffset Timestamp { get; set; } = DateTimeOffset.UtcNow;
[JsonPropertyName("correlationId")]
[JsonPropertyOrder(4)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? CorrelationId { get; set; }
[JsonPropertyName("causationId")]
[JsonPropertyOrder(5)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? CausationId { get; set; }
[JsonPropertyName("traceId")]
[JsonPropertyOrder(6)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? TraceId { get; set; }
[JsonPropertyName("spanId")]
[JsonPropertyOrder(7)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? SpanId { get; set; }
[JsonPropertyName("userId")]
[JsonPropertyOrder(8)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? UserId { get; set; }
[JsonPropertyName("sessionId")]
[JsonPropertyOrder(9)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? SessionId { get; set; }
[JsonPropertyName("metadata")]
[JsonPropertyOrder(10)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public CommandMetadata? Metadata { get; set; }
/// <summary>
/// Creates a new command with generated ID and specified type
/// </summary>
public static Command Create(string commandType)
{
return new Command(Guid.NewGuid(), commandType);
}
/// <summary>
/// Creates a new command with specific ID and type
/// </summary>
public static Command Create(Guid commandId, string commandType)
{
return new Command(commandId, commandType);
}
/// <summary>
/// Try to convert CommandType string to an enum value
/// </summary>
public Result<TEnum> GetCommandTypeAsEnum<TEnum>() where TEnum : struct, Enum
{
if (Enum.TryParse<TEnum>(CommandType, true, out var result))
{
return Result<TEnum>.Succeed(result);
}
return Result<TEnum>.Fail("InvalidCommandType", $"Cannot convert '{CommandType}' to enum {typeof(TEnum).Name}");
}
}