Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zsrdjan committed May 17, 2024
1 parent f55b8f9 commit ec0291d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 118 deletions.
4 changes: 1 addition & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="NUnit" Version="3.14.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageVersion Include="OneOf" Version="3.0.263" />
<PackageVersion Include="OneOf.SourceGenerator" Version="3.0.263" />
<PackageVersion Include="Verify.NUnit" Version="22.5.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
</ItemGroup>
</Project>
</Project>
24 changes: 11 additions & 13 deletions src/Fraktalio.FModel/InternalDecider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Fraktalio.FModel;



/// <summary>
/// [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.
Expand Down Expand Up @@ -60,7 +58,7 @@ internal InternalDecider(Func<C, Si, IEnumerable<Eo>> decide,
/// <param name="f"></param>
/// <typeparam name="Cn">Cn Command new</typeparam>
/// <returns></returns>
public InternalDecider<Cn, Si, So, Ei, Eo> MapLeftOnCommand<Cn>(Func<Cn, C> f)
internal InternalDecider<Cn, Si, So, Ei, Eo> MapLeftOnCommand<Cn>(Func<Cn, C> f)
{
return new InternalDecider<Cn, Si, So, Ei, Eo>(
(cn, si) => Decide(f(cn), si),
Expand Down Expand Up @@ -142,7 +140,7 @@ public InternalDecider<C, Sin, So, Ei, Eo> MapLeftOnState<Sin>(Func<Sin, Si> f)
/// <param name="f"></param>
/// <typeparam name="Son"></typeparam>
/// <returns></returns>
public InternalDecider<C, Si, Son, Ei, Eo> MapOnState<Son>(Func<So, Son> f)
private InternalDecider<C, Si, Son, Ei, Eo> MapOnState<Son>(Func<So, Son> f)
{
return DimapOnState<Si, Son>(si => si, f);
}
Expand All @@ -153,7 +151,7 @@ public InternalDecider<C, Si, Son, Ei, Eo> MapOnState<Son>(Func<So, Son> f)
/// <param name="ff"></param>
/// <typeparam name="Son"></typeparam>
/// <returns></returns>
public InternalDecider<C, Si, Son, Ei, Eo> ApplyOnState<Son>(InternalDecider<C, Si, Func<So, Son>, Ei, Eo> ff)
private InternalDecider<C, Si, Son, Ei, Eo> ApplyOnState<Son>(InternalDecider<C, Si, Func<So, Son>, Ei, Eo> ff)
{
return new InternalDecider<C, Si, Son, Ei, Eo>(
(c, si) => ff.Decide(c, si).Concat(Decide(c, si)),
Expand All @@ -168,7 +166,7 @@ public InternalDecider<C, Si, Son, Ei, Eo> ApplyOnState<Son>(InternalDecider<C,
/// <param name="fb"></param>
/// <typeparam name="Son"></typeparam>
/// <returns></returns>
public InternalDecider<C, Si, Tuple<So, Son>, Ei, Eo> ProductOnState<Son>(InternalDecider<C, Si, Son, Ei, Eo> fb)
internal InternalDecider<C, Si, Tuple<So, Son>, Ei, Eo> ProductOnState<Son>(InternalDecider<C, Si, Son, Ei, Eo> fb)
{
return ApplyOnState(fb.MapOnState(b => new Func<So, Tuple<So, Son>>(a => new Tuple<So, Son>(a, b))));
}
Expand Down Expand Up @@ -205,19 +203,19 @@ internal static class InternalDeciderExtensions
this InternalDecider<C?, Si, So, Ei?, Eo?> x,
InternalDecider<C2?, Si2, So2, Ei2?, Eo2?> 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_SUPER?>(c => (c as C)!)
var deciderX = x.MapLeftOnCommand<C_SUPER?>(c => c as C)
.MapLeftOnState<Tuple<Si, Si2>>(pair => pair.Item1)
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => (Ei)ei!, eo => eo);
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => ei as Ei, eo => eo);

var deciderY = y.MapLeftOnCommand<C_SUPER?>(c => (c as C2)!)
var deciderY = y.MapLeftOnCommand<C_SUPER?>(c => c as C2)
.MapLeftOnState<Tuple<Si, Si2>>(pair => pair.Item2)
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => (Ei2)ei!, eo => eo);
.DimapOnEvent<Ei_SUPER, Eo_SUPER?>(ei => ei as Ei2, eo => eo);

return deciderX.ProductOnState(deciderY);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Fraktalio.FModel/SagaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ namespace Fraktalio.FModel;

public static class SagaExtensions
{
/// <summary>
/// Saga DSL - A convenient builder DSL for the see cref="Saga{AR,A}"/>
/// </summary>
/// <param name="react"></param>
/// <typeparam name="AR"></typeparam>
/// <typeparam name="A"></typeparam>
/// <returns></returns>
public static Saga<AR, A> ToSaga<AR, A>(this Func<AR, IEnumerable<A>> react) => new(react);

/// <summary>
/// Combines [Saga]s into one [Saga]
///
Expand Down
16 changes: 0 additions & 16 deletions src/Fraktalio.FModel/SagaFactory.cs

This file was deleted.

98 changes: 17 additions & 81 deletions test/Fraktalio.FModel.Tests/Examples/NumberSagaFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Fraktalio.FModel;
using Fraktalio.FModel.Tests.Examples.Numbers;

namespace Fraktalio.FModel.Tests.Examples;
Expand All @@ -11,98 +10,38 @@ namespace Fraktalio.FModel.Tests.Examples;

public static class NumberSagaFactory
{
public static Saga<EvenNumberEvent?, OddNumberCommand> CreateEvenNumberSaga()
{
var react = EvenNumberSaga;
return SagaFactory.Create(react);
}

public static Saga<OddNumberEvent?, EvenNumberCommand> CreateOddNumberSaga()
{
var react = OddNumberSaga;
return SagaFactory.Create(react);
}
public static Saga<EvenNumberEvent?, OddNumberCommand> EvenNumberSaga() => EvenNumberSagaReact().ToSaga();

/// <summary>
/// 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]
/// </summary>
/// <param name="numberEvent">The event</param>
/// <returns>List of commands</returns>
private static IEnumerable<NumberCommand> NumberSaga(NumberEvent numberEvent)
{
return numberEvent switch
private static Func<EvenNumberEvent?, IEnumerable<OddNumberCommand>> EvenNumberSagaReact() =>
numberEvent => numberEvent switch
{
EvenNumberAdded evenNumberAdded => new NumberCommand[]
EvenNumberAdded evenNumberAdded => new OddNumberCommand[]
{
new OddNumberCommand.AddOddNumber(
new Description($"{evenNumberAdded.Value.Value - 1}"),
new Number(evenNumberAdded.Value.Value - 1)
)
},
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<NumberCommand>()
_ => []
};
}

/// <summary>
/// Even number saga
///
/// It reacts on Action Results of type of any [NumberEvent.EvenNumberEvent] and issue a Command/Action of type [NumberCommand.OddNumberCommand]
/// </summary>
/// <param name="numberEvent">The event</param>
/// <returns>List of commands</returns>
private static IEnumerable<OddNumberCommand> EvenNumberSaga(
this EvenNumberEvent? numberEvent)
{
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<OddNumberCommand>()
};
}
public static Saga<OddNumberEvent?, EvenNumberCommand> OddNumberSaga() => OddNumberSagaReact().ToSaga();

/// <summary>
/// Odd number saga
Expand All @@ -111,10 +50,8 @@ private static IEnumerable<OddNumberCommand> EvenNumberSaga(
/// </summary>
/// <param name="numberEvent">The event</param>
/// <returns>List of commands</returns>
public static IEnumerable<EvenNumberCommand> OddNumberSaga(
this OddNumberEvent? numberEvent)
{
return numberEvent switch
private static Func<OddNumberEvent?, IEnumerable<EvenNumberCommand>> OddNumberSagaReact() =>
numberEvent => numberEvent switch
{
OddNumberEvent.OddNumberAdded oddNumberAdded => new EvenNumberCommand[]
{
Expand All @@ -124,15 +61,14 @@ public static IEnumerable<EvenNumberCommand> 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<EvenNumberCommand>()
_ => []
};
}
}
9 changes: 4 additions & 5 deletions test/Fraktalio.FModel.Tests/SagaTest.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,8 +17,8 @@ public class SagaTest
[SetUp]
public void Setup()
{
_evenSaga = NumberSagaFactory.CreateEvenNumberSaga();
_oddSaga = NumberSagaFactory.CreateOddNumberSaga();
_evenSaga = EvenNumberSaga();
_oddSaga = OddNumberSaga();
}

[Test]
Expand Down

0 comments on commit ec0291d

Please sign in to comment.