Quantcast
Channel: Francis Shanahan[.com] » Technology
Viewing all articles
Browse latest Browse all 23

Using MVCExtension with Unity and Model Metadata Configuration

$
0
0
Using MVCExtension with Unity and Model Metadata Configuration

I did some toying with Unity and ASP.NET MVC Extensions and found the documentation and number examples somewhat sparse, particularly with MVCExtension. Here’s what I did to change my project over to use MVCExtensions with Unity.

Starting with a basic ASP.NET MVC2 application:

  1. Download and build MVCExtensions from the latest Trunk (http://mvcextensions.codeplex.com/). This is important as the MVCExtension project is under active development. If you download the last stable release you won’t get support for ModelMetadata.
  2. Add references to Microsoft.Practices.Unity, MvcExtensions and MvcExtensions.Unity.
  3. Changed the global.asax to inherit from UnityMvcApplication
  4. Cleaned out the global.asax and moved route configuration to a class inherited from RegisterRouteBase.

Stop there, compile and run. All should be fine. Now you can start taking advantage of Unity, here’s a silly example. Imagine a Person class that takes an Address as a dependency.


public Person(IAddress address)
{
// do something

}
<div>

Create a class implementing IModule and register your concrete class here.


public class UnityTypeRegistration : IModule

{

public void Load(IUnityContainer container)

{

container.RegisterType<IAddress, Address>();

}

}

Even more interesting you can take advantage of the ModelMetadataConfiguration which moves your configuration from Attributes on the model (untestable) to Fluent syntax in a class:

public class PersonDisplayModelConfiguration : ModelMetadataConfiguration<PersonDisplayModel>    {

public PersonDisplayModelConfiguration()        {

Configure(model => model.Lastname).DisplayName(() => MyResources.Lastname)

.Required(() => "Last name is required!!");

}

}

Really hope this MVCExtension project gains some traction as there are some nice ideas therein.


Viewing all articles
Browse latest Browse all 23

Trending Articles