From ccea28976108a5033447e05e11961e86a008fb65 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Fri, 3 May 2024 11:48:01 +0200 Subject: [PATCH] Refactored also commands --- .../Immutable/BusinessLogicTests.cs | 72 ++++++++----------- .../Immutable/ShoppingCartService.cs | 28 +++----- 2 files changed, 42 insertions(+), 58 deletions(-) diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs index d74b13fc9..e3855b109 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/BusinessLogicTests.cs @@ -15,7 +15,7 @@ public class BusinessLogicTests [Fact] public void OpensShoppingCart() => Spec.Given([]) - .When(() => Handle(new OpenShoppingCart(shoppingCartId, clientId, now), new Initial())) + .When(() => Decide(new Open(clientId, now), new Initial())) .Then(new Opened(clientId, now)); // Add @@ -23,10 +23,8 @@ public void OpensShoppingCart() => public void CantAddProductItemToNotExistingShoppingCart() => Spec.Given([]) .When(state => - Handle( - new AddProductItemToShoppingCart( - shoppingCartId, - FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), + Decide( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now ), state @@ -40,10 +38,8 @@ public void AddsProductItemToEmptyShoppingCart() => new Opened(clientId, now) ]) .When(state => - Handle( - new AddProductItemToShoppingCart( - shoppingCartId, - FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), + Decide( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now ), state @@ -64,10 +60,8 @@ public void AddsProductItemToNonEmptyShoppingCart() => new ProductItemAdded(pricedProductItem, now), ]) .When(state => - Handle( - new AddProductItemToShoppingCart( - shoppingCartId, - FakeProductPriceCalculator.Returning(OtherPrice).Calculate(OtherProductItem), + Decide( + new AddProductItem(FakeProductPriceCalculator.Returning(OtherPrice).Calculate(OtherProductItem), now ), state @@ -88,10 +82,8 @@ public void CantAddProductItemToConfirmedShoppingCart() => new Confirmed(now) ]) .When(state => - Handle( - new AddProductItemToShoppingCart( - shoppingCartId, - FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), + Decide( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now ), state @@ -107,10 +99,8 @@ public void CantAddProductItemToCanceledShoppingCart() => new Canceled(now) ]) .When(state => - Handle( - new AddProductItemToShoppingCart( - shoppingCartId, - FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), + Decide( + new AddProductItem(FakeProductPriceCalculator.Returning(Price).Calculate(ProductItem), now ), state @@ -123,8 +113,8 @@ public void CantAddProductItemToCanceledShoppingCart() => public void CantRemoveProductItemFromNotExistingShoppingCart() => Spec.Given([]) .When(state => - Handle( - new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem, now), + Decide( + new RemoveProductItem(pricedProductItem, now), state ) ) @@ -137,8 +127,8 @@ public void RemovesExistingProductItemFromShoppingCart() => new ProductItemAdded(pricedProductItem, now), ]) .When(state => - Handle( - new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem with { Quantity = 1 }, now), + Decide( + new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now), state ) ) @@ -156,8 +146,8 @@ public void CantRemoveNonExistingProductItemFromNonEmptyShoppingCart() => new ProductItemAdded(pricedProductItem, now), ]) .When(state => - Handle( - new RemoveProductItemFromShoppingCart(shoppingCartId, otherPricedProductItem with { Quantity = 1 }, + Decide( + new RemoveProductItem(otherPricedProductItem with { Quantity = 1 }, now), state ) @@ -172,8 +162,8 @@ public void CantRemoveExistingProductItemFromCanceledShoppingCart() => new Confirmed(now) ]) .When(state => - Handle( - new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem with { Quantity = 1 }, now), + Decide( + new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now), state ) ) @@ -187,8 +177,8 @@ public void CantRemoveExistingProductItemFromConfirmedShoppingCart() => new Canceled(now) ]) .When(state => - Handle( - new RemoveProductItemFromShoppingCart(shoppingCartId, pricedProductItem with { Quantity = 1 }, now), + Decide( + new RemoveProductItem(pricedProductItem with { Quantity = 1 }, now), state ) ) @@ -200,7 +190,7 @@ public void CantRemoveExistingProductItemFromConfirmedShoppingCart() => public void CantConfirmNotExistingShoppingCart() => Spec.Given([]) .When(state => - Handle(new ConfirmShoppingCart(shoppingCartId, now), state) + Decide(new Confirm(now), state) ) .ThenThrows(); @@ -211,7 +201,7 @@ public void CantConfirmEmptyShoppingCart() => new Opened(clientId, now), ]) .When(state => - Handle(new ConfirmShoppingCart(shoppingCartId, now), state) + Decide(new Confirm(now), state) ) .ThenThrows(); @@ -222,7 +212,7 @@ public void ConfirmsNonEmptyShoppingCart() => new ProductItemAdded(pricedProductItem, now), ]) .When(state => - Handle(new ConfirmShoppingCart(shoppingCartId, now), state) + Decide(new Confirm(now), state) ) .Then( new Confirmed(now) @@ -236,7 +226,7 @@ public void CantConfirmAlreadyConfirmedShoppingCart() => new Confirmed(now) ]) .When(state => - Handle(new ConfirmShoppingCart(shoppingCartId, now), state) + Decide(new Confirm(now), state) ) .ThenThrows(); @@ -248,7 +238,7 @@ public void CantConfirmCanceledShoppingCart() => new Canceled(now) ]) .When(state => - Handle(new ConfirmShoppingCart(shoppingCartId, now), state) + Decide(new Confirm(now), state) ) .ThenThrows(); @@ -258,7 +248,7 @@ public void CantConfirmCanceledShoppingCart() => public void CantCancelNotExistingShoppingCart() => Spec.Given([]) .When(state => - Handle(new CancelShoppingCart(shoppingCartId, now), state) + Decide(new Cancel(now), state) ) .ThenThrows(); @@ -268,7 +258,7 @@ public void CancelsEmptyShoppingCart() => new Opened(clientId, now), ]) .When(state => - Handle(new CancelShoppingCart(shoppingCartId, now), state) + Decide(new Cancel(now), state) ) .ThenThrows(); @@ -279,7 +269,7 @@ public void CancelsNonEmptyShoppingCart() => new ProductItemAdded(pricedProductItem, now), ]) .When(state => - Handle(new CancelShoppingCart(shoppingCartId, now), state) + Decide(new Cancel(now), state) ) .Then( new Canceled(now) @@ -293,7 +283,7 @@ public void CantCancelAlreadyCanceledShoppingCart() => new Canceled(now) ]) .When(state => - Handle(new CancelShoppingCart(shoppingCartId, now), state) + Decide(new Cancel(now), state) ) .ThenThrows(); @@ -305,7 +295,7 @@ public void CantCancelConfirmedShoppingCart() => new Confirmed(now) ]) .When(state => - Handle(new CancelShoppingCart(shoppingCartId, now), state) + Decide(new Cancel(now), state) ) .ThenThrows(); diff --git a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs index 92ad7bebd..f2bc1b3d5 100644 --- a/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs +++ b/Workshops/IntroductionToEventSourcing/Solved/07-BusinessLogic.Slimmed/Immutable/ShoppingCartService.cs @@ -8,31 +8,26 @@ namespace IntroductionToEventSourcing.BusinessLogic.Slimmed.Immutable; public abstract record ShoppingCartCommand { - public record OpenShoppingCart( - Guid ShoppingCartId, + public record Open( Guid ClientId, DateTimeOffset Now ): ShoppingCartCommand; - public record AddProductItemToShoppingCart( - Guid ShoppingCartId, + public record AddProductItem( PricedProductItem ProductItem, DateTimeOffset Now ): ShoppingCartCommand; - public record RemoveProductItemFromShoppingCart( - Guid ShoppingCartId, + public record RemoveProductItem( PricedProductItem ProductItem, DateTimeOffset Now ): ShoppingCartCommand; - public record ConfirmShoppingCart( - Guid ShoppingCartId, + public record Confirm( DateTimeOffset Now ): ShoppingCartCommand; - public record CancelShoppingCart( - Guid ShoppingCartId, + public record Cancel( DateTimeOffset Now ): ShoppingCartCommand; @@ -41,25 +36,24 @@ private ShoppingCartCommand() { } public static class ShoppingCartService { - public static Event Handle(ShoppingCartCommand command, ShoppingCart state) => + public static Event Decide(ShoppingCartCommand command, ShoppingCart state) => (state, command) switch { - (Initial, OpenShoppingCart(_, var clientId, var now)) => + (Initial, Open(var clientId, var now)) => new Opened(clientId, now), - (Pending, AddProductItemToShoppingCart(_, var productItem, var now)) => + (Pending, AddProductItem(var productItem, var now)) => new ProductItemAdded(productItem, now), - (Pending(var productItems), - RemoveProductItemFromShoppingCart(_, var productItem, var now)) => + (Pending(var productItems), RemoveProductItem(var productItem, var now)) => productItems.HasEnough(productItem) ? new ProductItemRemoved(productItem, now) : throw new InvalidOperationException("Not enough product items to remove"), - (Pending, ConfirmShoppingCart(_, var now)) => + (Pending, Confirm(var now)) => new Confirmed(now), - (Pending, CancelShoppingCart(_, var now)) => + (Pending, Cancel(var now)) => new Canceled(now), _ => throw new InvalidOperationException($"Cannot {command.GetType().Name} for {state.GetType().Name} shopping cart")