Posts Tagged ‘Dependency’

Monday, February 4th, 2008

Workflow Insights: Dependency

Dependency is one of those magic paradigms in Workflow Foundation which gives the use of the framework so much more richness, especially when using activities to break up functionality.
Dependency lets properties depend on other properties, and this in a dynamic way. In practise, if property object1.Property1 is dependent of object2.Property2 when you assign or get the Property1 of object1, you are actually modifying and retrieving Property2 of object2. The great advantage of this method and way of thinking is the avoidance of duplication of data. Also you enable activities to connect to each other.

So how does it work? There are actually two steps. The first one is the setup of the activity and its properties. The second one is the actual binding between two properties.

STEP 1 : Setup of the activity
Dependency at its core consists of two main classes: DependencyObject and DependencyProperty. The DependencyObject is the object that needs the data. It will do this by exposing properties which use DependencyProperty objects. One thing to remember is that when using dependency, the source object (the object which has the actual data) will never know that another object of objects is depending on it.
As dependency is one of the core paradigms of Workflow Foundation, it won’t come you as a shock to know that the base Activity class inherits from the DependencyObject class. The DependencyObject class exposes a number of methods to work with DependencyProperty’s. DependencyProperty’s are static values defined in the DependencyObject implementations. Those implementations (eg. Activities or Object1) are using these static values in Properties like this:

public class Object1
{
public static DependencyProperty Property1Property =
DependencyProperty.Register("Property1", typeof(string), typeof(Object1));

public string Property1
{
get { return (string)base.GetValue(Property1Property); }
set { base.SetValue(Property1Property, value); }
}
}

It is doing this as Properties should enhold actual data or link to other properties. This is why Workflow Foundation needed this alternative method with static values and internal methods. DependencyProperty’s define when a binding is chosen or when actual data is set. Actual data will be saved in an internal dictionary.

STEP 2 : Binding of properties
So how can we use these properties? There are two methods and both of them can be achieved in the visual designer as well as in code. (As you all know the visual designer is just creating code in the code behind designer class)
The first method is to use actual data. You can do this by assigning data to the property and retrieving them from the property, just like any other property. Nothing really special, actually.
The second and most interesting method is working with bindings. A new class is used to define them: the ActivityBind.

ActivityBind activityBind = new ActivityBind();
activityBind.Name = "object2";
activityBind.Path = "Property2";
this.object1.SetBinding(Object1.Property1, activitybind1);

As you see, the ActivityBind object creates a link to the object2′s Property2 property. As we are using this reference, the object2 does not actually know it is relied on by another object. Pretty simple.

In the end, it’s actually enough to remember just this one thing: if property object1.Property1 is dependent of object2.Property2 when you assign or get the Property1 of object1, you are actually modifying and retrieving Property2 of object2. Just keep to it, and you’ll be just fine. To end, here’s a quick overview. See ya.

object1..................object2
= DependencyObject.......= Plain object
= Target.................= Source
- Property1..............- Property2
...- get...=============>...- get
...- set...=============>...- set