Next comes transition handlers. A transition hadler is created by implementing ITransitionHandler interface. Below is an example of a generic transition handler that can be used to abstract common functionalities in our Service Ticket example.

Implementation approach is left to the user. It could from just hard coding for simple cases to database backed storage and retrieval for more advanced cases.

Here is an example implementation (hard coded for simplicity)

publicICollection<StateTransition>GetTransitions(){ICollection<StateTransition>map=newList<StateTransition>();// Define all possible transitions, and its source and target statesmap.Add(newStateTransition("?","Transition_New","New"));// ? means null or undefined statemap.Add(newStateTransition("New","Transition_Open","Open"));map.Add(newStateTransition("Open","Transition_Close","Closed"));map.Add(newStateTransition("Closed","Transition_ReOpen","Open"));map.Add(newStateTransition("?","Transition_Cancel","Cancelled"));returnmap;}publicIDictionary<string,ITransitionHandler<ServiceTicket,ServiceTicketState>>GetTransitionHandlersMap(){IDictionary<string,ITransitionHandler<ServiceTicket,ServiceTicketState>>map=newDictionary<string,ITransitionHandler<ServiceTicket,ServiceTicketState>>();// create a map of all available transitions and their respective transition handlersmap.Add("Transition_New",newNewTransitionHandler());map.Add("Transition_Open",newOpenTransitionHandler());map.Add("Transition_Close",newCloseTransitionHandler());map.Add("Transition_ReOpen",newOpenTransitionHandler());returnmap;}publicICollection<ServiceTicketState>AllStates(){List<ServiceTicketState>states=newList<ServiceTicketState>();// create a list of all possible statesstates.Add(newServiceTicketState(){Code="New",Name="New Ticket"});states.Add(newServiceTicketState(){Code="Open",Name="Ticket Open"});states.Add(newServiceTicketState(){Code="Closed",Name="Ticket Closed"});states.Add(newServiceTicketState(){Code="Cancelled",Name="Ticket Cancelled"});returnstates;}

Now ServiceTicket entity can be transitioned through state machine. Create an instance of StateMachine as follows.