You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A flexible finite state machine (FSM) library for .NET, supporting .NET Standard 2.0+, .NET Framework 4.6.1+, and .NET 5/6/7/8/9. FsmNet is a C# library designed to create and manage Finite State Machines (FSMs) in a simple and efficient way. It allows developers to define states, transitions, and events, enabling the modeling of complex behaviors in a structured manner.
4
4
5
-
## Features
6
-
-**State Management**: Easily define and manage states in your FSM. Strongly-typed FSMs using enums for states
7
-
-**Transition Handling**: Transition conditions and side effects.
8
-
-**Serialization**: Serialization to/from JSON and YAML.
9
-
-**Builder Pattern**: Builder pattern for easy FSM construction.
10
-
-**Extensible**: Easily extend the library to fit specific needs or integrate with other systems.
11
-
-**Cross-Platform**: Multi-targeted for broad .NET compatibility
5
+
## Quick Start
12
6
13
-
## Installation
14
7
Install FsmNet via NuGet Package Manager or dotnet.
-**State Management**: Easily define and manage states in your FSM. Strongly-typed FSMs using enums for states
33
+
-**Transition Handling**: Transition conditions and side effects.
34
+
-**Serialization**: Serialization to/from JSON and YAML.
35
+
-**Builder Pattern**: Builder pattern for easy FSM construction.
36
+
-**Extensible**: Easily extend the library to fit specific needs or integrate with other systems.
37
+
-**Cross-Platform**: Multi-targeted for broad .NET compatibility
38
+
39
+
## Concepts
40
+
41
+
### State
42
+
A state represents a distinct condition or situation in the lifecycle of an object or process. In FsmNet, states are typically defined using an enum (e.g., Open, Closed, InProgress).
43
+
44
+
### Transition
45
+
A transition is a rule that defines how the FSM moves from one state to another.
46
+
Each transition specifies:
47
+
- The source state (where the transition starts)
48
+
- The target state (where the transition ends)
49
+
- An optional condition (a function that must return true for the transition to occur)
50
+
- An optional side effect (an action to execute when the transition happens)
51
+
52
+
### Condition
53
+
A condition is a predicate (function) that determines if a transition is allowed, based on the current context.
54
+
Example: `ctx => ctx.IsAgentAssigned`
55
+
56
+
### Side Effect
57
+
A side effect is an action that is executed when a transition occurs.
The context is an object that holds data relevant to the FSM’s operation and is passed to conditions and side effects.
62
+
Example: a `TicketContext` with properties like `IsAgentAssigned`.
63
+
64
+
### State Machine Definition
65
+
A state machine definition describes all possible states, transitions, the initial state, and associated logic for an FSM.
66
+
67
+
## Advantage of using FSM
68
+
69
+
Using a finite state machine (FSM) for managing state offers several advantages over ad-hoc approaches (like scattered conditionals, flags, or event-driven code) and even over some object-oriented state patterns. Here are the key benefits:
70
+
71
+
1. Clarity and Explicitness
72
+
- All possible states and transitions are explicitly defined.
73
+
- The system’s behavior is easy to visualize, reason about, and document.
74
+
- Reduces ambiguity and hidden state changes.
75
+
2. Predictability and Robustness
76
+
- Transitions are controlled and validated.
77
+
- Only allowed transitions can occur, preventing invalid or unexpected state changes.
78
+
- Makes it easier to handle edge cases and errors.
79
+
3. Maintainability
80
+
- Adding or modifying states and transitions is straightforward.
81
+
- Changes are localized to the FSM definition, not scattered across the codebase.
82
+
- Reduces the risk of introducing bugs when requirements change.
83
+
4. Testability
84
+
- FSMs are easy to test.
85
+
- You can systematically test all states and transitions.
86
+
- Makes it easier to write unit tests for state-dependent logic.
87
+
5. Separation of Concerns
88
+
- State logic is separated from business logic.
89
+
- Conditions and side effects are encapsulated, making the codebase cleaner and more modular.
90
+
6. Scalability
91
+
- FSMs scale well as complexity grows.
92
+
- Adding new states or transitions does not exponentially increase code complexity, unlike nested if/else or switch statements.
93
+
7. Visualization and Documentation
94
+
- FSMs can be visualized as state diagrams.
95
+
- This aids in communication with stakeholders and helps onboard new developers.
fsm.TryTransitionTo(OrderState.Returned, context); // Moves to Returned, notifies return
154
263
```
155
264
156
-
## Concepts
157
-
158
-
### State
159
-
A state represents a distinct condition or situation in the lifecycle of an object or process. In FsmNet, states are typically defined using an enum (e.g., Open, Closed, InProgress).
160
-
161
-
### Transition
162
-
A transition is a rule that defines how the FSM moves from one state to another.
163
-
Each transition specifies:
164
-
- The source state (where the transition starts)
165
-
- The target state (where the transition ends)
166
-
- An optional condition (a function that must return true for the transition to occur)
167
-
- An optional side effect (an action to execute when the transition happens)
168
-
169
-
### Condition
170
-
A condition is a predicate (function) that determines if a transition is allowed, based on the current context.
171
-
Example: `ctx => ctx.IsAgentAssigned`
172
-
173
-
### Side Effect
174
-
A side effect is an action that is executed when a transition occurs.
The context is an object that holds data relevant to the FSM’s operation and is passed to conditions and side effects.
179
-
Example: a `TicketContext` with properties like `IsAgentAssigned`.
180
-
181
-
### State Machine Definition
182
-
A state machine definition describes all possible states, transitions, the initial state, and associated logic for an FSM.
183
265
184
-
## Advantage of using FSM
266
+
## Serialization/Deserialization Example
185
267
186
-
Using a finite state machine (FSM) for managing state offers several advantages over ad-hoc approaches (like scattered conditionals, flags, or event-driven code) and even over some object-oriented state patterns. Here are the key benefits:
268
+
1. JSON
269
+
```csharp
270
+
usingSystem.Text.Json;
187
271
188
-
1. Clarity and Explicitness
189
-
- All possible states and transitions are explicitly defined.
190
-
- The system’s behavior is easy to visualize, reason about, and document.
191
-
- Reduces ambiguity and hidden state changes.
192
-
2. Predictability and Robustness
193
-
- Transitions are controlled and validated.
194
-
- Only allowed transitions can occur, preventing invalid or unexpected state changes.
195
-
- Makes it easier to handle edge cases and errors.
196
-
3. Maintainability
197
-
- Adding or modifying states and transitions is straightforward.
198
-
- Changes are localized to the FSM definition, not scattered across the codebase.
199
-
- Reduces the risk of introducing bugs when requirements change.
200
-
4. Testability
201
-
- FSMs are easy to test.
202
-
- You can systematically test all states and transitions.
203
-
- Makes it easier to write unit tests for state-dependent logic.
204
-
5. Separation of Concerns
205
-
- State logic is separated from business logic.
206
-
- Conditions and side effects are encapsulated, making the codebase cleaner and more modular.
207
-
6. Scalability
208
-
- FSMs scale well as complexity grows.
209
-
- Adding new states or transitions does not exponentially increase code complexity, unlike nested if/else or switch statements.
210
-
7. Visualization and Documentation
211
-
- FSMs can be visualized as state diagrams.
212
-
- This aids in communication with stakeholders and helps onboard new developers.
272
+
// Serialization
273
+
vardefinition=builder.Build(); // Ensure build is called before calling ToSerializable
0 commit comments