-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateTransitionManager.cs
124 lines (113 loc) · 5.58 KB
/
StateTransitionManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
namespace StateTransition;
public partial class StateMachine<TState, TTrigger, TEntity>
{
public class StateTransitionManager
{
private readonly State _state;
private readonly Func<TState, State> _stateLookup;
private readonly Func<BaseTransitionOptions> _defaultTransitionOptionsLookup;
internal StateTransitionManager(State state, Func<TState, State> stateLookup, Func<BaseTransitionOptions> defaultTransitionOptionsLookup)
{
_state = state;
_stateLookup = stateLookup;
_defaultTransitionOptionsLookup = defaultTransitionOptionsLookup;
}
/// <summary>
/// Adds transition for the current state configuration to the destination state with required custom transition
/// options to control a transitioning process
/// In addition, it allows to set specified action and guard
/// </summary>
/// <param name="destinationState">The state that the trigger will cause a transition to</param>
/// <param name="trigger">Trigger which fires a transition to the destination state</param>
/// <param name="customTransitionOptions">Allows to use custom transition options</param>
/// <param name="transitionAction">Action to execute during transitioning process</param>
/// <param name="guardExpression">
/// Transition can be done only if a specified guard is met for the associated trigger.
/// Happy path is when we found a single trigger resolver and its guard met conditions.
/// By default, when guard is not defined, then we will allow transition without checking conditions.
/// </param>
/// <returns>The state configuration which describes transitioning from this state.</returns>
public StateTransitionManager AddTransitionTo<TOptions>(
TState destinationState,
TTrigger trigger,
Action<TOptions> customTransitionOptions,
ITransitionAction transitionAction = default,
Func<TEntity, bool> guardExpression = default) where TOptions : BaseTransitionOptions
{
TOptions options;
var defaultOptions = _defaultTransitionOptionsLookup();
if (defaultOptions is not null)
{
if (defaultOptions is not TOptions transitionOptions)
{
throw new ArgumentException($"Type of the custom transition options ({typeof(TOptions).Name}) differs from the default ({defaultOptions.GetType().Name})");
}
options = transitionOptions;
}
else
{
options = Activator.CreateInstance<TOptions>();
}
customTransitionOptions.Invoke(options);
return MapTransitionToTriggerStateResolver(_state, destinationState, trigger, transitionAction, guardExpression, options);
}
/// <summary>
/// Adds transition for the current state configuration to the destination state.
/// In addition, it allows to set specified action and guard
/// </summary>
/// <param name="destinationState">The state that the trigger will cause a transition to</param>
/// <param name="trigger">Trigger which fires a transition to the destination state</param>
/// <param name="transitionAction">Action to execute during transitioning process</param>
/// <param name="guardExpression">
/// Transition can be done only if a specified guard is met for the associated trigger.
/// Happy path is when we found a single trigger resolver and its guard met conditions.
/// By default, when guard is not defined, then we will allow transition without checking conditions.
/// </param>
/// <returns>The state configuration which describes transitioning from this state.</returns>
public StateTransitionManager AddTransitionTo(
TState destinationState,
TTrigger trigger,
ITransitionAction transitionAction = default,
Func<TEntity, bool> guardExpression = default)
{
return MapTransitionToTriggerStateResolver(_state, destinationState, trigger, transitionAction, guardExpression);
}
/// <summary>
/// Specify an action that will be executed before state transitioning from the source to specified destination state
/// </summary>
public StateTransitionManager BeforeTransitionTo(TState destinationState, ITransitionAction action)
{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
_state.BeforeTransitionTo(destinationState, action);
return this;
}
/// <summary>
/// Specify an action that will be executed after state transitioning from the source to specified destination state
/// </summary>
public StateTransitionManager AfterTransitionTo(TState destinationState, ITransitionAction action)
{
var state = _stateLookup(destinationState);
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}
state.AfterTransitionFrom(_state.Current, action);
return this;
}
private StateTransitionManager MapTransitionToTriggerStateResolver(State state,
TState destinationState,
TTrigger trigger,
ITransitionAction transitionAction = default,
Func<TEntity, bool> guardExpression = default,
BaseTransitionOptions options = default)
{
state.MapTo(transitionAction is not null
? new TransitionActionTriggerStateResolver(trigger, destinationState, options ?? _defaultTransitionOptionsLookup(), guardExpression, transitionAction)
: new TransitionTriggerStateResolver(trigger, destinationState, options ?? _defaultTransitionOptionsLookup(), guardExpression));
return this;
}
}
}