Tuesday, March 22, 2011

Setting up Unity in a new project

Using the Common Service Locator and Unity is fairly simple with Nuget. This post records the steps in case I need them again.


  1. Add the CommonServiceLocator and Unity packages to the project using Nuget.
  2. Add the following initialisation code (for example in Application_Start):
    UnityContainer container = new UnityContainer();
                
    //configure container
    ...
    
    var unityServiceLocator = new UnityServiceLocator(container);
    ServiceLocator.SetLocatorProvider(() => unityServiceLocator);
    
  3. Use the IServiceLocator and ServiceLocator interface and class for DI and service location, respectively.

0 comments:

Post a Comment