PDFx – Property Dependency Framework – Part V, Simple Property Dependencies

The PDFx is a lightweight open source .NET library that allows developers to describe dependencies between Properties in declarative C# code. Once relationships are registered, the framework monitors property changes and ensures that the INotifyPropertyChanged.PropertyChanged event is fired for all directly and indirectly dependent properties in a very efficient way. The library is available for WPF, Silverlight, WinRT (Windows Store) and Windows Phone.

I’ve developed the PDFx as an InterKnowlogy RECESS project and published the source code and examples on codeplex.

In a series of blog posts I am going to cover the library’s most important features:

Simple Property Dependencies

After you’ve followed the instructions from Getting Started, you are ready to use the PDFx.

Declarative Usage

The declarative PDFx API allows you to register a property dependency from within a property getter’s implementation following this pattern:

class MyClass : BindableExt
{

    AnyType MyTargetProperty
    {
       get 
       {
             Property(() => MyTargetProperty)
                 .Depends(p => p.On(() => FirstSourceProperty)
                                  .AndOn(() => SecondSourceProperty)
                                  .AndOn(() => LastSourceProperty));

            //Do something with the values of those 3 Properties and return a result     
        }       
    }

    AnyType FirstSourceProperty { get { } set { } }
    AnyType SecondSourceProperty { get { } set { } }
    AnyType LastSourceProperty { get { } set { } }

}

The call to method Property expects a lambda expression which points to the Property in which your code resides. The PDFx will use this expression to resolve the property’s name.
The returned object of method Property allows you to call the method Depends which expects a delegate that accepts a parameter of the fluent interface type IPropertyDependencyExt (or IPropertyDependency in the case of the PCL’s Bindable class).
This parameter allows you to specify source properties your current target property depends on by calling the On and AndOn methods. Both methods always return the very same instance of the interface type IPropertyDependencyExt, thereby allowing you to register the entire property registration in a single execution.

Example

The source code of this example can be found in ViewModel SimplePropertyDependencyVM which is part of the WPFSample’s source code.

Let’s assume we are required to implement the following graph:

SimplePropertyDependenciesPicture.png
Green circles stand for Input Properties while purple circles indicate calculated properties. The arrows show the underlying math operations as well as the property dependencies.

Using the PDFx’s Simple Property Dependencies feature, we can easily represent the depicted graph in a ViewModel similar to this:

class SimplePropertyDependencyVM : BindableExt
{
	public int A1
	{
		get
		{
			Property(() => A1)
				.Depends(p => p.On(() => B1)
						.AndOn(() => B2));

			return B1 + B2;
		}
	}

	public int B1
	{
		get
		{
			Property(() => B1)
				.Depends(p => p.On(() => C1)
						.AndOn(() => C2));

			return 2 * C1 - C2;
		}
	}

	public int B2
	{
		get
		{
			Property(() => B2)
				.Depends(p => p.On(() => C2)
						.AndOn(() => C3));

			return -C2 + C3;
		}
	}

	public int C1
	{
		get
		{
			Property(() => C1)
				.Depends(p => p.On(() => D1)
						.AndOn(() => D2));

			return D1 + D2;
		}
	}

	public int C2
	{
		get
		{
			Property(() => C2)
				.Depends(p => p.On(() => D3));

			return 3 * D3;
		}
	}

	public int C3
	{
		get
		{
			Property(() => C3)
				.Depends(p => p.On(() => D4)
						.AndOn(() => D5));

			return D4 + D5;
		}
	}

	private int _d1;
	public int D1
	{
		get { return _d1; }
		set
		{
			_d1 = value; 
			NotifyPropertyChanged(() => D1);
		}
	}

	private int _d2;
	public int D2
	{
		get { return _d2; }
		set
		{
			_d2 = value;
			NotifyPropertyChanged(() => D2);
		}
	}

	private int _d3;
	public int D3
	{
		get { return _d3; }
		set
		{
			_d3 = value;
			NotifyPropertyChanged(() => D3);
		}
	}

	private int _d4;
	public int D4
	{
		get { return _d4; }
		set
		{
			_d4 = value;
			NotifyPropertyChanged(() => D4);
		}
	}

	private int _d5;
	public int D5
	{
		get { return _d5; }
		set
		{
			_d5 = value;
			NotifyPropertyChanged(() => D5);
		}
	}
}

One thought on “PDFx – Property Dependency Framework – Part V, Simple Property Dependencies

  1. Pingback: PDFx – Property Dependency Framework – Part I, Introduction | //InterKnowlogy/ Blogs

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>