- Part 1: Introduction
- Part 2: Controllers
- Part 3: View Engines and View Pages
- Part 4: Filters
- Part 5: IDependencyResolver
- Part 6: Model Validation
- Part 7: Model Metadata
- Part 8: Value Providers
- Part 9: Model Binders
- Part 10: Controller Activator
- Part 11: View Page Activator
ASP.NET MVC 2 introduced an extensible model validation system where developers could implement a class which derived from ModelValidatorProvider to influence the validation process on both the client-side and server-side. In ASP.NET MVC 3, we have made these validator providers findable via the dependency resolver.
This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships, so please comment on this blog post or contact me if you have comments.
Writing a Validator Provider
For an example implementation of ModelValidatorProvider, see my blog post: Enterprise Library Validation example for ASP.NET MVC 2.
This is a “multiply registered” style service introduced in MVC 2. The static registration point for this service is at ModelValidatorProviders.Providers for non-DI users.
The logic in ModelValidatorProviderCollection (which implements ModelValidatorProviders.Providers) was updated to consult the dependency resolver, calling GetServices(typeof(ModelValidatorProvider)) and adding any services found to the list of statically registered service. Validator providers all collaborate to potentially validate a model value, so the ordering of validator providers is not important. Every provider will get an opportunity to provide validators.
Next up we’ll talk about Model Metadata.