This post describes the validations in my SL4 demo application using MVVM pattern in more detail.
The prerequisites:
- View talks (binds, calls) to ViewModel
- ViewModel exposes Model (any object graph) using a property (in Caliburn there is a property called Subject) => View can bind to the Model + any his property
- The effort for developing Model and all the validation aspects should be minimalized
- We need the validation ;o)
I differentiate the following validations:
- UI input fields validation – e.g. a validation of an input in a textbox
- A model validation – this validates the viewmodel and model in context of an action, e.g. validate Person in context of “UnregisterUser” task.
There are 2 tasks related with the validation:
- validate the object
- inform the user about the validation failures
Silverlight 4 supports these tasks via two interfaces: IDataErrorInfo, INotifyDataErrorInfo. INotifyDataErrorInfo is used for the asynchronous validation purposes but this can be achieved also by IDataErrorInfo together with INotificatonPropertyChanged (which is my case).
The solution
Task 1 – enable UI validation infrastructure
In order to enable the UI validation on both Model and ViewModel it’s necessary them to implement IDataErrorInfo. Just to remind, IDataErrorInfo interface looks like:
How is this interface used:
Let’s have a view which is bound to the property of the model (The model is exposed via property Subject on the viewmodel). When the validation should be evaluated, SL casts the object whose property should be validated (in this case a model) to IDataErrorInfo and asks its indexer this[property] for the errors.
The model has no glue of any its validation rules. It’s not his concern and it doesn’t know the case in which it’s used. So in order to get the validation errors the model forwards this task to it’s validation root – which is the viewmodel – and pass him the path from the validation root to that property. The same applies for the viewmodel itself . All of these forwarding, etc. is the boilepart infrastructure code which is necessary to write for each viewmodels and models. .
In order to avoid coding the same boilepart infrastructure code, I created two base classes: ValidatableModelBase and ValidatableViewModelBase.
Task 2 – create the validators
I used FluentValidator as the base validation framework and I separated that validation concern to the validator classes. So there are [Person]ViewModelValidator, [Person]Validator. For more info about FluentValidation check this
ViewModel is the master/director/manager in the MVVM pattern. His responsibility is drive the view. So the ViewModelValidator is the master of the validation for the view model. Because the validation and notifying the UI are very coupled tasks/concerns, I created a base class for the viewmodel validation and notification purposes and it’s called ViewModelValidatorBase.
Just to make it clear:
ModelValidator: validates the model. Important: they can be shared between client and server!
ViewModelValidator<TViewModel>:
extends ModelValidator concerns with UI fields validations and failure notifications.
Its concerns:
- defines which properties will be enabled for the standard UI validation

- validates and notifies UI in case of any validation failures

So then the concrete validatos can look like following:
So after satisfying the infrastructure needs and defining the validation logic + enabling what UI validations will be supported by the ViewModel using respective [xxx]ViewModelValidator we start to use it.
Using constructor injection the view model gets the dependency to its validator.
When the model/subject is set then the UI validation definition is used and the model is enabled for the UI input validations.
This will set two properties:
- the view model as the validation root
- path how it’s possible to navigate to that property (e.g. Subject.HomeAddress) from the validation root – viewmodel
So after that the bindings to properties (e.g. Street, Zip, etc.) on the property Subject.HomeAddress are able to get the validation errors.
Validation in the action
An action in the view model can ask viewmodel’s validator to validate the viewmodel instance in context of the action and then process the validation result.
Processing includes notifying the view about the validation failures and using this result in the action’s business logic.
This solutions gives me the following goals:
- I can share/reuse the validation rules for the models between client and server
- The standard validation mechanism in SL4 is supported (unfortunately, binding and validation in SL is completely different than in WPF and unfortunately written with Close/Close principal – so there are no ways to extend in any custom way in SL)
- I can use full templating and property paths to the data I want to bind to the views
- Client models are not so much ‘dirty’ from the infrastructure code, it’s necessary only to derive from the base class.


























