Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Windows Workflow Foundation Essentials : Page 6

Applications that contain business processes and rules can benefit immediately from Windows Workflow's diagrams, class libraries, runtime, rules engine, and customization capabilities.

Enabling Activity Data Binding
So far, you have created a custom activity that uses a CustomerID property to hold a customer ID it receives as input and a CanCustomerOrder property in which it stores the result of the activity. So, how does the workflow set the activity's CustomerID property? As mentioned earlier, you can do this by means of activity data binding with dependency properties.

First of all, the workflow host program can pass a customer ID to the workflow. The workflow stores this value in its own CustomerID dependency property (which you still need to create), which can in turn be automatically propagated to the activity's CustomerID property.

To set up this activity data binding, you must first create a workflow-level CustomerID property. To do this, right-click the workflow diagram, and select View Code from the shortcut menu. At the top of the ProcessOrderWorkflow definition, before the constructor, use the wdp snippet again to create a property named CustomerIDProperty of type int or Integer, with a description of "Customer ID" and a category of Custom Property. This is the same as the property you added to ValidateCustomerActivity. If you want to clean things up a bit, you can delete the handler at the bottom of the source code file that contains code that writes "Hello WF World" to the console.

Now, right-click the code and select View Designer from the shortcut menu to display the workflow diagram again. Select validateCustomerActivity in the diagram, and then go to the Visual Studio Properties window. As shown in Figure 15, in addition to the standard Description and Enabled properties you should see the CanCustomerOrder, Customer, and CustomerID custom properties. Notice the small information icon next to these properties. If you hover your mouse pointer over the icons, the tooltip text "Bind Property" appears. The tooltip indicates that these are dependency properties that can participate in activity data binding.

Figure 15. Binding Dependency Properties: The Properties window displays a small information icon to indicate dependency properties.
Figure 16. Propagating Values: You can use this dialog to set up activity data binding that automatically propagates values from one dependency property to another.
Select the CustomerID property and click its associated ellipsis button to display a dialog box (see Figure 16) that lists the workflow's dependency properties (there's only the one CustomerID property you just added) as well as the workflow activities and their associated dependency properties. Select the workflow's CustomerID property and click OK. This binds the activity's CustomerID property to the workflow's CustomerID property. Whenever your application sets the workflow's CustomerID property, the workflow now automatically propagates the value to the CustomerID property of the validateCustomerActivity activity.

Passing Parameters to the Workflow
The next step is to have the workflow host program pass a customer ID to the workflow. To do this, go to the Solution Explorer and double-click the Program.cs or Module1.vb file. Find the line of code about halfway down that calls the CreateWorkflow() method. Immediately before this line, add the following code that creates a generic Dictionary. Afterward, change the CreateWorkflow() call so it passes the arguments Dictionary.

In C#:

Dictionary<String, Object> wfArgs = new  Dictionary<string, object>(); wfArgs.Add("CustomerID", 1001); WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof( SequentialWorkflowConsole.ProcessOrderWorkflow), wfArgs);

And in Visual Basic:

Dim wfArgs As Dictionary(Of String, Object) = _ New Dictionary(Of String, Object)() wfArgs.Add("CustomerID", 1001) Dim workflowInstance As WorkflowInstance workflowInstance = _ workflowRuntime.CreateWorkflow(GetType( _ ProcessOrderWorkflow), wfArgs)

Notice this code adds an argument named CustomerID with the hard-coded value 1001 (for demo purposes) to the dictionary. The name of the argument (CustomerID) must exactly match the name of a property on the workflow, or you will get an exception at run time.

Testing Activity Data Binding
Before going further, you should run the workflow to test the activity data binding. You can set two breakpoints—one in code and one on the workflow diagram itself.

To set the breakpoint in code, go to the Solution Explorer, right-click ValidateCustomerActivity, and select View Code. In the Execute() method, set a breakpoint on the code that calls Customer.CanCustomerOrder() (see Figure 17). Next, go to the Solution Explorer again and double-click ProcessOrderWorkflow. Double-click the workflow diagram to open it in the designer, and then right-click the diagram and select Breakpoint → Insert Breakpoint from the shortcut menu to add a visual breakpoint indicator to the diagram as shown in Figure 18.

Figure 17. Setting Workflow Breakpoints: You can set a breakpoint on the workflow diagram itself, which displays a breakpoint icon in the upper left corner of the diagram.
Figure 18. Debugging Workflows: Visual Studio supports visual debugging for workflows. As you step through the workflow the currently executing activity is highlighted in yellow.
Figure 19. Propagation Successful: Activity data binding takes care of automatically populating the activity's CustomerID dependency property from the workflow's CustomerID dependency property.
Press F5 to run the workflow. You will hit the first breakpoint set earlier in the article. Press F5 again to continue. You should hit the diagram breakpoint, which displays a visual debugging cue—a yellow rectangle around the entire diagram—a very nice feature. Press F10 to continue and a yellow highlight bar appears around validateCustomerActivity as shown in Figure 18. Press F10 one more time to hit the next breakpoint. If you hover your mouse pointer over the reference to the CustomerID property, you will see it is set to 1001 as shown in Figure 19. Activity data binding worked! When the workflow stored this value in its CustomerID property, it automatically propagated the value to the activity's CustomerID property. You can press Shift+F5 to stop the workflow.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date