Tuesday, August 5, 2014

XAML as DSL - Load and saving Windows workflow foundation workflows

Context

Windows Workflow Foundation has got better after MSFT introduced the WWF 4.0. I don't think, it was stable earlier. We are now using WWF, in one of our project. The main reasons are as follows
  • There is a complex business scenario (similar to recommendation algorithm) which business experts itself, is not clear how to do it. They want to try/test the algorithm in production data using a different tool. During that test they may need to alter the algorithm and change configurations to try various possibilities. Since the production data is spread across different countries and some don't allow the data to be carried out of the country, its not feasible to have a developer going with the business testing team and alter the algorithm.
  • During the above testing conducted by business experts, they want to try different algorithms (copy and modify original) on same data and compare the results.

Why XAML as DSL

This clearly points to the need of a Domain Specific Language to code the algorithm. As mentioned in one of my previous posts about DSL, either we can develop a DSL with its own parser and all or leverage existing languages to manipulate the entities. Creating own domain specific language with its own parser is not feasible as it takes more time.We could easily use PowerShell or Javascript as DSL, but the problem is the tooling. So we took XAML as DSL to code the algorithm. Decision factors below.
  • The business want to change the algorithm on the fly.
  • Business people are not good coders and its difficult for them to understand by looking at program as text. So they need a UI to edit algorithm (Implemented using DSL)
  • There is not much time to create tooling for existing languages such as PowerShell or Javascript

Advantages

XAML as DSL is being executed by the WWF engine. If we see the WWF as technology, it has many inbuilt features we can leverage to have it as DSL. Some items listed below.
  • It stores the workflow in clear XAML format
  • It comes with out of the box workflow designer control based on WPF. This workflow editor control can be easily hosted in our WPF application.

How to load and save WWF workflow

Lets come to the topic of saving and loading windows workflow foundation .xaml workflow files. As we saw earlier, the business want to edit the algorithm on the fly. We are keeping the xaml file in file system itself , even though the application has database.Below are the code snippets

Load the WWF workflow.xaml from file

Mainly we need to create the instance of WorkflowDesigner class and invoke it's Load() method with string argument. We can pass the file name as string.

Important thing here is, we don't need to read the file ourselves and pass the workflow XAML. For doing that there is another method.

            _workflowDesigner = new WorkflowDesigner();
            _workflowDesigner.Load(_fileName);
            //Code to Load _workflowDesigner related controls such as _workflowDesigner.View,_workflowDesigner.PropertyInspectorView 
            //into the WPF container

Save modified workflow to file.

Saving the workflow into file too simple. Just invoke the method. Main thing to make sure the permissions on the file. The user who is running the process should have write permission on the file.

            _workflowDesigner.Save(_fileName);

Load and save the workflow from database or any other source

There is Text property in WorkflowDesigner class. Assign the workflow xaml content in string form to the Text property and call Load() method to load the XAML string directly. Read the XAML string from database using traditional techniques and assign to Text property. We may get the modified XAML via the same property to save back to database.

            _workflowDesigner = new WorkflowDesigner();
            _workflowDesigner.Text = GetWorkflowFromDatabaseAsString();
            _workflowDesigner.Load();

References

Loading workflow in WorkflowDesigner
Happy coding.

1 comment:

Blogger said...

If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
right away...

(VIDEO) Get your ex back with TEXT messages?