Reputation: 899
I'm new to MassTransit, and I can't seem to figure out how it maps the States that I define on a saga (MassTransitStateMachine) to the "State" property on the related SagaStateMachineInstance class.
For example, if I have a state machine class with three states:
public class MySaga :
MassTransitStateMachine<MySagaState>
{
public State Executing { get; private set; }
public State Completed { get; private set; }
public State Failed { get; private set; }
...
}
And my state machine instance class has a "State" property
public class MySagaState : SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }
public int State { get; set; }
}
How does MT decide which saga state is which integer?
There also appear to be two built-in saga states, "Initial" and "Final", so this example would have 5 states. How is the State to integer mapping done?
Upvotes: 1
Views: 3441
Reputation: 33565
You need to specify the states in order to assign numbers to them as part of the state machine definition:
public class MySaga :
MassTransitStateMachine<MySagaState>
{
public MySaga()
{
InstanceState(x => x.State, Executing, Completed, Failed);
// 1 = Initial, 2 = Final, 3 = Executing, 4 = Completed
// 5 - Failed (1 & 2 are built-in states)
}
public State Executing { get; private set; }
public State Completed { get; private set; }
public State Failed { get; private set; }
...
}
Upvotes: 6
Reputation: 19640
The State
property indicates the current state machine state. It changes whenever the state machine gets to another state.
It can either be of a complex type State
or primitive type string
or int
. The first case, if you use database persistence, the Name
property of the State
object will be stored in the database. If you use string
(all MassTransit examples suggest this) - it is obvious that the property will contain the state name, representing the state machine property name. If you use int
- it is the hardest to understand by looking at the value, since it has come convention like zero is no state, one is the initial state, 2 is the final state and 3 onwards represent all other states.
I would really suggest using string
, since it is easier to map and there is no confusion.
So your saga state will look like:
public class MySagaState : SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }
public string CurrentState { get; set; }
}
you should also configure the state property of the state machine state object in the state machine constructor:
InstanceState(x => x.CurrentState);
Along the execution of this saga, this property will get values Executing
, Completed
or Failed
as you have defined the state properties.
Upvotes: 3