hotrannam's corner

I enjoy building apps that people love

ASP.NET MVC 3: Inversion of control

with one comment

This is the first post in the series that I am going to warm up my knowledge in ASP.NET MVC. I will pick the latest version, ASP.NET MVC 3 RTM. It is good for me to recall the old things and know the new things as well.

In the new features, I first try IoC improvements. Controller factory was extensibility point that I liked. This is where I could register external services that controllers need. It’s not only to resolve dependencies for controllers, more than that, thinking of chain of dependency. In ASP.NET MVC 3, controller factory is disappeared and the interface IDependencyResolver is introduced.

public class ServiceResolver : IDependencyResolver
{
    private IUnityContainer container;

    public ServiceResolver()
    {
        this.container = new UnityContainer();
        this.RegisterServices();
    }

    private void RegisterServices()
    {
        this.container.RegisterType<IFooService, FooService>();
    }

    public object GetService(Type serviceType)
    {
        try
        {
            return this.container.Resolve(serviceType);
        }
        catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return this.container.ResolveAll(serviceType);
        }
        catch
        {
            return new List<object>();
        }
    }
}

Once I have an instance that is implementation of this interface, I could set it as default resolver to inject dependencies into controllers.

protected void Application_Start()
{
    //...
    DependencyResolver.SetResolver(new ServiceResolver());
}

There is also an overload of static method DependencyResolver.SetResolver with parameter is Common Service Locator. It sounds cool, but I am not sure will use it.

It is trick to must have try/catch block in GetService method. Without this, ASP.NET MVC 3 will throw an error related to IControllerFactory. Oops, it is not disappeared totally.

Advertisements

Written by Nam

January 16, 2011 at 8:05 pm

Posted in ASP.NET MVC

Tagged with

One Response

Subscribe to comments with RSS.

  1. It’s good for ASP.NET MVC 3. I am really not like Service Locator integrated in MVC 3 Preview 1. Now it is better. Brad also have a good series of articles about IoC in ASP.NET MVC 3 at http://bradwilson.typepad.com/blog/2010/07/service-location-pt1-introduction.html

    thangchung

    January 17, 2011 at 4:50 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: