-
-
Notifications
You must be signed in to change notification settings - Fork 518
/
CompositeIdsTests.cs
277 lines (216 loc) · 7.99 KB
/
CompositeIdsTests.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using FluentAssertions;
using Marten.Events.Projections;
using Marten.Integration.Tests.TestsInfrastructure;
using Marten.Schema;
using Newtonsoft.Json;
using Weasel.Core;
using Xunit;
namespace Marten.Integration.Tests.CompositeIds;
public class StronglyTypedValue<T>(T value): IEquatable<StronglyTypedValue<T>>
where T : IComparable<T>
{
public T Value { get; } = value;
public bool Equals(StronglyTypedValue<T>? other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return EqualityComparer<T>.Default.Equals(Value, other.Value);
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((StronglyTypedValue<T>)obj);
}
public override int GetHashCode() =>
EqualityComparer<T>.Default.GetHashCode(Value);
public static bool operator ==(StronglyTypedValue<T>? left, StronglyTypedValue<T>? right) =>
Equals(left, right);
public static bool operator !=(StronglyTypedValue<T>? left, StronglyTypedValue<T>? right) =>
!Equals(left, right);
}
public class ReservationId(Guid value): StronglyTypedValue<Guid>(value);
public class CustomerId(Guid value): StronglyTypedValue<Guid>(value);
public class SeatId(Guid value): StronglyTypedValue<Guid>(value);
public class ReservationNumber: StronglyTypedValue<string>
{
public ReservationNumber(string value): base(value)
{
if (string.IsNullOrEmpty(value) || value.StartsWith("RES/") || value.Length <= 4)
throw new ArgumentOutOfRangeException(nameof(value));
}
}
public record TentativeReservationCreated(
ReservationId ReservationId,
SeatId SeatId,
CustomerId CustomerId,
ReservationNumber Number
);
public record ReservationSeatChanged(
ReservationId ReservationId,
SeatId SeatId
);
public record ReservationConfirmed(
ReservationId ReservationId
);
public record ReservationCancelled(
ReservationId ReservationId
);
public abstract class Aggregate<TKey, T>
where TKey : StronglyTypedValue<T>
where T : IComparable<T>
{
public TKey Id { get; set; } = default!;
[Identity]
public T AggregateId
{
get => Id.Value;
set { }
}
public int Version { get; protected set; }
[JsonIgnore] private readonly Queue<object> uncommittedEvents = new();
public object[] DequeueUncommittedEvents()
{
var dequeuedEvents = uncommittedEvents.ToArray();
uncommittedEvents.Clear();
return dequeuedEvents;
}
protected void Enqueue(object @event)
{
uncommittedEvents.Enqueue(@event);
}
}
public enum ReservationStatus
{
Tentative,
Confirmed,
Cancelled
}
public class Reservation: Aggregate<ReservationId, Guid>
{
public CustomerId CustomerId { get; private set; } = default!;
public SeatId SeatId { get; private set; } = default!;
public ReservationNumber Number { get; private set; } = default!;
public ReservationStatus Status { get; private set; }
public static Reservation CreateTentative(
SeatId seatId,
CustomerId customerId) =>
new(
new ReservationId(Guid.NewGuid()),
seatId,
customerId,
new ReservationNumber(Guid.NewGuid().ToString())
);
private Reservation() { }
private Reservation(
ReservationId id,
SeatId seatId,
CustomerId customerId,
ReservationNumber reservationNumber
)
{
var @event = new TentativeReservationCreated(
id,
seatId,
customerId,
reservationNumber
);
Enqueue(@event);
Apply(@event);
}
public void ChangeSeat(SeatId newSeatId)
{
if (Status != ReservationStatus.Tentative)
throw new InvalidOperationException(
$"Changing seat for the reservation in '{Status}' status is not allowed.");
var @event = new ReservationSeatChanged(Id, newSeatId);
Enqueue(@event);
Apply(@event);
}
public void Confirm()
{
if (Status != ReservationStatus.Tentative)
throw new InvalidOperationException(
$"Only tentative reservation can be confirmed (current status: {Status}.");
var @event = new ReservationConfirmed(Id);
Enqueue(@event);
Apply(@event);
}
public void Cancel()
{
if (Status != ReservationStatus.Tentative)
throw new InvalidOperationException(
$"Only tentative reservation can be cancelled (current status: {Status}).");
var @event = new ReservationCancelled(Id);
Enqueue(@event);
Apply(@event);
}
public void Apply(TentativeReservationCreated @event)
{
Id = @event.ReservationId;
SeatId = @event.SeatId;
CustomerId = @event.CustomerId;
Number = @event.Number;
Status = ReservationStatus.Tentative;
}
public void Apply(ReservationSeatChanged @event)
{
SeatId = @event.SeatId;
}
public void Apply(ReservationConfirmed @event)
{
Status = ReservationStatus.Confirmed;
}
public void Apply(ReservationCancelled @event)
{
Status = ReservationStatus.Cancelled;
}
}
public class CompositeIdsTests(MartenFixture fixture): MartenTest(fixture.PostgreSqlContainer)
{
private const string FirstTenant = "Tenant1";
private const string SecondTenant = "Tenant2";
protected override IDocumentSession CreateSession(Action<StoreOptions>? setStoreOptions = null)
{
var store = DocumentStore.For(options =>
{
options.Connection(ConnectionString);
options.AutoCreateSchemaObjects = AutoCreate.All;
options.DatabaseSchemaName = SchemaName;
options.Events.DatabaseSchemaName = SchemaName;
options.UseNewtonsoftForSerialization(nonPublicMembersStorage: NonPublicMembersStorage.All);
options.Projections.Snapshot<Reservation>(SnapshotLifecycle.Inline);
});
return store.LightweightSession();
}
[Fact]
public void GivenAggregateWithCompositeId_WhenAppendedEvent_LiveAndInlineAggregationWorks()
{
var seatId = new SeatId(Guid.NewGuid());
var customerId = new CustomerId(Guid.NewGuid());
var reservation = Reservation.CreateTentative(seatId, customerId);
var @event = reservation.DequeueUncommittedEvents().Single();
//1. Create events
EventStore.Append(reservation.AggregateId, @event);
Session.SaveChanges();
//2. Get live aggregation
var issuesListFromLiveAggregation = EventStore.AggregateStream<Reservation>(reservation.AggregateId);
//3. Get inline aggregation
var issuesListFromInlineAggregation = Session.Load<Reservation>(reservation.AggregateId);
//4. Get inline aggregation through linq
var reservationId = reservation.Id;
var issuesListFromInlineAggregationFromLinq =
Session.Query<Reservation>().SingleOrDefault(r => r.Id.Value == reservationId.Value);
var issuesListFromInlineAggregationFromLinqWithAggregateId = Session.Query<Reservation>()
.SingleOrDefault(r => r.AggregateId == reservationId.Value);
issuesListFromLiveAggregation.Should().NotBeNull();
issuesListFromInlineAggregation.Should().NotBeNull();
issuesListFromInlineAggregationFromLinq.Should().NotBeNull();
issuesListFromInlineAggregationFromLinqWithAggregateId.Should().NotBeNull();
issuesListFromLiveAggregation!.AggregateId.Should().Be(reservationId.Value);
issuesListFromLiveAggregation.AggregateId.Should().Be(reservationId.Value);
issuesListFromInlineAggregationFromLinq!.AggregateId.Should().Be(reservationId.Value);
issuesListFromInlineAggregationFromLinqWithAggregateId!.AggregateId.Should().Be(reservationId.Value);
}
}