diff --git a/StateMachine.Tests/UnitTest1.cs b/StateMachine.Tests/UnitTest1.cs
index b4f1c3d..f88d453 100644
--- a/StateMachine.Tests/UnitTest1.cs
+++ b/StateMachine.Tests/UnitTest1.cs
@@ -700,6 +700,39 @@ public async Task ShouldExecuteActionsInOrder()
Assert.Equal(new[] { "FirstAction", "SecondAction" }, callOrder);
}
+ ///
+ /// Ensures that actions associated with transitions are executed in the correct order.
+ ///
+ [Fact]
+ public async Task ShouldExecuteActionsInOrderWithSimpleAddTransitionMethod()
+ {
+ // Arrange
+ var logger = new NullLogger>();
+ var callOrder = new List();
+
+ var stateMachine = new StateMachine(MyStates.Initial, logger);
+ stateMachine.AddTransition(MyStates.Initial, MyEvents.DoStuff, MyStates.SomeState, async (data, cancellationToken) =>
+ {
+ callOrder.Add("FirstAction");
+ await Task.Delay(1, cancellationToken);
+ });
+
+ stateMachine.AddTransition(MyStates.SomeState, MyEvents.DoOtherStuff, MyStates.Complete, async (data, cancellationToken) =>
+ {
+ callOrder.Add("SecondAction");
+ await Task.Delay(1, cancellationToken);
+ });
+
+ var dto = new MyCustomDto { Prop1 = 1 };
+
+ // Act
+ await stateMachine.GetNextAsync(MyEvents.DoStuff, dto);
+ await stateMachine.GetNextAsync(MyEvents.DoOtherStuff, dto);
+
+ // Assert
+ Assert.Equal(new[] { "FirstAction", "SecondAction" }, callOrder);
+ }
+
///
/// Dummy asynchronous operation used as the action delegate in several tests.
///
@@ -715,7 +748,7 @@ private async Task SomeMethodToExecuteAsync(MyCustomDto obj, CancellationToken c
public class MyCustomDto
{
public int Prop1 { get; set; }
- public string Prop2 { get; set; }
+ public string Prop2 { get; set; } = string.Empty;
}
///
diff --git a/StateMachine/StateMachine.cs b/StateMachine/StateMachine.cs
index b0d4316..157f894 100644
--- a/StateMachine/StateMachine.cs
+++ b/StateMachine/StateMachine.cs
@@ -65,6 +65,19 @@ public void AddTransition(Transition transit
_transitions.Add(key, transition);
}
+ // New simplified AddTransition method
+ public void AddTransition(
+ TStateEnum currentState,
+ TEventEnum evt,
+ TStateEnum nextState,
+ Func action,
+ Func? guard = null)
+ {
+ var transition = new Transition(currentState, evt, nextState, action, guard);
+ AddTransition(transition);
+ }
+
+
///
/// Handles an event and transitions to the next state if possible.
///
diff --git a/StateMachine/Transition.cs b/StateMachine/Transition.cs
index 71a0b97..1364cfd 100644
--- a/StateMachine/Transition.cs
+++ b/StateMachine/Transition.cs
@@ -54,7 +54,7 @@ public Transition(
TEventEnum evt,
TStateEnum nextState,
Func action,
- Func guard = null)
+ Func? guard = null)
{
CurrentState = currentState;
Event = evt;