-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathEventItem.cs
111 lines (94 loc) · 3.07 KB
/
EventItem.cs
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
// Copyright (c) David Pine. All rights reserved.
// Licensed under the MIT License.
using Microsoft.Azure.CosmosEventSourcing.Converters;
using Microsoft.Azure.CosmosEventSourcing.Events;
using Microsoft.Azure.CosmosEventSourcing.Stores;
using Microsoft.Azure.CosmosRepository;
using Newtonsoft.Json;
namespace Microsoft.Azure.CosmosEventSourcing.Items;
/// <summary>
/// A record the represents an event stored in an <see cref="IEventStore{TEventItem}"/>
/// </summary>
public abstract class EventItem : IItemWithEtag, IItemWithTimeToLive
{
[JsonProperty("ttl", NullValueHandling = NullValueHandling.Ignore)]
private int? _timeToLive;
private DomainEvent _domainEvent = null!;
private string? _etag;
/// <summary>
/// The payload of the event to be stored.
/// </summary>
[JsonConverter(typeof(DomainEventConverter))]
public DomainEvent DomainEvent
{
get
{
if (_domainEvent is AtomicEvent atomicEvent && string.IsNullOrWhiteSpace(atomicEvent.ETag))
{
// Use the ETag from the Item to pass back with the domain event to the aggregate.
_domainEvent = atomicEvent with
{
ETag = Etag ?? string.Empty,
EventId = Id,
};
}
return _domainEvent;
}
init
{
Id = value.EventId;
EventName = value.EventName;
_domainEvent = value;
}
}
/// <inheritdoc />
public string Id { get; set; }
/// <inheritdoc />
public string Type { get; set; }
/// <summary>
/// The value used to partition the event.
/// </summary>
public string PartitionKey { get; set; } = null!;
/// <summary>
/// The values used to partition the event.
/// </summary>
public IEnumerable<string> PartitionKeys { get; set; } = null!;
/// <summary>
/// The name of the event stored.
/// </summary>
public string EventName { get; set; } = null!;
/// <inheritdoc />
[JsonProperty("_etag")]
public string? Etag
{
get
{
if (_etag is null && _domainEvent is AtomicEvent atomicEvent && !string.IsNullOrWhiteSpace(atomicEvent.ETag))
{
// If the item is being created to be saved back then use the ETag from the AtomicEvent.
return atomicEvent.ETag;
}
return _etag;
}
private set => _etag = value;
}
/// <inheritdoc />
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public TimeSpan? TimeToLive
{
get => _timeToLive.HasValue ? TimeSpan.FromSeconds(_timeToLive.Value) : null;
set => _timeToLive = (int?)value?.TotalSeconds;
}
/// <summary>
/// An ID that correlates a request/scope with an event.
/// </summary>
public string? CorrelationId { get; set; }
/// <summary>
/// Creates an <see cref="EventItem"/>
/// </summary>
protected EventItem()
{
Type = GetType().Name;
Id = Guid.NewGuid().ToString();
}
}