![Programming Windows Workflow Foundation: Practical WF Techniques and Examples using XAML and C#](https://wfqqreader-1252317822.image.myqcloud.com/cover/46/34853046/b_34853046.jpg)
Pure Code
Building a workflow with a pure code approach means we are only using C# or Visual Basic code to define the workflow. There is no XAML involved. This doesn't mean we have to write all the code ourselves. Many designers in Visual Studio, like the Windows forms designer, have been generating C# and Visual Basic code for years. The workflow designer has the ability to generate code for us. We will want to combine the designer-generated code with our own code to build a workflow.
Pure Code and Visual Studio
The Visual Studio 2005 Extensions for Windows Workflow package will add project and item templates into Visual Studio. These item templates provide a starting point for building a workflow project, a workflow, or an activity.
To use an item template we merely need to right-click a project and select Add | New Item. One of the item templates appearing in the Add New Item dialog box sets up the files and code needed to support a pure code approach. This item template is the Sequential Workflow (code) template. Other item templates appear in the screenshot below:
![Pure Code and Visual Studio](https://epubservercos.yuewen.com/F214F9/18607241408219806/epubprivate/OEBPS/Images/1213_02_01.jpg?sign=1739396291-XtCfY8cDyGEIyV7iNpT77KwrGIeFRNfF-0-fc79e243c62445819274bd1c25dd47ad)
When we add a workflow to our project using the Sequential Workflow (code) template, we don't add just a single source code file. If we create a new workflow from the template, and give the workflow the name of PureCode
, the template will add two files to the project: a PureCode.cs
file and a PureCode.Designer.cs
file. PureCode.cs
is a file we can edit. The PureCode.Designer.cs
file contains code the graphical designer will edit.
With our PureCode
workflow in the designer window, we can drop a CodeActivity
from the Toolbox window into the designer. We can then use the Properties window to assign a method for the ExecuteCode
event. Select the CodeActivity
, and then click the Generate Handlers hyperlink in the Properties window to generate a default handler for ExecuteCode
.
![Pure Code and Visual Studio](https://epubservercos.yuewen.com/F214F9/18607241408219806/epubprivate/OEBPS/Images/1213_02_02.jpg?sign=1739396291-sWVu3QMQToJhXKGjUPR2LaSKx0f1gT0K-0-1aae9d5f4827dc97f8d48955ac3caca6)
All the steps listed above will produce the following code inside of PureCode.cs
.
using System; using System.Workflow.Activities; namespace chapter2_library { Windows Workflowpure code approachpublic sealed partial class PureCode: SequentialWorkflowActivity { public PureCode() { InitializeComponent(); } private void codeActivity1_ExecuteCode(object sender, EventArgs e) { } } }
PureCode.cs is our file to edit. The codeActivity1_ExecuteCode
method is here and waiting for us to provide an implementation. Notice the constructor calls a method by the name of InitializeComponent
, but this method is not present in the PureCode.cs
file. As it turns out, the InitializeComponent
method is a member of the PureCode
class, but we will find its definition in another file. This is the magic provided by the partial
keyword modifier on our class. The partial
keyword allows us to split a class definition across multiple source code files.
The rest of the class definition for our PureCode
workflow lives in PureCode.Designer.cs. We typically would never need to look at or edit this class, because the workflow designer is responsible for generating code inside the file. Here is what the designer generates for this workflow.
using System; using System.Workflow.Activities; namespace chapter2_library { public sealed partial class PureCode { #region Designer generated code private void InitializeComponent() { this.CanModifyActivities = true; Windows Workflowpure code approachthis.codeActivity1 = new System.Workflow.Activities.CodeActivity(); // // codeActivity1 // this.codeActivity1.Name = "codeActivity1"; this.codeActivity1.ExecuteCode += new System.EventHandler( this.codeActivity1_ExecuteCode); // // PureCode // this.Activities.Add(this.codeActivity1); this.Name = "PureCode"; this.CanModifyActivities = false; } #endregion private CodeActivity codeActivity1; } }
The InitializeComponent
method appears in this half of the partial class definition and contains the code to set up the activities in our workflow. In this case, we have only a single Code
activity in our workflow. The code constructs the activity and adds the activity as a child of the workflow. The class also defines private fields for each activity in our workflow — in this case only codeActivity1
. We can control the names of the activity fields by setting the Name
property of an activity in the Property window.
When we build our project, the C# compiler will merge the two partial class definitions into a single type. The new type will have the name of PureCode
and contain both the InitializeComponent
and codeActivity1_ExecuteCode
methods as shown in the figure below:
![Pure Code and Visual Studio](https://epubservercos.yuewen.com/F214F9/18607241408219806/epubprivate/OEBPS/Images/1213_02_03.jpg?sign=1739396291-KfvqXGecmSgHk8S7bymR9WZa3BBYI2YU-0-00741b4a029c5d880a3ecad8b83b9f44)
With the pure code approach of Visual Studio, each workflow is the combination of a designer-managed code file, and a developer-managed code file. This is a perfectly reasonable approach if we construct all of our workflows using the designer. Of course, it would also be possible to write all that code by hand, or with a different tool. The ultimate goal is to create a hierarchical tree of activities inside our workflow. Some of the alternative approaches to defining workflows are more amiable to outside tools.