How to encapsulate .NET Stateless state machine : <p>I have a project where there is a mostly linear workflow.  I'm attempting to use the .NET Stateless <a href="https://github.com/dotnet-state-machine/stateless" rel="noreferrer">library</a> to act as workflow engine/state machine.  The number of examples out there is limited, but I've put together the following code:</p>

<pre><code>private StateMachine&lt;WorkflowStateType, WorkflowStateTrigger&gt; stateMachine;
private StateMachine&lt;WorkflowStateType, WorkflowStateTrigger&gt;.TriggerWithParameters&lt;Guid, DateTime&gt; registrationTrigger;
private Patient patient;

public Patient RegisterPatient(DateTime dateOfBirth)
{
    configureStateMachine(WorkflowState.Unregistered);
    stateMachine.Fire&lt;DateTime&gt;(registrationTrigger, dateOfBirth);
    logger.Info("State changed to: " + stateMachine.State);
    return patient;
}

private void configureStateMachine(WorkflowState state)
{
    stateMachine = new StateMachine&lt;WorkflowState, WorkflowTrigger&gt;(state);

    registrationTrigger = stateMachine.SetTriggerParameters&lt;DateTime&gt;(WorkflowTrigger.Register);

    stateMachine.Configure(WorkflowState.Unregistered)
        .Permit(WorkflowTrigger.Register, WorkflowStateType.Registered);

    stateMachine.Configure(WorkflowState.Registered)
        .Permit(WorkflowTrigger.ScheduleSampling, WorkflowState.SamplingScheduled)
        .OnEntryFrom(registrationTrigger, (dateOfBirth) =&gt; registerPatient(dateOfBirth));
}

private void registerPatient(DateTime dateOfBirth)
{
    //Registration code
}
</code></pre>

<p>As you can see, I'm using the Stateless Fire() overload that allows me to pass in a trigger.  This is so I can have the state machine process business logic, in this case, code to register a new patient.</p>

<p>This all works, but now I'd like to move all the state machine code into another class to encapsulate it and I'm having trouble doing this.  The challenges I've had in doing this are:</p>

<ul>
<li>instantiating a <code>StateMachine</code> object requires you to specify state and <code>State</code> is a readonly property that can only be set at instantiation.</li>
<li>my <code>registrationTrigger</code> has to be instantiated during state machine configuration and also has to be available by the calling class.</li>
</ul>

<p>How can I overcome these items and encapsulate the state machine code?</p>
