-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApiResponseWithStateless.cs
More file actions
30 lines (26 loc) · 1.07 KB
/
ApiResponseWithStateless.cs
File metadata and controls
30 lines (26 loc) · 1.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
namespace Thinktecture.Unions;
/// <summary>
/// API response union demonstrating stateless types for memory optimization.
/// NotFound and Unauthorized are stateless types that carry no instance data.
/// </summary>
[Union<SuccessResponse, NotFound, Unauthorized>(
T1Name = "Success",
T2Name = "NotFound", T2IsStateless = true,
T3Name = "Unauthorized", T3IsStateless = true)]
public partial class ApiResponseWithStateless;
public sealed class SuccessResponse
{
public required string Data { get; init; }
}
/// <summary>
/// Stateless type for "not found" state.
/// No backing field is allocated - only the discriminator index is stored.
/// Using a struct avoids null-handling complexity since default(NotFound) is a valid value.
/// </summary>
public readonly record struct NotFound;
/// <summary>
/// Stateless type for "unauthorized" state.
/// No backing field is allocated - only the discriminator index is stored.
/// Using a struct avoids null-handling complexity since default(Unauthorized) is a valid value.
/// </summary>
public readonly record struct Unauthorized;