PDFx – Property Dependency Framework – Part XV, Two Way Converters

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:

Two Way Converters

Although the following paragraph doesn’t describe a new feature of the PDFx, it demonstrates a use case that solves a common problem developers face every day, apart from the sole monitoring of large Property Dependency networks.

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

Oftentimes developers use value converters to transform domain-specific data into other domain-specific data (e.g. convert a money value from one currency to another), and thereby scatter conversion business logic throughout the project by implementing it in various different converters (that technically belong to the UI layer). The reason for this typical behavior is simple: comfort. value converters get automatically reevaluated as soon as the input data changes and therefore they are an easy tool to display up-to-date transformed data in the UI.
From my point of view, however, value converters should be used mainly to convert domain-specific data to UI-specific data (e.g. convert a bool from the ViewModel to a Visibility for the View). All the massaged data, which eventually ends up in the View, should be exposed by the ViewModel. This way a new View could easily be plugged into the existing ViewModel and business logic is executed before it reaches the View layer.

The PDFx allows you to enjoy the comfort of value converters (automatic reevaluation) within your ViewModels.

As explained in Data Delegation, the PDFx can be used to expose Model-data in the ViewModel.
The implementation of the MoneyInUSD-Property shows that the data doesn’t have to be delegated as is but can easily be transformed on the fly:

class ViewModel : BindableExt
	private Model Model { get; set; }

	public double MoneyInUSD
			Property(() => MoneyInUSD)
				.Depends(p => p.On(Model, k => k.MoneyInEuros));

			return Model.MoneyInEuros * 1.3;
		set { Model.MoneyInEuros = value / 1.3; }

class Model : BindableExt
	private double _moneyInEuros = 100;
	public double MoneyInEuros
		get { return _moneyInEuros; }
			_moneyInEuros = value;
			NotifyPropertyChanged(() => MoneyInEuros);

In this scenario, the PDFx ensures that the MoneyInUSD Property gets reevaluated whenever the Model’s MoneyInEuros Property changes.

5 thoughts on “PDFx – Property Dependency Framework – Part XV, Two Way Converters

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

  2. Pingback: PDFx – Property Dependency Framework – Part II, Library Versions | //InterKnowlogy/ Blogs

  3. Pingback: PDFx – Property Dependency Framework – Part VIII, Collection Dependencies | //InterKnowlogy/ Blogs

  4. Pingback: PDFx – Property Dependency Framework – Part IX, Dynamic Collection Dependencies | //InterKnowlogy/ Blogs

  5. Pingback: PDFx – Property Dependency Framework – Part XVI, One Way Converters | //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>