Search code examples
c#.netstate-machinestateless-state-machine

.NET stateless multiple PermitIf


I need to add multiple guards to a certain state in my .Net Stateless state machine.

Consider the following mock scenario. I can only allow to go from Insert to Modify if two conditions are met:

        _sm.Configure(State.Insert)
            .PermitReentry(Trigger.Insert)
            .PermitIf(Trigger.Modify, State.Modified, Condition1, "Only permitted when condition 1 true")
            .PermitIf(Trigger.Modify, State.Modified, Condition2, "Only permitted when condition 2 true")
            .Permit(Trigger.Cancel, State.Canceled)

In the code above only the first condition is evaluated. I could alternatively bundle two conditions into a single one passing it in one PermitIf, but I'd rather not do that:

        .PermitIf(Trigger.Modify, State.Modified, BundledCondition, "Only permitted when bundled condition is true")

Is there any way of implementing multiple guards in dotnet-state-machine?


Solution

  • I don't think that's possible. Specially after reading this statement in the documentation:

    Guard clauses within a state must be mutually exclusive (multiple guard clauses cannot be valid at the same time.)

    I guess bundling all the conditions in only guard is the way to go.

    https://github.com/dotnet-state-machine/stateless