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


Windows Workflow Foundation Essentials : Page 5

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

Enhancing Custom Activities (continued)
Now you can add code to the activity that sets the CanCustomerOrder dependency property to the correct value. As mentioned previously, the best approach is to call the services of a business object from the activity. The source code for this article contains a Customer business object that has a CanCustomerOrder() method you can use to determine if a customer can order products. First, you need to go back to the top of the activity's source code, and add a reference to the business object project's namespace to the activity.

In C#:

   using OakLeaf.OrderSystem.Business;
And in Visual Basic:

   Imports OakLeaf.OrderSystem.Business
You can add a third dependency property to the activity that can hold a reference to the Customer business object. This time use the wdp code snippet to create a dependency property named CustomerProperty, of type Customer, a description of "Customer Business Object," and the Custom Property category.

Your property should look like this in C#:

   public static DependencyProperty 
      CustomerProperty = System.Workflow.
      Register("Customer", typeof(Customer), 
   [Description("Customer Business Object")]
   [Category("Custom Property")]
   public Customer Customer
         return ((Customer)(base.GetValue(
            CustomerProperty, value);
And in Visual Basic:

   Public Shared CustomerProperty As _
      DependencyProperty = _
      DependencyProperty.Register("Customer", _
      GetType(Customer), GetType(Customer))
   <Description("Customer business object")> _
   <Category("Custom Property")> _
   <Browsable(True)> _
   <DesignerSerializationVisibility( _
      DesignerSerializationVisibility.Visible)> _
   Public Property Customer() As Customer
         Return (CType((MyBase.GetValue(Customer.
            CustomerProperty)), Customer))
      End Get
      Set(ByVal Value As Customer)
         MyBase.SetValue(Customer. _
            CustomerProperty, Value)
      End Set
   End Property
Now you're ready to add code to the activity that does the real work of validating a customer. All workflow activities inherit an Execute() method that the workflow automatically calls when it executes the activity. You override this method to add custom code that accomplishes the task required. Here is the code you can add to the ValidateCustomerActivity.

In C#:

   protected override ActivityExecutionStatus 
   Execute(ActivityExecutionContext executionContext)
      if (this.Customer == null)
         this.Customer = new Customer();
      this.CanCustomerOrder = 
      return base.Execute(executionContext);
And in Visual Basic:

   Protected Overrides Function Execute( _
      ByVal executionContext As _
      System.Workflow.ComponentModel. _
      ActivityExecutionContext) As _
      System.Workflow.ComponentModel. _
      If Me.Customer Is Nothing Then
         Me.Customer = New Customer()
      End If
      Me.CanCustomerOrder = 
      Return MyBase.Execute(executionContext)
   End Function
This code first checks to see if the Customer property already contains a reference to a Customer business object. In certain contexts the workflow may set the Customer property. In other contexts, if the Customer property isn't automatically set, the activity instantiates a new instance. The next line of code calls the Customer business object's CanCustomerOrder() method passing the CustomerID property and storing the result into the activity's CanCustomerOrder property. You'll see in just a bit how the workflow sets the CustomerID property. As an aside, if you look at the CanCustomerOrder() method, you'll see it simply returns a true rather than doing any real checking.

The last line of code in the method calls the base class Execute() method. This method returns an ActivityExecutionStatus value. By default, this base class method returns a value of ActivityExecutionStatus.Closed indicating the activity has finished its work—which is true of this custom activity.

Adding a Custom Activity to the Workflow Diagram
Now you're ready to add the ValidateCustomerActivity to the workflow. First, rebuild the Visual Studio project. Next, go to the Solution Explorer and double-click ProcessOrderWorkflow. In the workflow diagram, right-click codeActivity1, and select Delete from the shortcut menu, because you no longer need this activity. Next, go to the Visual Studio Toolbox, and under the SequentialWorkflowConsole Components tab you should see the ValidateCustomerActivity. Drag the activity from the toolbox, and drop it on the workflow diagram where it says "Drop Activities to create a Sequential Workflow."

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