-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Possible to share common transitions / specialize different classes? #317
Comments
Alternatively does anyone have any patterns for how they do items sequentially? |
Typically when I need to apply a general-use transition such as an error handling state, I'd use substates and configure the public enum MyTrigger
{
Next,
Error
}
public enum MyState
{
ParentState,
State1,
State2,
HandleError
}
class Program
{
static void Main(string[] args)
{
var stateMachine = new StateMachine<MyState, MyTrigger>(MyState.State1);
stateMachine.Configure(MyState.State1).SubstateOf(MyState.ParentState);
stateMachine.Configure(MyState.State2).SubstateOf(MyState.ParentState);
stateMachine.Configure(MyState.ParentState).Permit(MyTrigger.Error, MyState.HandleError);
}
} |
I think I get it a bit more - so I'd make all my base states and then for my more specialized machines, I'd just have additional states and configurations, and in the methods in the more specialized machines, if I fire a permitted trigger it'll handle the transition based on the already configured machine? Do you have any other code laying around where you use this? I think I'm starting to get it, and really appreciate your help! |
Good to hear I wasn’t too far off the track! I’m not at a machine with Visual Studio right now, but there are a handful of samples using substates in the unit tests, e.g. here: https://github.com/dotnet-state-machine/stateless/blob/07b92c56dbf2621abec9b90c55c42691fd8cd754/test/Stateless.Tests/StateMachineFixture.cs |
@paul42 I think you are on the right track. |
In case someone finds this question looking for starters on using objects for states and/or triggers, I've put together a small example here: |
Hey all, love the project but just wondering if there a way to have state machines inherit base transitions so I'm not copy/pasting default 'error' transition states for my specialized machines - I'm possibly coming to state machines in the wrong way, but right now I have it like this:
I was hoping to have each machine just inherit from a base class that has an abstract methods for
ValidateIncomingRequest
andRequestValidatedCallback
since I want to have the 'CompletedSuccessfullyand
InternalErrorStates` handle reporting the same way - but each machine has a different amount of states / actions for various work.also is there a way to use classes for states, like in #234 ? There was no code posted so I am curious what your base-class looks like.
Thanks in advance!
The text was updated successfully, but these errors were encountered: