-
Notifications
You must be signed in to change notification settings - Fork 17
Getting started with MvcExtensions
MvcExtensions is the library that gives you great extension points for your ASP.NET MVC application. The library provides an extremely powerfull tool for ModelMetadata regigistration via fluent interface. IoC container ignorance is key point of MvcExtensions library.
To get started with the MvcExtensions, first off, you need to pick up your favorite IoC container: MvcExtensions contains adapter for most popular IoC containers: Autofac, Windsor, Ninject, StructureMap, Unity.
You can install any required version from the nuget:
- MvcExtensions Autofac Adapter
- MvcExtensions Windsor Adapter
- MvcExtensions Ninject Adapter
- MvcExtensions StructureMap Adapter
- MvcExtensions Unity Adapter
The next step is to inherit your MvcApplication
from base class for the chosen container adapter, e.g., MvcExtensions.Windsor.WindsorMvcApplication
:
// Global.asax.cs
public class MvcApplication : WindsorMvcApplication
{
public MvcApplication() {}
}
To initialize your favorite IoC container you just need to implement interface or base class provided by your container:
Autofac.Module
, IWindsorInstaller
, Ninject.Modules.NinjectModule
, StructureMap.Configuration.DSL.Registry
or Microsoft.Practices.Unity.IModule
. See examples for Autofac or Windsor.
I recommend to place these files into any MVC application folder (e.g., /Infrastructure) Important note, you don't have to register controllers that way! MvcExtensions has another approach to do that. Generally speaking, you don't even need to use default integration of your IoC with ASP.NET MVC!
To run any startup code you have to put it into special class inherited from BootstrapperTask
. MvcExtensions has some built-in tasks and one of them is controllers registration. To enable controller registration in IoC container you need just to include the task RegisterControllers
into the list of bootstrapper tasks:
//Global.asax.cs
public class MvcApplication : WindsorMvcApplication {
public MvcApplication() {
Bootstrapper.BootstrapperTasks
.Include<RegisterControllers>();
}
}
Eventually all controllers in your MVC application will be activated via IoC container.
The next important step is to register your routes with custom BootstrapperTask. You just need to inhereit in from base class RegisterRoutesBase
(read more):
//Global.asax.cs
public class MvcApplication : WindsorMvcApplication {
public MvcApplication() {
Bootstrapper.BootstrapperTasks
.Include<RegisterControllers>()
.Include<RegisterRoutes>();
}
}
Read more about custom bootstrapper tasks
###Model Metadata
MvcExtensions has a great replacement for default metadata configuration via DataAnnotations. Now you able to do the configuration with fluent interface. This approach has extremely flexibility and provides unlimited extensibility point for you.
To use this feature you need just to include to Bootstrapper:
//Global.asax.cs
public class MvcApplication : WindsorMvcApplication {
public MvcApplication() {
Bootstrapper.BootstrapperTasks
.Include<RegisterControllers>()
.Include<RegisterRoutes>()
.Include<RegisterModelMetadata>();
}
}
And then:
public class ChangePasswordMetadata : MvcExtensions.ModelMetadataConfiguration<ChangePassword>
{
public ChangePasswordMetadata()
{
Configure(x => x.OldPassword)
.DisplayName("Old password")
.Required("Old password is required")
.AsPassword();
Configure(x => x.NewPassword)
.DisplayName("New password")
.Required("New password is required")
.MinimumLength(6, "Password length should be at least 6 characters")
.AsPassword();
Configure(x => x.ConfirmPassword)
.DisplayName("Password confirmation")
.Required("Password confirmation is required")
.Compare("NewPassword", "Password and its confirmation should be equal")
.AsPassword();
}
}
Read more about metadata configuration
MvcExtensions supports the same set of server/client side validation rules. In addition, MvcExtensions has an adapter for Foolproof validation provider (http://foolproof.codeplex.com/) which has a lot of useful methods for conditional validations.