I've recently read about the boost::statechart
library (finite state machines) and I loved the concept.
Does C# have a similar mechanism ? Or can it be implemented using a specific design pattern?
I've recently read about the boost::statechart
library (finite state machines) and I loved the concept.
Does C# have a similar mechanism ? Or can it be implemented using a specific design pattern?
Check out Stateless -> http://code.google.com/p/stateless/. Its a lightweight alternative to the heavier WWF.
Here's a couple of articles by the author of the tool:
State Machines in Domain Models
Parameterised Triggers and Re-entrant States in Stateless
Workflow Foundation (.NET 3.0) has a state machine workflow. 4.0 doesn't have exactly the same thing currently, but you can definitely create a state machine workflow using 4.0.
The things that come near to FSMs are workflows in .NET 3.5, however, also workflows are not exactly FSMs.
The power of using FSMs is that you can create them explicitly in your code, having less chance of creating bugs. Besides, of course some systems are FSMs by nature, so it is more natural to code them like so.
.NET 4 Update 1 now supports it in the following class: System.Activities.Statements.StateMachine
Here is a tutorial on how to use it. Here's a hands on lab.
Windows Workflow Foundation (WF) that is part of the base class library in 3.0 and 3.5 includes a state-machine workflow design to manage state machines for your applications.
They have completely rewritten workflow for the upcoming 4.0 release, and the new WF 4.0 classes do not natively support state-machines, but all of the 3.0/3.5 classes are still fully supported under 4.0.
Yes, C# has iterator blocks which are compiler-generated state machines.
If you wish to implement you own state machine you can create custom implementations of the
IEnumerable<T>
andIEnumerator<T>
interfaces.Both of these approaches highlight the .NET framework's implementation of the iterator pattern.