I think a lot about how to reuse code when building for both Windows 8 and Windows Phone. Recently I started thinking about how to implement validation logic in a good way. Validation is a big topic, if one takes the time to delve into it. However, it is not easy to find a good validation example that is implemented in MVVM (Model-View-ViewModel) with PCL (Portable Class Library).

Some of the questions I want to address are:

  1. Where to trigger the validation?
  2. How to display the validation errors?
  3. How are the validation rules defined?

I would like to give some preconditions for my investigation. I want to be able to reuse most of the code and I want my code to be easy to understand.

Support for Windows 8 and Windows Phone

When it comes to validation functions in NET, there are already a lot of platform support. The problem is that different platforms support different validation functions. With PCL I have to find something that is supported by all the platforms that I want to write for.

Here is a rundown of which validation features that the various platforms supports.

  1. Is only supported if Windows Phone is not included in the target frameworks list.

From this table we can see that there is only one feature that supports both platforms, INotifyDataErrorInfo. The documentation says this about this interface:

This interface enables data entity classes to implement custom validation rules and expose validation results asynchronously. This interface also supports custom error objects, multiple errors per property, cross-property errors, and entity-level errors. Cross-property errors are errors that affect multiple properties. You can associate these errors with one or all of the affected properties, or you can treat them as entity-level errors. Entity-level errors are errors that either affect multiple properties or affect the entire entity without affecting a particular property.

In the next part of this series will I show how to implement the INotifyDataErrorInfo in MVVM with PCL.