88 /// <summary>
99 /// Builder for creating a finite state machine based on an enumeration type.
1010 /// </summary>
11- /// <typeparam name="TState"></typeparam>
12- /// <typeparam name="TContext"></typeparam>
11+ /// <typeparam name="TState">State Enum Type </typeparam>
12+ /// <typeparam name="TContext">Context Type </typeparam>
1313 public class FiniteStateMachineBuilder < TState , TContext > where TState : struct , Enum
1414 {
15- private readonly string _workItemType ;
15+ private readonly string _entityType ;
1616 private TState ? _initial ;
1717 private readonly HashSet < TState > _states = new HashSet < TState > ( ) ;
1818 private readonly List < ITransition < TContext > > _transitions = new List < ITransition < TContext > > ( ) ;
1919
2020 /// <summary>
21- /// Initializes a new instance of the <see cref="FiniteStateMachineBuilder{TState, TContext}"/> class with the specified work item type.
21+ /// Initializes a new instance of the <see cref="FiniteStateMachineBuilder{TState, TContext}"/> class with the specified entity type.
2222 /// </summary>
23- /// <param name="workItemType "></param>
24- private FiniteStateMachineBuilder ( string workItemType ) => _workItemType = workItemType ;
23+ /// <param name="entityType "></param>
24+ private FiniteStateMachineBuilder ( string entityType ) => _entityType = entityType ;
2525
2626 /// <summary>
27- /// Creates a new instance of the <see cref="FiniteStateMachineBuilder{TState, TContext}"/> for the specified work item type.
27+ /// Creates a new instance of the <see cref="FiniteStateMachineBuilder{TState, TContext}"/> for the specified entity type.
2828 /// </summary>
2929 /// <param name="workItemType"></param>
3030 /// <returns></returns>
@@ -63,7 +63,7 @@ public TransitionBuilder AddTransition(TState from, TState to)
6363 public EnumStateMachineDefinition < TState , TContext > Build ( )
6464 {
6565 if ( _initial is null ) throw new InvalidOperationException ( "Initial state not specified." ) ;
66- return new EnumStateMachineDefinition < TState , TContext > ( _workItemType , _states , _transitions , _initial . Value ) ;
66+ return new EnumStateMachineDefinition < TState , TContext > ( _entityType , _states , _transitions , _initial . Value ) ;
6767 }
6868
6969 /// <summary>
@@ -76,7 +76,7 @@ public SerializableStateMachine ToSerializable()
7676 if ( _initial is null ) throw new InvalidOperationException ( "Initial state must be defined." ) ;
7777 return new SerializableStateMachine
7878 {
79- EntityType = _workItemType ,
79+ EntityType = _entityType ,
8080 InitialState = _initial . ToString ( ) ,
8181 States = _states . Select ( s => s . ToString ( ) ) . ToList ( ) ,
8282 Transitions = _transitions . Select ( t => new SerializableTransition
@@ -102,33 +102,59 @@ public class TransitionBuilder
102102 private string _conditionName ;
103103 private string _sideEffectName ;
104104
105+ /// <summary>
106+ /// Initializes a new instance of the <see cref="TransitionBuilder"/> class with the specified parent builder and states.
107+ /// </summary>
108+ /// <param name="parent"></param>
109+ /// <param name="from"></param>
110+ /// <param name="to"></param>
105111 public TransitionBuilder ( FiniteStateMachineBuilder < TState , TContext > parent , TState from , TState to )
106112 {
107113 _parent = parent ;
108114 _from = from ;
109115 _to = to ;
110116 }
111117
118+ /// <summary>
119+ /// Defines a condition for the transition. The condition is a function that takes the context and returns a boolean indicating whether the transition can occur.
120+ /// </summary>
121+ /// <param name="condition"></param>
122+ /// <param name="name"></param>
123+ /// <returns></returns>
112124 public TransitionBuilder When ( Func < TContext , bool > condition , string name = null )
113125 {
114126 _condition = condition ;
115127 _conditionName = name ;
116128 return this ;
117129 }
118130
131+ /// <summary>
132+ /// Defines a side effect for the transition. The side effect is an action that takes the context and is executed when the transition occurs.
133+ /// </summary>
134+ /// <param name="effect"></param>
135+ /// <param name="name"></param>
136+ /// <returns></returns>
119137 public TransitionBuilder WithSideEffect ( Action < TContext > effect , string name = null )
120138 {
121139 _sideEffect = effect ;
122140 _sideEffectName = name ;
123141 return this ;
124142 }
125143
144+ /// <summary>
145+ /// Finalizes the transition definition and adds it to the parent finite state machine builder's list of transitions.
146+ /// </summary>
147+ /// <returns></returns>
126148 public FiniteStateMachineBuilder < TState , TContext > Done ( )
127149 {
128150 _parent . _transitions . Add ( new EnumTransition < TState , TContext > ( _from , _to , _condition , _sideEffect , _conditionName , _sideEffectName ) ) ;
129151 return _parent ;
130152 }
131153
154+ /// <summary>
155+ /// Implicitly converts the TransitionBuilder to a FiniteStateMachineBuilder, allowing for method chaining.
156+ /// </summary>
157+ /// <param name="b"></param>
132158 public static implicit operator FiniteStateMachineBuilder < TState , TContext > ( TransitionBuilder b ) => b . Done ( ) ;
133159 }
134160
0 commit comments