15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1322 ❘ ChaPTer 44 windOws wOrkflOw fOundAtiOn 4<br />

This control is bound to the Body property of the Retry activity, <strong>and</strong> the HintText defines the help text<br />

shown when no child activity has been added to the control. The XAML also includes some styles that are<br />

used to show the exp<strong>and</strong>ed or contracted version of the designer — this ensures that the activity works<br />

the same way as the built-in activities. All of the code <strong>and</strong> XAML for this example is available in the<br />

04_CustomActivities solution.<br />

WorKfloWs<br />

Up to this point, the chapter has concentrated on activities but has not discussed workflows. A workflow<br />

is simply a list of activities, <strong>and</strong> indeed a workflow itself is just another type of activity. Using this model<br />

simplifies the runtime engine, because the engine just needs to know how to execute one type of object — that<br />

being anything derived from the Activity class.<br />

You’ve already seen the WorkflowExecutor class, which can be used to execute a workflow, but as<br />

mentioned at the time, this is only one of the ways that a workflow can be executed. There are three<br />

different options for executing workflows, <strong>and</strong> each has different capabilities. Before getting into the other<br />

methods of executing workflows, we must delve into arguments <strong>and</strong> variables.<br />

arguments <strong>and</strong> Variables<br />

A workflow can be considered a program, <strong>and</strong> one of the facets of any programming language is the ability<br />

to create variables, <strong>and</strong> pass arguments into <strong>and</strong> out of that program. Naturally, Workflow 4 supports both<br />

constructs, <strong>and</strong> in this section we show you how you can define both arguments <strong>and</strong> variables.<br />

To begin with, let’s assume that the workflow is processing an insurance policy, so a likely argument to<br />

pass to the workflow would be the policy ID. In order to define an argument to a workflow, you need to go<br />

into the Designer <strong>and</strong> click on the Arguments button on the bottom left. This brings up a list of arguments<br />

defined for the workflow, as shown in Figure 44-12, <strong>and</strong> here you can also add your own.<br />

figure 44-12<br />

To define an argument, you need to specify the Name, Direction (which can be In, Out, or InOut) <strong>and</strong> the<br />

data type for that argument. You can optionally specify a default value, which will be used if the argument<br />

is not provided.<br />

The direction of the argument is used to define whether the argument is expected as an input to the workflow,<br />

as an output from the workflow, or in the case of the InOut direction, as both an input <strong>and</strong> output.<br />

In the first section of this chapter, we described using the WorkflowInvoker class to execute a workflow. There<br />

are several overrides of the Invoke method that can be used to pass arguments into the workflow. They are<br />

passed as a dictionary of name/value pairs, where the name must match the argument’s name exactly — this<br />

match is case sensitive. The code below is used to pass a PolicyId value into a workflow — the code for this<br />

example is available in the 05_ArgsAndVars solution.<br />

Dictionary parms = new Dictionary();<br />

parms.Add("PolicyId", 123);<br />

WorkflowInvoker.Invoke(new PolicyFlow(), parms);<br />

code download 05_ArgsAndVars<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!