PDFx – Property Dependency Framework – Part VI, External 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:

External Property Dependencies

Declarative Usage

The declarative PDFx API allows you to register a dependency on a property that resides in any INotifyPropertyChanged-instance different from the owner of the dependent Property:

class SourceClass : INotifyPropertyChanged
{
	private AnyType _sourceProperty;
	public AnyType SourceProperty
	{
		get { return _sourceProperty; }
		set
		{
			_sourceProperty = value;
			OnPropertyChanged("SourceProperty");
		}
	}
}

class DestinationClass : BindableExt
{
	SourceClass Source { get; set; }

	public AnyOtherType TargetProperty
	{
		get
		{
			Property(() => TargetProperty)
				.Depends(p => p.On(Source, k => k.SourceProperty));

			//Source.SourceProperty has changed. Do Something with it and return.
		}
	}
}

Overloads of the methods On and AndOn both allow you to pass in an INotifyPropertyChanged instance as the first parameter and point to any property of that instance in the Lambda Expression you pass in as the second parameter.

Example

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

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

ExternalPropertyDependencies.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, we can easily represent the depicted graph in C#:

public class DestinationVM : BindableExt
{
	public DestinationVM()
	{
		ExternalSource1 = new ExternalSource1();
		ExternalSource2 = new ExternalSource2();
	}

	public ExternalSource1 ExternalSource1 { get; private set; }
	public ExternalSource2 ExternalSource2 { get; private set; }

	public int A1
	{
		get
		{
			Property(() => A1)
				.Depends(p => p.On(() => B1)
						.AndOn(ExternalSource1, k => k.A1)
						.AndOn(ExternalSource2, k => k.A1));

			return B1 + ExternalSource1.A1 - ExternalSource2.A1;
		}
	}

	private int _b1;
	public int B1
	{
		get { return _b1; }
		set { _b1 = value; NotifyPropertyChanged(() => B1); }
	}
}

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

			return 3 * B1;
		}
	}

	private int _b1;
	public int B1
	{
		get { return _b1; }
		set { _b1 = value; NotifyPropertyChanged(() => B1); }
	}
}

public class ExternalSource2 : BindableExt
{
	private int _a1;
	public int A1
	{
		get { return _a1; }
		set { _a1 = value; NotifyPropertyChanged(() => A1); }
	}
}

One thought on “PDFx – Property Dependency Framework – Part VI, External Property Dependencies

  1. Pingback: First Thoughts: PDFx | //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>