Stateflows

Structr.Stateflows package provides functionality for modeling behavior of state-driven entities by creating and using state machines in your .NET application. It provides simple yet functional API via Stateflow object that encapsulate methods and properties to monitor available actions and change state of controlled entity.

Installation

Stateflows package is available on NuGet.

dotnet add package Structr.Stateflows

Setup

Configure stateflow services:

services.AddStateflows(typeof(Program).Assembly);

AddStateflows() extension method performs registration of all configurator classes that implement IStateMachineConfigurator and stateflow providers that implement IStateflowProvider.

Param name
Param type
Description

assembliesToScan

params Assembly[]

List of assemblies to search configurator classes and stateflow providers.

configureOptions

Action<StateflowServiceOptions>

Options to be used by state machine provider.

Additionally configure IStateMachineProvider service by specifying it's type and lifetime used StateflowServiceOptions.

StateflowServiceOptions properties:

Property name
Property type
Description

ProviderServiceType

Type

Changes standard implementation of IStateMachineProvider to specified one. Default value is typeof(StateMachineProvider).

ProviderServiceLifetime

ServiceLifetime

Specifies the lifetime of an IStateMachineProvider service. Default value is Transient.

Usage

The basic usage is:

Let's say we have some application where user could create Issue objects containing feedback or comments and publish them:

Every such Issue has one of these states:

Each of such states imply different set of actions, some of which lead to change of state (like Submitting or Solving) and some not (like Editing) Create an enum for all possible actions:

Now u should create configurator class that implements IStateMachineConfigurator for configuring state machine:

Then create IssueStateflowProvider that inheriting from IStateflowProvider and provides access to configured state machine and issue object:

The last step is to inject IIssueStateflowProvider service and use it:

To check available actions for issue on the presentation layer (WebUI for example):

Then in any place of your presentation layer you can check issue action. For example in controller:

Last updated

Was this helpful?