Skip to content

Commit 7b95e6f

Browse files
committed
Updated Marten aggregates to have Version applied by convention
1 parent 96a4c22 commit 7b95e6f

File tree

19 files changed

+24
-73
lines changed

19 files changed

+24
-73
lines changed

Core.Marten/Repository/MartenRepository.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ public interface IMartenRepository<T> where T : class, IAggregate
88
{
99
Task<T?> Find(Guid id, CancellationToken cancellationToken);
1010
Task<long> Add(T aggregate, TraceMetadata? eventMetadata = null, CancellationToken cancellationToken = default);
11-
Task<long> Update(T aggregate, long? expectedVersion = null, TraceMetadata? traceMetadata = null, CancellationToken cancellationToken = default);
12-
Task<long> Delete(T aggregate, long? expectedVersion = null, TraceMetadata? eventMetadata = null, CancellationToken cancellationToken = default);
11+
12+
Task<long> Update(T aggregate, long? expectedVersion = null, TraceMetadata? traceMetadata = null,
13+
CancellationToken cancellationToken = default);
14+
15+
Task<long> Delete(T aggregate, long? expectedVersion = null, TraceMetadata? eventMetadata = null,
16+
CancellationToken cancellationToken = default);
1317
}
1418

1519
public class MartenRepository<T>: IMartenRepository<T> where T : class, IAggregate
@@ -26,7 +30,8 @@ IDocumentSession documentSession
2630
public Task<T?> Find(Guid id, CancellationToken cancellationToken) =>
2731
documentSession.Events.AggregateStreamAsync<T>(id, token: cancellationToken);
2832

29-
public async Task<long> Add(T aggregate, TraceMetadata? traceMetadata = null, CancellationToken cancellationToken = default)
33+
public async Task<long> Add(T aggregate, TraceMetadata? traceMetadata = null,
34+
CancellationToken cancellationToken = default)
3035
{
3136
documentSession.CorrelationId = traceMetadata?.CorrelationId?.Value;
3237
documentSession.CausationId = traceMetadata?.CausationId?.Value;
@@ -43,20 +48,19 @@ public async Task<long> Add(T aggregate, TraceMetadata? traceMetadata = null, Ca
4348
return events.Length;
4449
}
4550

46-
public async Task<long> Update(T aggregate, long? expectedVersion = null, TraceMetadata? traceMetadata = null, CancellationToken cancellationToken = default)
51+
public async Task<long> Update(T aggregate, long? expectedVersion = null, TraceMetadata? traceMetadata = null,
52+
CancellationToken cancellationToken = default)
4753
{
4854
documentSession.CorrelationId = traceMetadata?.CorrelationId?.Value;
4955
documentSession.CausationId = traceMetadata?.CausationId?.Value;
5056

5157
var events = aggregate.DequeueUncommittedEvents();
5258

53-
var nextVersion = expectedVersion.HasValue ?
54-
expectedVersion.Value + events.Length
55-
: aggregate.Version;
59+
var nextVersion = (expectedVersion ?? aggregate.Version) + events.Length;
5660

5761
documentSession.Events.Append(
5862
aggregate.Id,
59-
aggregate.Version,
63+
nextVersion,
6064
events
6165
);
6266

@@ -65,6 +69,7 @@ public async Task<long> Update(T aggregate, long? expectedVersion = null, TraceM
6569
return nextVersion;
6670
}
6771

68-
public Task<long> Delete(T aggregate, long? expectedVersion = null, TraceMetadata? traceMetadata = null, CancellationToken cancellationToken = default) =>
72+
public Task<long> Delete(T aggregate, long? expectedVersion = null, TraceMetadata? traceMetadata = null,
73+
CancellationToken cancellationToken = default) =>
6974
Update(aggregate, expectedVersion, traceMetadata, cancellationToken);
7075
}

Marten.Integration.Tests/CompositeIds/CompositeIdsTests.cs

-4
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,21 @@ public void Apply(TentativeReservationCreated @event)
224224
CustomerId = @event.CustomerId;
225225
Number = @event.Number;
226226
Status = ReservationStatus.Tentative;
227-
Version++;
228227
}
229228

230229
public void Apply(ReservationSeatChanged @event)
231230
{
232231
SeatId = @event.SeatId;
233-
Version++;
234232
}
235233

236234
public void Apply(ReservationConfirmed @event)
237235
{
238236
Status = ReservationStatus.Confirmed;
239-
Version++;
240237
}
241238

242239
public void Apply(ReservationCancelled @event)
243240
{
244241
Status = ReservationStatus.Cancelled;
245-
Version++;
246242
}
247243
}
248244

Sample/ECommerce/Carts/Carts.Tests/Carts/ConfirmingCart/ConfirmCartTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ public void ForTentativeCart_ShouldSucceed()
2323

2424
// Then
2525
cart.Status.Should().Be(ShoppingCartStatus.Confirmed);
26-
cart.Version.Should().Be(2);
2726

2827
var @event = cart.PublishedEvent<ShoppingCartConfirmed>();
2928

3029
@event.Should().NotBeNull();
3130
@event.Should().BeOfType<ShoppingCartConfirmed>();
3231
@event!.CartId.Should().Be(cart.Id);
3332
}
34-
}
33+
}

Sample/ECommerce/Carts/Carts.Tests/Extensions/Reservations/CartExtensions.cs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static ShoppingCart IsOpenedCartWith(
1818
shoppingCart.Status.Should().Be(ShoppingCartStatus.Pending);
1919
shoppingCart.ProductItems.Should().BeEmpty();
2020
shoppingCart.TotalPrice.Should().Be(0);
21-
shoppingCart.Version.Should().Be(1);
2221

2322
return shoppingCart;
2423
}

Sample/ECommerce/Carts/Carts/ShoppingCarts/GettingCartById/ShoppingCartDetails.cs

-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class ShoppingCartDetails
2424

2525
public void Apply(ShoppingCartOpened @event)
2626
{
27-
Version++;
28-
2927
Id = @event.CartId;
3028
ClientId = @event.ClientId;
3129
ProductItems = new List<PricedProductItem>();
@@ -34,8 +32,6 @@ public void Apply(ShoppingCartOpened @event)
3432

3533
public void Apply(ProductAdded @event)
3634
{
37-
Version++;
38-
3935
var newProductItem = @event.ProductItem;
4036

4137
var existingProductItem = FindProductItemMatchingWith(newProductItem);
@@ -54,8 +50,6 @@ public void Apply(ProductAdded @event)
5450

5551
public void Apply(ProductRemoved @event)
5652
{
57-
Version++;
58-
5953
var productItemToBeRemoved = @event.ProductItem;
6054

6155
var existingProductItem = FindProductItemMatchingWith(@event.ProductItem);
@@ -77,15 +71,11 @@ public void Apply(ProductRemoved @event)
7771

7872
public void Apply(ShoppingCartConfirmed @event)
7973
{
80-
Version++;
81-
8274
Status = ShoppingCartStatus.Confirmed;
8375
}
8476

8577
public void Apply(ShoppingCartCanceled @event)
8678
{
87-
Version++;
88-
8979
Status = ShoppingCartStatus.Canceled;
9080
}
9181

Sample/ECommerce/Carts/Carts/ShoppingCarts/ShoppingCart.cs

-10
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ private ShoppingCart(
4444

4545
public void Apply(ShoppingCartOpened @event)
4646
{
47-
Version++;
48-
4947
Id = @event.CartId;
5048
ClientId = @event.ClientId;
5149
ProductItems = new List<PricedProductItem>();
@@ -69,8 +67,6 @@ public void AddProduct(
6967

7068
public void Apply(ProductAdded @event)
7169
{
72-
Version++;
73-
7470
var newProductItem = @event.ProductItem;
7571

7672
var existingProductItem = FindProductItemMatchingWith(newProductItem);
@@ -109,8 +105,6 @@ public void RemoveProduct(
109105

110106
public void Apply(ProductRemoved @event)
111107
{
112-
Version++;
113-
114108
var productItemToBeRemoved = @event.ProductItem;
115109

116110
var existingProductItem = FindProductItemMatchingWith(@event.ProductItem);
@@ -143,8 +137,6 @@ public void Confirm()
143137

144138
public void Apply(ShoppingCartConfirmed @event)
145139
{
146-
Version++;
147-
148140
Status = ShoppingCartStatus.Confirmed;
149141
}
150142

@@ -161,8 +153,6 @@ public void Cancel()
161153

162154
public void Apply(ShoppingCartCanceled @event)
163155
{
164-
Version++;
165-
166156
Status = ShoppingCartStatus.Canceled;
167157
}
168158

Sample/ECommerce/Orders/Orders/Orders/Order.cs

-8
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ private Order(Guid id, Guid clientId, IReadOnlyList<PricedProductItem> productIt
5151

5252
public void Apply(OrderInitialized @event)
5353
{
54-
Version++;
55-
5654
Id = @event.OrderId;
5755
ClientId = @event.ClientId;
5856
ProductItems = @event.ProductItems;
@@ -75,8 +73,6 @@ public void RecordPayment(Guid paymentId, DateTime recordedAt)
7573

7674
public void Apply(OrderPaymentRecorded @event)
7775
{
78-
Version++;
79-
8076
PaymentId = @event.PaymentId;
8177
Status = OrderStatus.Paid;
8278
}
@@ -94,8 +90,6 @@ public void Complete()
9490

9591
public void Apply(OrderCompleted @event)
9692
{
97-
Version++;
98-
9993
Status = OrderStatus.Completed;
10094
}
10195

@@ -112,8 +106,6 @@ public void Cancel(OrderCancellationReason cancellationReason)
112106

113107
public void Apply(OrderCancelled @event)
114108
{
115-
Version++;
116-
117109
Status = OrderStatus.Cancelled;
118110
}
119111
}

Sample/ECommerce/Payments/Payments/Payments/Payment.cs

+1-9
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ private Payment(Guid id, Guid orderId, decimal amount)
3131

3232
public void Apply(PaymentRequested @event)
3333
{
34-
Version++;
35-
3634
Id = @event.PaymentId;
3735
OrderId = @event.OrderId;
3836
Amount = @event.Amount;
@@ -51,8 +49,6 @@ public void Complete()
5149

5250
public void Apply(PaymentCompleted @event)
5351
{
54-
Version++;
55-
5652
Status = PaymentStatus.Completed;
5753
}
5854

@@ -69,8 +65,6 @@ public void Discard(DiscardReason discardReason)
6965

7066
public void Apply(PaymentDiscarded @event)
7167
{
72-
Version++;
73-
7468
Status = PaymentStatus.Failed;
7569
}
7670

@@ -87,8 +81,6 @@ public void TimeOut()
8781

8882
public void Apply(PaymentTimedOut @event)
8983
{
90-
Version++;
91-
9284
Status = PaymentStatus.Failed;
9385
}
94-
}
86+
}

Sample/MeetingsManagement/MeetingsManagement/Meetings/Meeting.cs

-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public void Apply(MeetingCreated @event)
4141
Id = @event.MeetingId;
4242
Name = @event.Name;
4343
Created = @event.Created;
44-
45-
Version++;
4644
}
4745

4846
internal void Schedule(DateRange occurs)
@@ -56,7 +54,5 @@ internal void Schedule(DateRange occurs)
5654
public void Apply(MeetingScheduled @event)
5755
{
5856
Occurs = @event.Occurs;
59-
60-
Version++;
6157
}
6258
}

Sample/Tickets/Tickets.Tests/Extensions/Reservations/ReservationExtensions.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public static Reservation IsTentativeReservationWith(
1717
reservation.Id.Should().Be(id);
1818
reservation.Number.Should().Be(number);
1919
reservation.SeatId.Should().Be(seatId);
20-
reservation.Version.Should().Be(1);
2120

2221
return reservation;
2322
}
@@ -38,4 +37,4 @@ public static Reservation HasTentativeReservationCreatedEventWith(
3837

3938
return reservation;
4039
}
41-
}
40+
}

Sample/Tickets/Tickets.Tests/Reservations/CancellingReservation/CancelReservationTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public void ForTentativeReservation_ShouldSucceed()
2323

2424
// Then
2525
reservation.Status.Should().Be(ReservationStatus.Cancelled);
26-
reservation.Version.Should().Be(2);
2726

2827
var @event = reservation.PublishedEvent<ReservationCancelled>();
2928

@@ -43,4 +42,4 @@ public void ForCancelledReservation_ShouldFailWithInvalidOperation()
4342
{
4443

4544
}
46-
}
45+
}

Sample/Tickets/Tickets.Tests/Reservations/ChangingReservationSeat/ChangeSeatTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public void ForValidParams_UpdatesSeatId()
2424

2525
// Then
2626
reservation.SeatId.Should().Be(newSeatId);
27-
reservation.Version.Should().Be(2);
2827

2928
var @event = reservation.PublishedEvent<ReservationSeatChanged>();
3029

@@ -33,4 +32,4 @@ public void ForValidParams_UpdatesSeatId()
3332
@event!.ReservationId.Should().Be(reservation.Id);
3433
@event.SeatId.Should().Be(newSeatId);
3534
}
36-
}
35+
}

Sample/Tickets/Tickets.Tests/Reservations/ConfirmingReservation/ConfirmReservationTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ public void ForTentativeReservation_ShouldSucceed()
2323

2424
// Then
2525
reservation.Status.Should().Be(ReservationStatus.Confirmed);
26-
reservation.Version.Should().Be(2);
2726

2827
var @event = reservation.PublishedEvent<ReservationConfirmed>();
2928

3029
@event.Should().NotBeNull();
3130
@event.Should().BeOfType<ReservationConfirmed>();
3231
@event!.ReservationId.Should().Be(reservation.Id);
3332
}
34-
}
33+
}

Sample/Tickets/Tickets/Reservations/GettingReservationById/ReservationDetails.cs

-4
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,21 @@ public void Apply(TentativeReservationCreated @event)
2424
SeatId = @event.SeatId;
2525
Number = @event.Number;
2626
Status = ReservationStatus.Tentative;
27-
Version++;
2827
}
2928

3029
public void Apply(ReservationSeatChanged @event)
3130
{
3231
SeatId = @event.SeatId;
33-
Version++;
3432
}
3533

3634
public void Apply(ReservationConfirmed @event)
3735
{
3836
Status = ReservationStatus.Confirmed;
39-
Version++;
4037
}
4138

4239
public void Apply(ReservationCancelled @event)
4340
{
4441
Status = ReservationStatus.Cancelled;
45-
Version++;
4642
}
4743
}
4844

Sample/Tickets/Tickets/Reservations/Reservation.cs

-4
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,20 @@ public void Apply(TentativeReservationCreated @event)
9494
SeatId = @event.SeatId;
9595
Number = @event.Number;
9696
Status = ReservationStatus.Tentative;
97-
Version++;
9897
}
9998

10099
public void Apply(ReservationSeatChanged @event)
101100
{
102101
SeatId = @event.SeatId;
103-
Version++;
104102
}
105103

106104
public void Apply(ReservationConfirmed @event)
107105
{
108106
Status = ReservationStatus.Confirmed;
109-
Version++;
110107
}
111108

112109
public void Apply(ReservationCancelled @event)
113110
{
114111
Status = ReservationStatus.Cancelled;
115-
Version++;
116112
}
117113
}

0 commit comments

Comments
 (0)