Home > .NET, technology, workflow, workflow rules > Visual Studio 2008 (Orcas) Feature: Instantiating Objects in WF Rules

Visual Studio 2008 (Orcas) Feature: Instantiating Objects in WF Rules

One of the new WF-related features in Visual Studio 2008 (Orcas) is the ability to new-up objects from within a rule. Previously, this seemingly simple task of creating a new object wasn’t possible within a rule. Now it is.

To illustrate this new feature, consider the following contrived example. Assume that you have a workflow that must return an instance of the following class:

using System;

namespace NewUpRules
{
    public class MyClass
    {
        public MyClass(String dayOfWeekDesc)
        {
            _dayOfWeekDesc = dayOfWeekDesc;
        }

        private String _dayOfWeekDesc;
        public String DayOfWeekDesc
        {
            get { return _dayOfWeekDesc; }
            set { _dayOfWeekDesc = value; }
        }
    }
}

Previously, your workflow could use a PolicyActivity and implement a few rules, but you would have to create an instance of this class in code instead of in the rule. However, now you can implement a rule that directly creates an instance of the object like this:

Condition:
System.DateTime.Now.DayOfWeek == System.DayOfWeek.Monday
Then Actions:
this.MyWFResult = new NewUpRules.MyClass("Starting a new work week")
Else Actions:
this.MyWFResult = new NewUpRules.MyClass("Some other day")

The MyWFResult variable is assumed to be a workflow variable that is used to return the resulting instance of MyClass to the host application.

But you’re not limited to creating objects in the rule actions. You can also new-up objects in the Condition portion of the rule. Consider this class:

using System;

namespace NewUpRules
{
    public class MyDayOfWeekChecker
    {
        private DateTime _dateToCheck;
        public MyDayOfWeekChecker(DateTime value)
        {
            _dateToCheck = value;
        }

        public Boolean IsStartOfWorkWeek
        {
            get
            {
                return (_dateToCheck.DayOfWeek == DayOfWeek.Monday);
            }
        }
    }
}

A DateTime value is passed to the constructor of this class, and an IsStartOfWorkWeek property is defined which returns a Boolean. With the VS 2008 enhancements, you can now create an instance of this class and use it in a rule condition like this:

Condition:
new NewUpRules.MyDayOfWeekChecker(System.DateTime.Now).IsStartOfWorkWeek
Then Actions:
this.MyWFResult = new NewUpRules.MyClass("Starting a new work week")
Else Actions:
this.MyWFResult = new NewUpRules.MyClass("Some other day")

Granted, this isn’t the most exciting new feature in Orcas, but I do think it is a welcomed change that improves the rules capabilities in WF. Note: All of this code works in Beta 2 of Orcas.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: