Skip to content
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

Open
paul42 opened this issue May 13, 2019 · 6 comments

Comments

@paul42
Copy link

paul42 commented May 13, 2019

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:

GetTicketByIdMachine
NewUpdateToTicketMachine
NewTicketMachine

I was hoping to have each machine just inherit from a base class that has an abstract methods for ValidateIncomingRequest and RequestValidatedCallback since I want to have the 'CompletedSuccessfullyandInternalErrorStates` 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!

@paul42
Copy link
Author

paul42 commented May 15, 2019

Alternatively does anyone have any patterns for how they do items sequentially?

@mclift
Copy link
Member

mclift commented May 15, 2019

Typically when I need to apply a general-use transition such as an error handling state, I'd use substates and configure the Permitted trigger on the parent state. I expect I'm missing some details, but could this be a useful approach for you? If not, can you elaborate a little?

    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);
        }
    }

@paul42
Copy link
Author

paul42 commented May 15, 2019

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!

@mclift
Copy link
Member

mclift commented May 15, 2019

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

@HenningNT
Copy link
Contributor

@paul42 I think you are on the right track.
I did something similar on a project I worked on a few years ago. Since you can call .Configure() repeatedly you can add more trigger handlers in your specialized classes.

@HenningNT
Copy link
Contributor

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:
https://github.com/HenningNT/ObjectsAsStates/blob/master/ObjectsAsStates/ObjectsAsStates/Program.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants