statemachine icon indicating copy to clipboard operation
statemachine copied to clipboard

Extension.EnteredInitialState has context.State == null

Open fubar-coder opened this issue 6 years ago • 4 comments

This prevents getting the IState<TState, TEvent> of the initial status.

Example extension code:

private class CurrentStatusExtension : AsyncExtensionBase<States, Triggers>
{
	private readonly AppccelerateTestData _instance;

	public CurrentStatusExtension(AppccelerateTestData instance)
	{
		_instance = instance;
	}

	public override void EnteredInitialState(
		IStateMachineInformation<States, Triggers> stateMachine,
		States state,
		ITransitionContext<States, Triggers> context)
	{
		// context.State is null ... so there is no way to get the initial state?
		_instance.CurrentStatus = context.State;
	}
}

fubar-coder avatar Mar 16 '18 11:03 fubar-coder

Use the state passed to the method directly. The state of the context is the source state, which is null in case of initialization.

Does this work for you?

ursenzler avatar Mar 16 '18 12:03 ursenzler

No, sorry, I need the IState, because I need to get the valid triggers.

fubar-coder avatar Mar 16 '18 16:03 fubar-coder

I see.

That can be accomplished by changing the code in the method Appccelerate.StateMachine.AsyncMachine.StateMachine.Initialize on line 193 so that it passes the IState to the extension (the same value is already passed to a method the line before).

If you want the change quickly, I suggest you change the code as explained above and compile the state machine on your own. Otherwise, you'll have to wait for me and my time is very limited at the moment.

See http://www.appccelerate.com/howtocontributewalkthrough.html for an explanation how to get your own nuget package easily from our build infrastructure through a pull request.

I hope that helps. Urs

ursenzler avatar Mar 16 '18 16:03 ursenzler

Maybe it might be useful to add a property containing the target state? My assumption was that the context state always contains the target state for all transitioned/initialized events.

fubar-coder avatar Mar 19 '18 08:03 fubar-coder