diff --git a/Core.ElasticSearch/Projections/ElasticSearchProjection.cs b/Core.ElasticSearch/Projections/ElasticSearchProjection.cs index c9bd35547..0315bc84b 100644 --- a/Core.ElasticSearch/Projections/ElasticSearchProjection.cs +++ b/Core.ElasticSearch/Projections/ElasticSearchProjection.cs @@ -30,7 +30,7 @@ public async Task Handle(EventEnvelope eventEnvelope, CancellationToken var entity = (await elasticClient.GetAsync(id, i => i.Index(indexName), ct).ConfigureAwait(false))?.Source ?? (TView) Activator.CreateInstance(typeof(TView), true)!; - entity.When(eventEnvelope); + entity.Evolve(eventEnvelope); await elasticClient.IndexAsync( entity, diff --git a/Core.EventStoreDB/Events/AggregateStreamExtensions.cs b/Core.EventStoreDB/Events/AggregateStreamExtensions.cs index 68ae66912..59a55feaf 100644 --- a/Core.EventStoreDB/Events/AggregateStreamExtensions.cs +++ b/Core.EventStoreDB/Events/AggregateStreamExtensions.cs @@ -30,7 +30,7 @@ await foreach (var @event in readResult) { var eventData = @event.Deserialize(); - aggregate.When(eventData!); + aggregate.Evolve(eventData!); } return aggregate; diff --git a/Core.Marten/ExternalProjections/MartenExternalProjection.cs b/Core.Marten/ExternalProjections/MartenExternalProjection.cs index dc96d6265..1c91068e3 100644 --- a/Core.Marten/ExternalProjections/MartenExternalProjection.cs +++ b/Core.Marten/ExternalProjections/MartenExternalProjection.cs @@ -33,7 +33,7 @@ public async Task Handle(EventEnvelope eventEnvelope, CancellationToken if (entity.LastProcessedPosition >= eventLogPosition) return; - entity.When(@event); + entity.Evolve(@event); entity.LastProcessedPosition = eventLogPosition; diff --git a/Core.Tests/AggregateWithWhenTests.cs b/Core.Tests/AggregateWithWhenTests.cs index 6f27bb860..6ddea77de 100644 --- a/Core.Tests/AggregateWithWhenTests.cs +++ b/Core.Tests/AggregateWithWhenTests.cs @@ -55,7 +55,7 @@ public class Invoice: Aggregate public InvoiceSendMethod SentVia { get; private set; } public DateTime SentAt { get; private set; } - public override void When(object @event) + public override void Evolve(object @event) { switch (@event) { @@ -125,7 +125,7 @@ public void AggregationWithWhenShouldGetTheCurrentState() // 3. Apply each event on the entity. foreach (var @event in events) { - invoice.When(@event); + invoice.Evolve(@event); } invoice.Id.Should().Be(invoiceInitiated.Number); diff --git a/Core/Aggregates/Aggregate.cs b/Core/Aggregates/Aggregate.cs index 275213b29..516824bd9 100644 --- a/Core/Aggregates/Aggregate.cs +++ b/Core/Aggregates/Aggregate.cs @@ -12,7 +12,7 @@ public abstract class Aggregate: IAggregate where T : notnull [NonSerialized] private readonly Queue uncommittedEvents = new(); - public virtual void When(object @event) { } + public virtual void Evolve(object @event) { } public object[] DequeueUncommittedEvents() { diff --git a/Core/ProcessManagers/ProcessManager.cs b/Core/ProcessManagers/ProcessManager.cs index a344d32f8..fedbb86bd 100644 --- a/Core/ProcessManagers/ProcessManager.cs +++ b/Core/ProcessManagers/ProcessManager.cs @@ -29,7 +29,7 @@ public EventOrCommand[] DequeuePendingMessages() protected void ScheduleCommand(object @event) => scheduledCommands.Enqueue(EventOrCommand.Command(@event)); - public virtual void When(object @event) + public virtual void Evolve(object @event) { } } diff --git a/Core/Projections/IProjection.cs b/Core/Projections/IProjection.cs index 46e56d09e..6f4857796 100644 --- a/Core/Projections/IProjection.cs +++ b/Core/Projections/IProjection.cs @@ -2,7 +2,7 @@ public interface IProjection { - void When(object @event); + void Evolve(object @event); } public interface IVersionedProjection: IProjection diff --git a/Sample/EventStoreDB/ECommerce/Carts/Carts.Tests/Builders/CartBuilder.cs b/Sample/EventStoreDB/ECommerce/Carts/Carts.Tests/Builders/CartBuilder.cs index f3faea795..dd56ab68d 100644 --- a/Sample/EventStoreDB/ECommerce/Carts/Carts.Tests/Builders/CartBuilder.cs +++ b/Sample/EventStoreDB/ECommerce/Carts/Carts.Tests/Builders/CartBuilder.cs @@ -25,7 +25,7 @@ public ShoppingCart Build() foreach (var @event in eventsToApply) { - cart.When(@event); + cart.Evolve(@event); } return cart; diff --git a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartById/ShoppingCartDetails.cs b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartById/ShoppingCartDetails.cs index 1496e5d32..b335fab52 100644 --- a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartById/ShoppingCartDetails.cs +++ b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartById/ShoppingCartDetails.cs @@ -23,7 +23,7 @@ public class ShoppingCartDetails: IVersionedProjection public long Version { get; set; } - public void When(object @event) + public void Evolve(object @event) { switch (@event) { diff --git a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartHistory/CartHistory.cs b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartHistory/CartHistory.cs index 4de4cb7ff..3edbea9ca 100644 --- a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartHistory/CartHistory.cs +++ b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCartHistory/CartHistory.cs @@ -14,7 +14,7 @@ public class CartHistory: IVersionedProjection public string Description { get; set; } = default!; public ulong LastProcessedPosition { get; set; } - public void When(object @event) + public void Evolve(object @event) { switch (@event) { diff --git a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCarts/ShoppingCartShortInfo.cs b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCarts/ShoppingCartShortInfo.cs index bc7fe42aa..30ecd7cdd 100644 --- a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCarts/ShoppingCartShortInfo.cs +++ b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/GettingCarts/ShoppingCartShortInfo.cs @@ -17,7 +17,7 @@ public class ShoppingCartShortInfo: IVersionedProjection public ulong LastProcessedPosition { get; set; } - public void When(object @event) + public void Evolve(object @event) { switch (@event) { diff --git a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/ShoppingCart.cs b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/ShoppingCart.cs index 65fd7ca4e..986019b97 100644 --- a/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/ShoppingCart.cs +++ b/Sample/EventStoreDB/ECommerce/Carts/Carts/ShoppingCarts/ShoppingCart.cs @@ -29,7 +29,7 @@ public class ShoppingCart: Aggregate private ShoppingCart(){} - public override void When(object @event) + public override void Evolve(object @event) { switch (@event) { diff --git a/Workshops/IntroductionToEventSourcing/Solved/01-EventsDefinition/README.md b/Workshops/IntroductionToEventSourcing/Solved/01-EventsDefinition/README.md index 0e5a3e9ac..da4b03eae 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/01-EventsDefinition/README.md +++ b/Workshops/IntroductionToEventSourcing/Solved/01-EventsDefinition/README.md @@ -17,5 +17,6 @@ Events model: ![events](./assets/events.jpg) +There are two alternative solutions: 1. Simple events structure [Solution1/EventsDefinitionTests.cs](./Solution1/EventsDefinitionTests.cs). 2. Events structure with union type to give option to tell that Shopping Cart event is one of the defined types [Solution2/EventsDefinitionTests.cs](./Solution2/EventsDefinitionTests.cs). Read more in [Union types in C#](https://event-driven.io/en/union_types_in_csharp/). diff --git a/Workshops/IntroductionToEventSourcing/Solved/03-AppendingEvents.Marten/AppendingEvents.cs b/Workshops/IntroductionToEventSourcing/Solved/03-AppendingEvents.Marten/AppendingEvents.cs index 80a376e05..507f8c87c 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/03-AppendingEvents.Marten/AppendingEvents.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/03-AppendingEvents.Marten/AppendingEvents.cs @@ -4,32 +4,39 @@ using Xunit; namespace IntroductionToEventSourcing.AppendingEvents; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public record PricedProductItem( diff --git a/Workshops/IntroductionToEventSourcing/Solved/04-AppendingEvents.EventStoreDB/AppendingEvents.cs b/Workshops/IntroductionToEventSourcing/Solved/04-AppendingEvents.EventStoreDB/AppendingEvents.cs index 7478cf06f..74c87869f 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/04-AppendingEvents.EventStoreDB/AppendingEvents.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/04-AppendingEvents.EventStoreDB/AppendingEvents.cs @@ -4,32 +4,39 @@ using Xunit; namespace IntroductionToEventSourcing.AppendingEvents; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public record PricedProductItem( diff --git a/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Immutable/GettingStateFromEventsTests.cs b/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Immutable/GettingStateFromEventsTests.cs index 02abf7b5b..2230bbf2b 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Immutable/GettingStateFromEventsTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Immutable/GettingStateFromEventsTests.cs @@ -4,32 +4,39 @@ using Xunit; namespace IntroductionToEventSourcing.GettingStateFromEvents.Immutable; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public record PricedProductItem( diff --git a/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Mutable/GettingStateFromEventsTests.cs b/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Mutable/GettingStateFromEventsTests.cs index 99a0b6de8..6d8af803e 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Mutable/GettingStateFromEventsTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/05-GettingStateFromEvents.Marten/Mutable/GettingStateFromEventsTests.cs @@ -4,32 +4,39 @@ using Xunit; namespace IntroductionToEventSourcing.GettingStateFromEvents.Mutable; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public class PricedProductItem diff --git a/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Immutable/GettingStateFromEventsTests.cs b/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Immutable/GettingStateFromEventsTests.cs index 69aac612f..88e063fbd 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Immutable/GettingStateFromEventsTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Immutable/GettingStateFromEventsTests.cs @@ -5,32 +5,39 @@ using Xunit; namespace IntroductionToEventSourcing.GettingStateFromEvents.Immutable; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public record PricedProductItem( diff --git a/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Mutable/GettingStateFromEventsTests.cs b/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Mutable/GettingStateFromEventsTests.cs index 0225249b9..9e6828070 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Mutable/GettingStateFromEventsTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/06-GettingStateFromEvents.EventStoreDB/Mutable/GettingStateFromEventsTests.cs @@ -5,32 +5,39 @@ using Xunit; namespace IntroductionToEventSourcing.GettingStateFromEvents.Mutable; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public class PricedProductItem diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogic.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogic.cs index 218e99882..df88c356f 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogic.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogic.cs @@ -1,4 +1,5 @@ namespace IntroductionToEventSourcing.BusinessLogic.Immutable; +using static ShoppingCartEvent; // ENTITY public record ShoppingCart( diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogicTests.cs index 3507fb7c9..29bb9402d 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Immutable/BusinessLogicTests.cs @@ -2,32 +2,39 @@ using Xunit; namespace IntroductionToEventSourcing.BusinessLogic.Immutable; +using static ShoppingCartEvent; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogic.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogic.cs index f7eae1955..5617e355f 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogic.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogic.cs @@ -1,4 +1,5 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mutable.Solution1; +using static ShoppingCartEvent; public abstract class Aggregate { @@ -6,7 +7,7 @@ public abstract class Aggregate private readonly Queue uncommittedEvents = new(); - public virtual void When(object @event) { } + public virtual void Evolve(object @event) { } public object[] DequeueUncommittedEvents() { @@ -34,7 +35,7 @@ public class ShoppingCart: Aggregate public bool IsClosed => ShoppingCartStatus.Closed.HasFlag(Status); - public override void When(object @event) + public override void Evolve(object @event) { switch (@event) { diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogicTests.cs index 16cdb5881..13574e287 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution1/BusinessLogicTests.cs @@ -4,30 +4,36 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mutable.Solution1; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public class PricedProductItem @@ -52,7 +58,7 @@ public static ShoppingCart GetShoppingCart(this IEnumerable events) foreach (var @event in events) { - shoppingCart.When(@event); + shoppingCart.Evolve(@event); } return shoppingCart; diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogic.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogic.cs index 87e9070cd..f61b28a53 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogic.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogic.cs @@ -1,4 +1,5 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mutable.Solution2; +using static ShoppingCartEvent; public interface IAggregate { diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogicTests.cs index a0c1a3f74..7b40ec5d6 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic/Mutable/Solution2/BusinessLogicTests.cs @@ -4,30 +4,36 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mutable.Solution2; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public record ProductItem( diff --git a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogic.cs b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogic.cs index 10d491742..6ffd2f974 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogic.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogic.cs @@ -1,4 +1,5 @@ namespace IntroductionToEventSourcing.BusinessLogic.Immutable.Solution1; +using static ShoppingCartEvent; // ENTITY public record ShoppingCart( diff --git a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogicTests.cs index 4391510f4..2ad6e1be6 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Immutable/Solution1/BusinessLogicTests.cs @@ -5,30 +5,36 @@ namespace IntroductionToEventSourcing.BusinessLogic.Immutable.Solution1; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS diff --git a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogic.cs b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogic.cs index 75b6b2cc6..fa30cb97e 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogic.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogic.cs @@ -1,4 +1,5 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mixed; +using static ShoppingCartEvent; public interface IAggregate { diff --git a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogicTests.cs index a8ac68fac..687d866dc 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mixed/BusinessLogicTests.cs @@ -5,30 +5,36 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mixed; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public record ProductItem( diff --git a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogic.cs b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogic.cs index 885214dc5..6d93c238e 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogic.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogic.cs @@ -1,4 +1,5 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mutable; +using static ShoppingCartEvent; public abstract class Aggregate { @@ -6,7 +7,7 @@ public abstract class Aggregate private readonly Queue uncommittedEvents = new(); - public virtual void When(object @event) { } + public virtual void Evolve(object @event) { } public object[] DequeueUncommittedEvents() { @@ -108,7 +109,7 @@ public class ShoppingCart: Aggregate public bool IsClosed => ShoppingCartStatus.Closed.HasFlag(Status); - public override void When(object @event) + public override void Evolve(object @event) { switch (@event) { diff --git a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogicTests.cs index d2f0f8ef1..1854e79d1 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/08-BusinessLogic.Marten/Mutable/BusinessLogicTests.cs @@ -5,30 +5,36 @@ namespace IntroductionToEventSourcing.BusinessLogic.Mutable; // EVENTS -public record ShoppingCartOpened( - Guid ShoppingCartId, - Guid ClientId -); - -public record ProductItemAddedToShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ProductItemRemovedFromShoppingCart( - Guid ShoppingCartId, - PricedProductItem ProductItem -); - -public record ShoppingCartConfirmed( - Guid ShoppingCartId, - DateTime ConfirmedAt -); - -public record ShoppingCartCanceled( - Guid ShoppingCartId, - DateTime CanceledAt -); +public abstract record ShoppingCartEvent +{ + public record ShoppingCartOpened( + Guid ShoppingCartId, + Guid ClientId + ): ShoppingCartEvent; + + public record ProductItemAddedToShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ProductItemRemovedFromShoppingCart( + Guid ShoppingCartId, + PricedProductItem ProductItem + ): ShoppingCartEvent; + + public record ShoppingCartConfirmed( + Guid ShoppingCartId, + DateTime ConfirmedAt + ): ShoppingCartEvent; + + public record ShoppingCartCanceled( + Guid ShoppingCartId, + DateTime CanceledAt + ): ShoppingCartEvent; + + // This won't allow + private ShoppingCartEvent(){} +} // VALUE OBJECTS public class PricedProductItem