dotnet-state-machine / stateless

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

How To Use dynamic Stateless #554

Open Mr-Mohammad opened 6 months ago

Mr-Mohammad commented 6 months ago

Hi, What solution do you have for me to use stateless dynamically?

mclift commented 6 months ago

Hi,

Can you give us any more details on what you need to achieve?

If you're looking for a way to modify the state machine's transitions at runtime based on external changes, you could look at guard conditions with PermitIf. You could also look at dynamic transitions, which select a destination state based on a delegate and are configured with PermitDynamic – this is an area that could do with better documentation, but the unit tests should give you an idea of how these work.

Mr-Mohammad commented 6 months ago

I want to design the process first with Json and then let Stateless execute it

gius commented 2 months ago

Hi, do you have any update on this topic? It would be great to be able to (de)serialize a state machine from JSON/XML.

I can imagine dynamically calling the fluent functions to build a state machine based on some template, but feels like a hack to me 🤷

HenningNT commented 1 month ago

Just to be clear, are we talking about storing some object state to database, and then reading in the object into the same state? The state machine itself is stateless, so it supports de/serializing an object state, please see the JsonExample.

Are we talking about reading in a file that defines the state machine, the guards and actions? I don't think Stateless will ever support that, unless someone decides they really want that feature, and builds it themselves.

mclift commented 1 month ago

I agree with @HenningNT that this isn't likely to be something Stateless would support as a built-in feature and may be better suited to a separate project. All of the state machines I've come across in my work have custom logic (e.g. PermitIf, PermitDynamic, etc.) with dependencies on the application's internals. It shouldn't be too difficult to write a parser to build very simple state machines from Mermaid, DOT graph or some other structured text format, but I don't see a path to building a parser that could include conditionals, dynamic transitions, entry behaviours, or any other custom logic without significant development effort.

All that said, there's a really interesting project idea there and I'd love to see what people would come up with.