I have a project that has an almost linear workflow. I am trying to use the .NET Stateless library to act as a processing engine / state machine. The number of examples there is limited, but I have compiled the following code:
private StateMachine<WorkflowStateType, WorkflowStateTrigger> stateMachine; private StateMachine<WorkflowStateType, WorkflowStateTrigger>.TriggerWithParameters<Guid, DateTime> registrationTrigger; private Patient patient; public Patient RegisterPatient(DateTime dateOfBirth) { configureStateMachine(WorkflowState.Unregistered); stateMachine.Fire<DateTime>(registrationTrigger, dateOfBirth); logger.Info("State changed to: " + stateMachine.State); return patient; } private void configureStateMachine(WorkflowState state) { stateMachine = new StateMachine<WorkflowState, WorkflowTrigger>(state); registrationTrigger = stateMachine.SetTriggerParameters<DateTime>(WorkflowTrigger.Register); stateMachine.Configure(WorkflowState.Unregistered) .Permit(WorkflowTrigger.Register, WorkflowStateType.Registered); stateMachine.Configure(WorkflowState.Registered) .Permit(WorkflowTrigger.ScheduleSampling, WorkflowState.SamplingScheduled) .OnEntryFrom(registrationTrigger, (dateOfBirth) => registerPatient(dateOfBirth)); } private void registerPatient(DateTime dateOfBirth) {
As you can see, I use Stateless Fire () overload, which allows me to pass the trigger. This means that I can have the business logic of the state machine process, in this case, a code for registering a new patient.
It all works, but now I would like to move all the code of the destination computer to another class in order to encapsulate it, and I am having problems with this. The problems that I had with this are the following:
- Creating an instance of a
StateMachine object requires a state, and State requires the readonly property, which can only be set when an instance is created. - my
registrationTrigger must be created during state machine configuration, and must also be accessible to the calling class.
How can I overcome these elements and encapsulate the state machine code?
c # stateless-state-machine
im1dermike
source share