-
Notifications
You must be signed in to change notification settings - Fork 77
WorkflowDefinition
Rico Suter edited this page Jun 3, 2015
·
2 revisions
- Package: MyToolkit (PCL)
- Platforms: All (PCL)
A WorkflowDefinition acts as template for multiple WorkflowInstance objects.
A workflow definition consists of optional global parameters (an object of type WorkflowParametersBase), activities (WorkflowActivityBase objects), a starting activity and transitions (WorkflowTransition objects):
var workflow = new WorkflowDefinition();
workflow.Parameters = new MyParameters();
workflow.Activities = new List<WorkflowActivityBase>
{
new MyActivity { Id = "1" },
new MyActivity { Id = "2" },
new MyActivity { Id = "3" },
};
workflow.StartActivity = workflow.Activities.First();
workflow.Transitions = new List<WorkflowTransition>
{
new WorkflowTransition { From = "1", To = "2" },
new WorkflowTransition { From = "2", To = "3" },
};
return workflow;
After defining a workflow definition, we can create WorkflowInstance objects from it:
var instance = workflow.CreateInstance();
The instance is now in the initial state and has the StartActivity()
in the list of current activities.
A workflow definition object can be serialized and deserialized from XML:
var xml = workflow.ToXml();
var workflow = WorkflowDefinition.FromXml(xml, activityTypes, parameterTypes);
As we can see, the FromXml()
method needs to know all the possible activity and parameter types so that the serialization can work. These types could be loaded using reflection:
var activityTypes = Assembly.GetExecutingAssembly()
.DefinedTypes
.Where(t => typeof(WorkflowActivityBase).IsAssignableFrom(t))
.ToArray();
var parameterTypes = Assembly.GetExecutingAssembly()
.DefinedTypes
.Where(t => typeof(WorkflowParametersBase).IsAssignableFrom(t))
.ToArray();