From 1fc9429fef7e30d991e722b6c6dd060dbbe9b5d3 Mon Sep 17 00:00:00 2001 From: Srdjan Zivojinovic Date: Fri, 17 May 2024 10:28:17 +0200 Subject: [PATCH] Cleanup --- .github/workflows/ci.yml | 50 +++++----- Directory.Packages.props | 20 ++-- .../Fraktalio.FModel.Contracts.csproj | 16 ++-- src/Fraktalio.FModel.Contracts/ISaga.cs | 2 - src/Fraktalio.FModel/Fraktalio.FModel.csproj | 16 ++-- src/Fraktalio.FModel/InternalDecider.cs | 24 +++-- src/Fraktalio.FModel/SagaExtensions.cs | 23 ++++- src/Fraktalio.FModel/SagaFactory.cs | 16 ---- .../DeciderExtensions.cs | 3 +- .../Examples/NumberSagaFactory.cs | 92 ++++--------------- .../Examples/Numbers/Description.cs | 23 +++-- .../Examples/Numbers/Number.cs | 22 +++-- .../Fraktalio.FModel.Tests.csproj | 6 +- test/Fraktalio.FModel.Tests/SagaTest.cs | 9 +- 14 files changed, 140 insertions(+), 182 deletions(-) delete mode 100644 src/Fraktalio.FModel/SagaFactory.cs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e6d70cf..40eeb90 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: push: branches: - - main + - main pull_request: branches: - main @@ -11,27 +11,27 @@ jobs: build: runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Setup dotnet 8.0 - uses: actions/setup-dotnet@v1 - with: - dotnet-version: '8.0.204' - - name: Build and Test - run: ./Build.ps1 - shell: pwsh - - name: Push to MyGet - if: false - env: - NUGET_URL: https://www.myget.org/F/TODO/api/v3/index.json - NUGET_API_KEY: ${{ secrets.MYGET_FRAKTALIO_CI_API_KEY }} - run: ./Push.ps1 - shell: pwsh - - name: Artifacts - if: false - uses: actions/upload-artifact@v2 - with: - name: artifacts - path: artifacts/**/* + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Setup dotnet 8.0 + uses: actions/setup-dotnet@v1 + with: + dotnet-version: '8.0.204' + - name: Build and Test + run: ./Build.ps1 + shell: pwsh + - name: Push to MyGet + if: false + env: + NUGET_URL: https://www.myget.org/F/TODO/api/v3/index.json + NUGET_API_KEY: ${{ secrets.MYGET_FRAKTALIO_CI_API_KEY }} + run: ./Push.ps1 + shell: pwsh + - name: Artifacts + if: false + uses: actions/upload-artifact@v2 + with: + name: artifacts + path: artifacts/**/* diff --git a/Directory.Packages.props b/Directory.Packages.props index aa6cb94..45b23f3 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,19 +3,17 @@ true - - - - - - - - - - + + + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - \ No newline at end of file + diff --git a/src/Fraktalio.FModel.Contracts/Fraktalio.FModel.Contracts.csproj b/src/Fraktalio.FModel.Contracts/Fraktalio.FModel.Contracts.csproj index e0673e6..da18a98 100644 --- a/src/Fraktalio.FModel.Contracts/Fraktalio.FModel.Contracts.csproj +++ b/src/Fraktalio.FModel.Contracts/Fraktalio.FModel.Contracts.csproj @@ -1,13 +1,13 @@  - - net8.0 - enable - enable - + + net8.0 + enable + enable + - - - + + + diff --git a/src/Fraktalio.FModel.Contracts/ISaga.cs b/src/Fraktalio.FModel.Contracts/ISaga.cs index db26648..f2ccfb3 100644 --- a/src/Fraktalio.FModel.Contracts/ISaga.cs +++ b/src/Fraktalio.FModel.Contracts/ISaga.cs @@ -2,8 +2,6 @@ namespace Fraktalio.FModel.Contracts; -using System.Collections.Generic; - /// /// An interface of the Saga /// diff --git a/src/Fraktalio.FModel/Fraktalio.FModel.csproj b/src/Fraktalio.FModel/Fraktalio.FModel.csproj index 946e9d2..5121a1d 100644 --- a/src/Fraktalio.FModel/Fraktalio.FModel.csproj +++ b/src/Fraktalio.FModel/Fraktalio.FModel.csproj @@ -1,13 +1,13 @@  - - net8.0 - enable - enable - + + net8.0 + enable + enable + - - - + + + diff --git a/src/Fraktalio.FModel/InternalDecider.cs b/src/Fraktalio.FModel/InternalDecider.cs index f077a80..313cb83 100644 --- a/src/Fraktalio.FModel/InternalDecider.cs +++ b/src/Fraktalio.FModel/InternalDecider.cs @@ -1,7 +1,5 @@ namespace Fraktalio.FModel; - - /// /// [InternalDecider] is a datatype that represents the main decision-making algorithm. /// It has five generic parameters [C], [Si], [So], [Ei], [Eo] , representing the type of the values that [InternalDecider] may contain or use. @@ -60,7 +58,7 @@ internal InternalDecider(Func> decide, /// /// Cn Command new /// - public InternalDecider MapLeftOnCommand(Func f) + internal InternalDecider MapLeftOnCommand(Func f) { return new InternalDecider( (cn, si) => Decide(f(cn), si), @@ -142,7 +140,7 @@ public InternalDecider MapLeftOnState(Func f) /// /// /// - public InternalDecider MapOnState(Func f) + private InternalDecider MapOnState(Func f) { return DimapOnState(si => si, f); } @@ -153,7 +151,7 @@ public InternalDecider MapOnState(Func f) /// /// /// - public InternalDecider ApplyOnState(InternalDecider, Ei, Eo> ff) + private InternalDecider ApplyOnState(InternalDecider, Ei, Eo> ff) { return new InternalDecider( (c, si) => ff.Decide(c, si).Concat(Decide(c, si)), @@ -168,7 +166,7 @@ public InternalDecider ApplyOnState(InternalDecider /// /// - public InternalDecider, Ei, Eo> ProductOnState(InternalDecider fb) + internal InternalDecider, Ei, Eo> ProductOnState(InternalDecider fb) { return ApplyOnState(fb.MapOnState(b => new Func>(a => new Tuple(a, b)))); } @@ -205,19 +203,19 @@ internal static class InternalDeciderExtensions this InternalDecider x, InternalDecider y) where C : class?, C_SUPER? - where C2 : class?, C_SUPER - where Ei : Ei_SUPER + where C2 : class, C_SUPER + where Ei : class?, Ei_SUPER? where Eo : Eo_SUPER - where Ei2 : Ei_SUPER + where Ei2 : class, Ei_SUPER where Eo2 : Eo_SUPER { - var deciderX = x.MapLeftOnCommand(c => (c as C)!) + var deciderX = x.MapLeftOnCommand(c => c as C) .MapLeftOnState>(pair => pair.Item1) - .DimapOnEvent(ei => (Ei)ei!, eo => eo); + .DimapOnEvent(ei => ei as Ei, eo => eo); - var deciderY = y.MapLeftOnCommand(c => (c as C2)!) + var deciderY = y.MapLeftOnCommand(c => c as C2) .MapLeftOnState>(pair => pair.Item2) - .DimapOnEvent(ei => (Ei2)ei!, eo => eo); + .DimapOnEvent(ei => ei as Ei2, eo => eo); return deciderX.ProductOnState(deciderY); } diff --git a/src/Fraktalio.FModel/SagaExtensions.cs b/src/Fraktalio.FModel/SagaExtensions.cs index d0829cd..adffe0e 100644 --- a/src/Fraktalio.FModel/SagaExtensions.cs +++ b/src/Fraktalio.FModel/SagaExtensions.cs @@ -2,6 +2,18 @@ namespace Fraktalio.FModel; public static class SagaExtensions { + /// + /// Saga DSL - A convenient builder DSL for the see cref="Saga{AR,A}"/> + /// + /// + /// + /// + /// + public static Saga ToSaga(this Func> react) + { + return new Saga(react); + } + /// /// Combines [Saga]s into one [Saga] /// @@ -18,11 +30,12 @@ public static class SagaExtensions /// common superclass for [AR] and [AR2] /// common superclass for [A] and [A2] /// new Saga of type Saga`[AR_SUPER], [A_SUPER]>` - public static Saga Combine(this Saga sagaX, Saga sagaY) - where AR : AR_SUPER - where A : A_SUPER - where AR2 : AR_SUPER - where A2 : A_SUPER + public static Saga Combine(this Saga sagaX, + Saga sagaY) + where AR : AR_SUPER + where A : A_SUPER + where AR2 : AR_SUPER + where A2 : A_SUPER { var newSagaX = sagaX.MapLeftOnActionResult(it => it is AR ar ? ar : default) .MapOnAction(it => it); diff --git a/src/Fraktalio.FModel/SagaFactory.cs b/src/Fraktalio.FModel/SagaFactory.cs deleted file mode 100644 index 7a9b94e..0000000 --- a/src/Fraktalio.FModel/SagaFactory.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace Fraktalio.FModel; - -public static class SagaFactory -{ - /// - /// Saga DSL - A convenient builder DSL for the see cref="Saga{AR,A}"/> - /// - /// - /// - /// - /// - public static Saga Create(Func> react) - { - return new Saga(react); - } -} diff --git a/test/Fraktalio.FModel.Tests/DeciderExtensions.cs b/test/Fraktalio.FModel.Tests/DeciderExtensions.cs index 28f142d..6b4f324 100644 --- a/test/Fraktalio.FModel.Tests/DeciderExtensions.cs +++ b/test/Fraktalio.FModel.Tests/DeciderExtensions.cs @@ -4,7 +4,8 @@ namespace Fraktalio.FModel.Tests; public static class DeciderExtensions { - public static IEnumerable GivenEvents(this IDecider decider, IEnumerable events, Func command) + public static IEnumerable GivenEvents(this IDecider decider, IEnumerable events, + Func command) { var currentState = events.Aggregate(decider.InitialState, (s, e) => decider.Evolve(s, e)); return decider.Decide(command(), currentState); diff --git a/test/Fraktalio.FModel.Tests/Examples/NumberSagaFactory.cs b/test/Fraktalio.FModel.Tests/Examples/NumberSagaFactory.cs index 715d082..acdd1f6 100644 --- a/test/Fraktalio.FModel.Tests/Examples/NumberSagaFactory.cs +++ b/test/Fraktalio.FModel.Tests/Examples/NumberSagaFactory.cs @@ -1,4 +1,3 @@ -using Fraktalio.FModel; using Fraktalio.FModel.Tests.Examples.Numbers; namespace Fraktalio.FModel.Tests.Examples; @@ -11,31 +10,23 @@ namespace Fraktalio.FModel.Tests.Examples; public static class NumberSagaFactory { - public static Saga CreateEvenNumberSaga() + public static Saga EvenNumberSaga() { - var react = EvenNumberSaga; - return SagaFactory.Create(react); - } - - public static Saga CreateOddNumberSaga() - { - var react = OddNumberSaga; - return SagaFactory.Create(react); + return EvenNumberSagaReact().ToSaga(); } /// - /// Very simple Number saga, just for fun ;) + /// Even number saga /// - /// It reacts on Action Results of type of any Event (Even or Odd) and issue a Command/Action (Odd or Even) - /// For example if the EvenNumberAdded happened with value 4, a new command of type AddOddNumber will be published with value EvenNumberAdded-1=3 + /// It reacts on Action Results of type of any [NumberEvent.EvenNumberEvent] and issue a Command/Action of type [NumberCommand.OddNumberCommand] /// /// The event /// List of commands - private static IEnumerable NumberSaga(NumberEvent numberEvent) + private static Func> EvenNumberSagaReact() { - return numberEvent switch + return numberEvent => numberEvent switch { - EvenNumberAdded evenNumberAdded => new NumberCommand[] + EvenNumberAdded evenNumberAdded => new OddNumberCommand[] { new OddNumberCommand.AddOddNumber( new Description($"{evenNumberAdded.Value.Value - 1}"), @@ -43,65 +34,21 @@ private static IEnumerable NumberSaga(NumberEvent numberEvent) ) }, - EvenNumberEvent.EvenNumberSubtracted evenNumberSubtracted => new NumberCommand[] - { + EvenNumberEvent.EvenNumberSubtracted evenNumberSubtracted => + [ new OddNumberCommand.SubtractOddNumber( new Description($"{evenNumberSubtracted.Value.Value - 1}"), new Number(evenNumberSubtracted.Value.Value - 1) ) - }, - - OddNumberEvent.OddNumberAdded oddNumberAdded => new NumberCommand[] - { - new EvenNumberCommand.AddEvenNumber( - new Description($"{oddNumberAdded.Value.Value + 1}"), - new Number(oddNumberAdded.Value.Value + 1) - ) - }, - - OddNumberEvent.OddNumberSubtracted oddNumberSubtracted => new NumberCommand[] - { - new EvenNumberCommand.SubtractEvenNumber( - new Description($"{oddNumberSubtracted.Value.Value + 1}"), - new Number(oddNumberSubtracted.Value.Value + 1) - ) - }, + ], - _ => Enumerable.Empty() + _ => [] }; } - /// - /// Even number saga - /// - /// It reacts on Action Results of type of any [NumberEvent.EvenNumberEvent] and issue a Command/Action of type [NumberCommand.OddNumberCommand] - /// - /// The event - /// List of commands - private static IEnumerable EvenNumberSaga( - this EvenNumberEvent? numberEvent) + public static Saga OddNumberSaga() { - return numberEvent switch - { - EvenNumberAdded evenNumberAdded => new OddNumberCommand[] - { - new OddNumberCommand.AddOddNumber( - new Description($"{evenNumberAdded.Value.Value - 1}"), - new Number(evenNumberAdded.Value.Value - 1) - ) - }, - - EvenNumberEvent.EvenNumberSubtracted evenNumberSubtracted => new OddNumberCommand - [] - { - new OddNumberCommand.SubtractOddNumber( - new Description($"{evenNumberSubtracted.Value.Value - 1}"), - new Number(evenNumberSubtracted.Value.Value - 1) - ) - }, - - _ => Enumerable.Empty() - }; + return OddNumberSagaReact().ToSaga(); } /// @@ -111,10 +58,9 @@ private static IEnumerable EvenNumberSaga( /// /// The event /// List of commands - public static IEnumerable OddNumberSaga( - this OddNumberEvent? numberEvent) + private static Func> OddNumberSagaReact() { - return numberEvent switch + return numberEvent => numberEvent switch { OddNumberEvent.OddNumberAdded oddNumberAdded => new EvenNumberCommand[] { @@ -124,15 +70,15 @@ public static IEnumerable OddNumberSaga( ) }, - OddNumberEvent.OddNumberSubtracted oddNumberSubtracted => new EvenNumberCommand[] - { + OddNumberEvent.OddNumberSubtracted oddNumberSubtracted => + [ new EvenNumberCommand.SubtractEvenNumber( new Description($"{oddNumberSubtracted.Value.Value - 1}"), new Number(oddNumberSubtracted.Value.Value - 1) ) - }, + ], - _ => Enumerable.Empty() + _ => [] }; } } diff --git a/test/Fraktalio.FModel.Tests/Examples/Numbers/Description.cs b/test/Fraktalio.FModel.Tests/Examples/Numbers/Description.cs index 2897126..6471d82 100644 --- a/test/Fraktalio.FModel.Tests/Examples/Numbers/Description.cs +++ b/test/Fraktalio.FModel.Tests/Examples/Numbers/Description.cs @@ -2,12 +2,23 @@ public record Description(string Value) { - public static Description operator +(Description a, Description b) => - new($"{a.Value} + {b.Value}"); + public static Description operator +(Description a, Description b) + { + return new Description($"{a.Value} + {b.Value}"); + } - public static Description operator -(Description a, Description b) => - new($"{a.Value} - {b.Value}"); - public static Description Create(string value) => new(value); + public static Description operator -(Description a, Description b) + { + return new Description($"{a.Value} - {b.Value}"); + } - public static implicit operator string(Description value) => value.Value; + public static Description Create(string value) + { + return new Description(value); + } + + public static implicit operator string(Description value) + { + return value.Value; + } } diff --git a/test/Fraktalio.FModel.Tests/Examples/Numbers/Number.cs b/test/Fraktalio.FModel.Tests/Examples/Numbers/Number.cs index 3223a60..49eb704 100644 --- a/test/Fraktalio.FModel.Tests/Examples/Numbers/Number.cs +++ b/test/Fraktalio.FModel.Tests/Examples/Numbers/Number.cs @@ -2,13 +2,23 @@ namespace Fraktalio.FModel.Tests.Examples.Numbers; public record Number(int Value) { - public static Description operator +(Number a, Number b) => - new($"{a.Value} + {b.Value}"); + public static Description operator +(Number a, Number b) + { + return new Description($"{a.Value} + {b.Value}"); + } - public static Description operator -(Number a, Number b) => - new($"{a.Value} - {b.Value}"); + public static Description operator -(Number a, Number b) + { + return new Description($"{a.Value} - {b.Value}"); + } - public static Number Create(int value) => new Number(value); + public static Number Create(int value) + { + return new Number(value); + } - public static implicit operator int(Number value) => value.Value; + public static implicit operator int(Number value) + { + return value.Value; + } } diff --git a/test/Fraktalio.FModel.Tests/Fraktalio.FModel.Tests.csproj b/test/Fraktalio.FModel.Tests/Fraktalio.FModel.Tests.csproj index 576cc8d..53c7651 100644 --- a/test/Fraktalio.FModel.Tests/Fraktalio.FModel.Tests.csproj +++ b/test/Fraktalio.FModel.Tests/Fraktalio.FModel.Tests.csproj @@ -6,7 +6,7 @@ - + @@ -22,8 +22,8 @@ - - + + diff --git a/test/Fraktalio.FModel.Tests/SagaTest.cs b/test/Fraktalio.FModel.Tests/SagaTest.cs index 061c1dc..914d030 100644 --- a/test/Fraktalio.FModel.Tests/SagaTest.cs +++ b/test/Fraktalio.FModel.Tests/SagaTest.cs @@ -1,11 +1,10 @@ -using Fraktalio.FModel.Tests.Examples; -using Fraktalio.FModel.Tests.Examples.Numbers; - +using Fraktalio.FModel.Tests.Examples.Numbers; using EvenNumberEvent = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent; using EvenNumberAdded = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.EvenNumberEvent.EvenNumberAdded; using OddNumberEvent = Fraktalio.FModel.Tests.Examples.Numbers.NumberEvent.OddNumberEvent; using OddNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.OddNumberCommand; using EvenNumberCommand = Fraktalio.FModel.Tests.Examples.Numbers.NumberCommand.EvenNumberCommand; +using static Fraktalio.FModel.Tests.Examples.NumberSagaFactory; namespace Fraktalio.FModel.Tests; @@ -18,8 +17,8 @@ public class SagaTest [SetUp] public void Setup() { - _evenSaga = NumberSagaFactory.CreateEvenNumberSaga(); - _oddSaga = NumberSagaFactory.CreateOddNumberSaga(); + _evenSaga = EvenNumberSaga(); + _oddSaga = OddNumberSaga(); } [Test]