diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs index 26cc032f4..49b1ff1f3 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs @@ -5,8 +5,7 @@ namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable; using static ShoppingCart.Event; -using static ShoppingCartCommand; -using static ShoppingCartService; +using static ShoppingCart.Command; using static ShoppingCart; public class BusinessLogicTests @@ -15,20 +14,15 @@ public class BusinessLogicTests [Fact] public void OpensShoppingCart() => Spec.Given([]) - .When(() => Decide(new Open(clientId, now), new Initial())) + .When(new Open(clientId, now)) .Then(new Opened(clientId, now)); // Add [Fact] public void CantAddProductItemToNotExistingShoppingCart() => Spec.Given([]) - .When(state => - Decide( - new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), - now - ), - state - ) + .When( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now) ) .ThenThrows(); @@ -37,14 +31,7 @@ public class BusinessLogicTests Spec.Given([ new Opened(clientId, now) ]) - .When(state => - Decide( - new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), - now - ), - state - ) - ) + .When(new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now)) .Then( new ProductItemAdded( new PricedProductItem(ProductItem.ProductId, ProductItem.Quantity, Price), @@ -59,13 +46,8 @@ public class BusinessLogicTests new Opened(clientId, now), new ProductItemAdded(pricedProductItem, now), ]) - .When(state => - Decide( - new AddProductItem(FakeProductPriceCalculator.Returning(OtherPrice).Calculate(OtherProductItem), - now - ), - state - ) + .When( + new AddProductItem(FakeProductPriceCalculator.Returning(OtherPrice).Calculate(OtherProductItem), now) ) .Then( new ProductItemAdded( @@ -81,13 +63,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Confirmed(now) ]) - .When(state => - Decide( - new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), - now - ), - state - ) + .When( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now) ) .ThenThrows(); @@ -98,13 +75,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Canceled(now) ]) - .When(state => - Decide( - new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), - now - ), - state - ) + .When( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now) ) .ThenThrows(); @@ -112,11 +84,8 @@ public class BusinessLogicTests [Fact] public void CantRemoveProductItemFromNotExistingShoppingCart() => Spec.Given([]) - .When(state => - Decide( - new RemoveProductItem(pricedProductItem, now), - state - ) + .When( + new RemoveProductItem(pricedProductItem, now) ) .ThenThrows(); @@ -126,11 +95,8 @@ public class BusinessLogicTests new Opened(clientId, now), new ProductItemAdded(pricedProductItem, now), ]) - .When(state => - Decide( - new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now), - state - ) + .When( + new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now) ) .Then( new ProductItemRemoved( @@ -145,12 +111,8 @@ public class BusinessLogicTests new Opened(clientId, now), new ProductItemAdded(pricedProductItem, now), ]) - .When(state => - Decide( - new RemoveProductItem(otherPricedProductItem with { Quantity = 1 }, - now), - state - ) + .When( + new RemoveProductItem(otherPricedProductItem with { Quantity = 1 }, now) ) .ThenThrows(); @@ -161,11 +123,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Confirmed(now) ]) - .When(state => - Decide( - new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now), - state - ) + .When( + new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now) ) .ThenThrows(); @@ -176,11 +135,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Canceled(now) ]) - .When(state => - Decide( - new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now), - state - ) + .When( + new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now) ) .ThenThrows(); @@ -189,8 +145,8 @@ public class BusinessLogicTests [Fact] public void CantConfirmNotExistingShoppingCart() => Spec.Given([]) - .When(state => - Decide(new Confirm(now), state) + .When( + new Confirm(now) ) .ThenThrows(); @@ -200,8 +156,8 @@ public class BusinessLogicTests Spec.Given([ new Opened(clientId, now), ]) - .When(state => - Decide(new Confirm(now), state) + .When( + new Confirm(now) ) .ThenThrows(); @@ -211,8 +167,8 @@ public class BusinessLogicTests new Opened(clientId, now), new ProductItemAdded(pricedProductItem, now), ]) - .When(state => - Decide(new Confirm(now), state) + .When( + new Confirm(now) ) .Then( new Confirmed(now) @@ -225,8 +181,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Confirmed(now) ]) - .When(state => - Decide(new Confirm(now), state) + .When( + new Confirm(now) ) .ThenThrows(); @@ -237,8 +193,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Canceled(now) ]) - .When(state => - Decide(new Confirm(now), state) + .When( + new Confirm(now) ) .ThenThrows(); @@ -247,8 +203,8 @@ public class BusinessLogicTests [Trait("Category", "SkipCI")] public void CantCancelNotExistingShoppingCart() => Spec.Given([]) - .When(state => - Decide(new Cancel(now), state) + .When( + new Cancel(now) ) .ThenThrows(); @@ -257,8 +213,8 @@ public class BusinessLogicTests Spec.Given([ new Opened(clientId, now), ]) - .When(state => - Decide(new Cancel(now), state) + .When( + new Cancel(now) ) .ThenThrows(); @@ -268,8 +224,8 @@ public class BusinessLogicTests new Opened(clientId, now), new ProductItemAdded(pricedProductItem, now), ]) - .When(state => - Decide(new Cancel(now), state) + .When( + new Cancel(now) ) .Then( new Canceled(now) @@ -282,8 +238,8 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Canceled(now) ]) - .When(state => - Decide(new Cancel(now), state) + .When( + new Cancel(now) ) .ThenThrows(); @@ -294,14 +250,13 @@ public class BusinessLogicTests new ProductItemAdded(pricedProductItem, now), new Confirmed(now) ]) - .When(state => - Decide(new Cancel(now), state) + .When( + new Cancel(now) ) .ThenThrows(); private readonly DateTimeOffset now = DateTimeOffset.Now; private readonly Guid clientId = Guid.NewGuid(); - private readonly Guid shoppingCartId = Guid.NewGuid(); private static readonly ProductItem ProductItem = new(Guid.NewGuid(), Random.Shared.Next(1, 200)); private static readonly ProductItem OtherProductItem = new(Guid.NewGuid(), Random.Shared.Next(1, 200)); private static readonly int Price = Random.Shared.Next(1, 1000); @@ -314,6 +269,6 @@ public class BusinessLogicTests new(OtherProductItem.ProductId, OtherProductItem.Quantity, Price); - private readonly HandlerSpecification Spec = - Specification.For(Evolve, () => new Initial()); + private readonly DeciderSpecification Spec = + Specification.For(Decide, Evolve, () => new Initial()); } diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCart.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCart.cs index 91f96db15..60bd65055 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCart.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCart.cs @@ -6,7 +6,7 @@ namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable; // EVENTS -public abstract record ShoppingCart +public abstract partial record ShoppingCart { public abstract record Event { diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartDecider.cs similarity index 50% rename from Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs rename to Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartDecider.cs index 1d996be2c..fee56874b 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartDecider.cs @@ -3,40 +3,39 @@ namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable; using static ShoppingCart.Event; -using static ShoppingCartCommand; -using static ShoppingCart; +using static ShoppingCart.Command; -public abstract record ShoppingCartCommand +public partial record ShoppingCart { - public record Open( - Guid ClientId, - DateTimeOffset Now - ): ShoppingCartCommand; - - public record AddProductItem( - PricedProductItem ProductItem, - DateTimeOffset Now - ): ShoppingCartCommand; - - public record RemoveProductItem( - PricedProductItem ProductItem, - DateTimeOffset Now - ): ShoppingCartCommand; - - public record Confirm( - DateTimeOffset Now - ): ShoppingCartCommand; - - public record Cancel( - DateTimeOffset Now - ): ShoppingCartCommand; - - private ShoppingCartCommand() { } -} - -public static class ShoppingCartService -{ - public static Event Decide(ShoppingCartCommand command, ShoppingCart state) => + public abstract record Command + { + public record Open( + Guid ClientId, + DateTimeOffset Now + ): Command; + + public record AddProductItem( + PricedProductItem ProductItem, + DateTimeOffset Now + ): Command; + + public record RemoveProductItem( + PricedProductItem ProductItem, + DateTimeOffset Now + ): Command; + + public record Confirm( + DateTimeOffset Now + ): Command; + + public record Cancel( + DateTimeOffset Now + ): Command; + + private Command() { } + } + + public static Event Decide(Command command, ShoppingCart state) => (state, command) switch { (Initial, Open(var clientId, var now)) => @@ -56,6 +55,7 @@ public static class ShoppingCartService (Pending, Cancel(var now)) => new Canceled(now), - _ => throw new InvalidOperationException($"Cannot {command.GetType().Name} for {state.GetType().Name} shopping cart") + _ => throw new InvalidOperationException( + $"Cannot {command.GetType().Name} for {state.GetType().Name} shopping cart") }; }