dotnet-state-machine / stateless

A simple library for creating state machines in C# code
Other
5.53k stars 765 forks source link

Expose unmet guard conditions #581

Closed andrewmckaskill closed 3 months ago

andrewmckaskill commented 4 months ago

This change adds a method to expose the unmet guard conditions.

I'm using this to show validation messages to users as to WHY they are unable to take a certain action within the workflow.

While it is possible to get the entire set of guard condition descriptions through the StateMachine.GetInfo() method, the info object exposes all guard conditions for a Trigger, without reporting back which ones are unmet.

Please let me know if this needs additional changes, and what kind of tests would be appropriate.

I'm also happy to change the return value IEnumerable<Tuple<TTrigger, string[]>> with Dictionary<TTrigger, ICollection<string[]> if that more closely aligns with other types in use - please let me know.

mclift commented 3 months ago

Thanks for taking the time to contribute!

From what you've described, it looks like the overloads of CanFire with out ICollection<string> unmetGuards are a close fit, although they differ in that you need to provide the trigger you want to test. Would CanFire meet your needs here?

Some feedback on the PR: as it is now, if the current state is configured with multiple triggers that expect different parameters from each other, GetTriggersWithUnmetGuardConditions throws an exception as it attempts to invoke all the guards with the same parameters, so there might need to be some additional parameter/overload to decide how mismatched parameters would be handled.

Mike

andrewmckaskill commented 3 months ago

Thanks for the feedback Mike. I've had a review of the code and yes, CanFire does meet my needs. I thought I had scoured the codebase thoroughly, but I guess I missed it. I'll go ahead and cancel this PR.

Thanks!