Using Unity as a Dependency Resolver – ASP.Net MVC 3

**Update if you are using unity, you might want to check out my ideas on bootstrapping it in ASP.net MVC.**

ASP.Net MVC 3 has developed the concept of a Dependency Resolver to provide IOC/DI support. This is analogous to the common IServiceLocator interface exposed in the Common Service Locator assemblies from Patterns and Practices.

System.Web.Mvc.IDependencyResolver is a very simple interface to implement:

public interface IDependencyResolver{
  object GetService(Type serviceType);
  IEnumerable<object> GetServices(Type serviceType);
}

Below is a simple implementation of a IDependencyResolver that uses Unity under the hood:

public class UnityDependencyResolver : IDependencyResolver{
  private IUnityContainer container;

  public UnityDependencyResolver(IUnityContainer container){
    this.container = container;
  }

  public object GetService(Type serviceType){
    if (!container.IsRegistered(serviceType)){
      if (serviceType.IsAbstract || serviceType.IsInterface){
        return null;
      }
    }
    return container.Resolve(serviceType);
  }

  public IEnumerable<object> GetServices(Type serviceType){
    return container.ResolveAll(serviceType);
  }
}

Note how if the serviceType is not able to be instantiated and is not registered with the container, then a null object is returned in the case of GetService. This is important or an exception wil be thrown becuase Unity will try an instantiate the object.

Once an IDependencyResolver has been registered with the runtime, it will be requested to provide Controllers and Views. To set the dependency resolver for an application, call the static method in your Application_Start method in Global.asax as follows:

DependencyResolver.SetResolver(new UnityDependencyResolver(UnityInstance.Container));

Conclusion

This post has covered how to configure Unity as a Dependency Resolver for ASP.net MVC 3

kick it on DotNetKicks.com

About these ads
This entry was posted in ASP.Net MVC, Dependency Injection/Inversion of Control, Unity and tagged , , . Bookmark the permalink.

6 Responses to Using Unity as a Dependency Resolver – ASP.Net MVC 3

  1. Andy Hirst says:

    The logic in GetServiceType seems wrong, only returning null if the servicType isn’t registered in the container AND serviceType is an interface or abstract class.

    regards

  2. Jim Skim says:

    Hi

    I’m trying to understand if there’s a problem with unity’s ResolveAll not retuting the default instance. I’ve posted the question and would welcome your input?

    • xhalent says:

      You’re right – it struck me as **somewhat** bizarre that ResolveAll would only give named instances. Prehaps the assumption that if you were going to have more than one, then you’d want to name them. IMHO it’s fine to have a “default” instance and some other additional instances, but Unity doesn’t see it that way.

  3. Alex says:

    Very nice approach, most blogs just wrap the Resolve in a try catch, and in the catch just return null. I like this approach as it avoids the needless exceptions.

    My only question is, how can you be sure that serviceType will never be a concrete type? It’s entirely possible for the MVC framework to call GetService with a concrete type in which case this would fail.

    • xhalent says:

      If serviceType is a concrete type, such a controller class type, then the default unity behavior will be applied which will be to try and instantiate the instance using the greediest constructor.

      I’m only avoiding the default unity behavior for non-concrete service types.

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