Note: This expands on the code created in my previous post: WPF MVVM Multi-Solution Composite App
* Download Complete Source Code
Continuing on my WPF MVVM Adventures... My next task was to architect and code a validation framework that would:
- Adhere to the DRY principle.
- Allow me to define all validation in my domain objects.
- Allow me to easily relay validation issues to the UI/users.
- Allow me to easily add/modify validation rules.
- Not be *too* complex :)
Not wanting to re-invent the wheel I did some Googling, on Bing of course. The first post that caught my eye was by Karl Shifflett, whom to me is one of the Grandfathers of WPF, and despite being a VB guy, usually has the answer. So I read Karl’s article, downloaded the code and did some investigating. The code would get the job done, but it wasn’t DRY. Basically every field that was required had a check for not null and not empty, all validation code was done in the IDataErrorInfo’s Item override, and even Karl himself recommends not doing it this way, and that you should use Ocean or another validation framework. Not to discredit Karl, and I certainly recommend reading not only this post but all of Karl’s posts, but this didn’t fill all of my requirements. Also, I am the type of developer who won’t use a framework for something I can code myself with out too much effort.
The next post that looked promising was by Josh Smith, whose Advanced MVVM book is a must read for anyone using MVVM. Josh’s approach is nearly identical to Karl’s in that the base of the validation logic is repeated for every field. Josh’s example also allows for further validation to be done at the ViewModel level, and this confuses me a little, in that the domain object itself should be the *gatekeeper* of valid object state.
So, having read these two posts from two of the more prominent WPF developers, and not finding what I wanted worried me. Maybe I am totally missing something or what I want is flawed. But hey, that’s why I blog…if I’m wrong call me out…and tell me why.
I started throwing some designs and thoughts down on paper and kept coming back to using attributes on the properties of domain objects. Now, I am by no means a developer that pushes or even prefers attribute based solutions, but in this case it seemed to fit.
I knew that I was going to need an Interface or Abstract Base Class that would allow me to generically process the validators and always know what I was dealing with. In this case I decided to go with an abstract base class just so I could have it inherit from Attribute and only require my instances of Validator to inherit a single class. As you can see below the base class is very simple, it inherits Attribute and declares an abstract Validate method. The other thing you’ll notice is that I decided to decorate the base class with the Attribute configuration. This may change in the future, i.e. in some instances I may want to allow multiple of the same validator…say, if each is configurable.
1: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property,
2: AllowMultiple = false, Inherited = true)]
3: public abstract class ValidatorBase : Attribute {
4: public abstract string Validate(object value);
5: }
Now I need a validator. Let’s start simple with a standard required field validator. I created my class, made it inherit from ValidatorBase and coded the Validate override.
1: public class ValidRequiredField : ValidatorBase {
2:
3: private const string _INVALID_REQUIRED_FIELD = "Required Field";
4:
5: public override string Validate(object value) {
6: try {
7: if (value == null || string.IsNullOrEmpty(value.ToString()))
8: return _INVALID_REQUIRED_FIELD;
9: }
10: catch (NullReferenceException) {
11: return _INVALID_REQUIRED_FIELD;
12: }
13: return null;
14: }
15: }
The next step is decorate the properties in the domain entity with any validation attributes. In this case I am decorating the LastName property with the ValidRequiredField attribute.
1: [ValidRequiredField]
2: public string LastName {
3: get { return _lastName; }
4: set {
5: if (_lastName != value) {
6: _lastName = value;
7: OnPropertyChanged("LastName");
8: }
9: }
10: }
OK. Cool. Unfortunately, this by itself does nothing. We have to kick off the validation and put in the processes that allow the ViewModel and UI to receive notification of validation issues. We will start this process by creating an abstract base class that our domain entities will inherit from. This abstract base class will implement INotifyPropertyChanged, and IDataErrorInfo. I am sure we are all familiar with INotifyPropertyChanged so I am not going to go into detail on what needs to be done to implement that interface.
IDataErrorInfo is what is going to allow us to fire off our validation and communicate any issues to the ViewModel and UI. First we are going to create a Validate function that will accept a string parameter that represents the property name. The function will verity that the passed in property name is valid and that a property with said name exists on the domain entity. If the property exists, its value is retrieved. The property is then interrogated for attributes of type ValidatorBase. If/When one is found the validation logic is performed on that property. A the validation of the property returns something other than null that means the property is invalid and a string containing details of the validation issue is returned. If no validation issue is found on the current attribute then the next attribute is checked and the process continues until we are out of attributes or a validation issue is found.
1: public string Validate(string propertyName) {
2: string result = null;
3: PropertyInfo prop = GetType().GetProperty(propertyName);
4: if (prop != null && !prop.Name.Equals("item", StringComparison.InvariantCultureIgnoreCase)) {
5: object value = prop.GetValue(this, null);
6: var attributes = (ValidatorBase[])prop.GetCustomAttributes(typeof(ValidatorBase), false);
7: foreach (ValidatorBase attribute in attributes) {
8: result = attribute.Validate(value);
9: if (result != null)
10: return result;
11: }
12: return result;
13: }
14: return result;
15: }
16: }
Next we will implement the contract for IDataErrorInfo. This contract requires us to implement two properties, even know WPF only uses one of them. The Item property is an enumerator for the collection of strings representing errors. In the getter of this property we will return the results of calling our Validate function with the supplied property name. This will later be hooked up in a round about way to the View via the ViewModel.
1: public string this[string propertyName] {
2: get { return Validate(propertyName); }
3: }
4:
5: //Note: WPF does not use this function.
6: public string Error {
7: get { return null; }
8: }
This completes the plumbing for our validation framework. There are an abundant number of blogs post on how to consume the IDataErrorInfo details in your views. Beth Massi has done an excellent one which you can find here.
So, in wrapping up I feel like I have completed a validation framework that satisfies my goals. Hopefully you will find it adaptable to your situation.
* Download Complete Source Code