State Machine Frameworks for .NET

2019-03-12 09:09发布

问题:

We have a system at my work that is basically a message-driven state machine. It takes in a variety of types of messages, looks up some context/state based on the message, then decides what to do, based on the message and the current state. Normally the result is a message being sent out of the system.

Are there any good open-source frameworks for implementing a state machine in .NET? I've looked into the latest release of Windows Workflow, and it seems like it would be a good option; however, I have some concerns about the default persistence mechanisms (we need to report off the state transition data), testability, and the fact that the WF team is re-architecting the framework as we speak (supposedly).

Instead of WF, I'm thinking of trying to implement a plain-old GoF "State" pattern, and using Spring.NET to wire everything together. Is there anything out there that already does this, or something similar?

回答1:

Depending on your exact requirements you might find the State Machine Compiler is sufficient for implementing the GoF "State" pattern. It can generate C# as well as dot which can be transformed into several image formats using Graphviz. I've used it on a previous project and found it sufficient for generating a 'simple' state machine. I hope this helps.



回答2:

Take a look at NState

https://github.com/benaston/NState

https://nuget.org/packages/NState/.

Disclaimer: I maintain it.



回答3:

Two great state machines for .NET

  • Stateless - Create state machines and lightweight state machine-based workflows directly in .NET code
  • Automatonymous - A state machine library for .Net - allows you to write fluent style state machines


回答4:

I maintain an open-source project which implements (among other things) a generic finite state machine for .NET. It is built on top of QuickGraph, so you get many graph-analysis algorithms for free. See this page for more information.



回答5:

You might consider the North State Framework in C#. Although it is not open source, it is relatively inexpensive to license. It is the only framework that I know of that allows reuse through state machine composition and inheritance. In other words, you can inherit from a base state machine with only a few lines of code, and add states, transitions, actions, etc. You can also take an existing state machine class and "plug it into" another state machine. You won't believe how much easier it is to implement and work with state machines using this framework, relative to GOF or other patterns.



回答6:

Have a look at StateBuilderDotNet, a state machine code generator that transforms an XML description of a state machine into an extended version of the C# state pattern. It supports all UML semantics: state machine can be hierarchical, concurrent and asynchronous. A graphical tool is also available to create state machines.

Disclaimer: I'm the author of these software.



回答7:

I've created Staty which is a smart state-machine for .NET. Written as C# portable class library (PCL), Staty comes as an expressive event-driven state-machine with features like

  • concurrence-safety
  • external and interal events
  • separate classes per state
  • entry and exit actions and
  • a fluent syntax to configure the state-machine.

It's free, open-source, available on NuGet and based on ideas by David Lafreniere's C++ State Machine, which is the most famous state-machine in C++.