0

I am using Unity library for using dependency injection. I have a controller(product) and below is the constructor code

public ProductController(IService1 ser1,IService2 ser2,IService3 ser3,IService4 ser4)
{
    this._Service1 = ser1;
    this._Service2 = ser2;
    this._Service3 = ser3;
    this._Service4 = ser4;
}

And below is the unit test code for this controller.Which is injecting above 4 service class object to that controller's constructor.

  ProductController _productController;
  Mock<IService1> _ser1Mock;
  Mock<IService2> _ser2Mock;
  Mock<IService3> _ser3Mock;
  Mock<IService4> _ser4Mock;

  if(_productController!=null)
  {
     _productController = new ProductController(
         _ser1Mock.Object,_ser2Mock.Object,_ser3Mock.Object,_ser4Mock.Object) 
  }

Here i am injecting multiple dependencies(services) object to controller's constructor. And the unit test methods working well.But by this when i run the application then its not working.

It's giving 'error 500'.

And i want to know what we should do if you have multiple dependencies and you want to inject those dependencies to controller.Do we need to inject separately or we can inject in once(like i did above).

Below is the Bootstrapper file code using System.Web.Mvc; using Microsoft.Practices.Unity; using Unity.Mvc4;

public static class Bootstrapper
{
    public static IUnityContainer Initialise()
    {
        var container = BuildUnityContainer();

        DependencyResolver.SetResolver(
            new UnityDependencyResolver(container));

        return container;
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    }

    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterType<IService1Dal,Service1Dal>();
        container.RegisterType<IService1,Service1>();
        container.RegisterType<IService2Dal,Service2Dal>();
        container.RegisterType<IService2,Service2>();
        container.RegisterType<IService3Dal,Service3Dal>();
        container.RegisterType<IService3,Service3>();
        container.RegisterType<IService4Dal,Service4Dal>();
        container.RegisterType<IService4,Service4>();    
    }
}
Steven
  • 166,672
  • 24
  • 332
  • 435
Pawan
  • 2,150
  • 11
  • 45
  • 73
  • 1
    as far as I can tell, you are not injecting anything, you are just calling a constructor – thumbmunkeys May 01 '14 at 12:20
  • Show us your Unity configuration (XML or Fluent). That is probably where the issue is. Also, WHERE / HOW are you injecting / resolving? Not calling `new ProductController` I hope! – Belogix May 01 '14 at 12:25
  • @Belogix,i have updated the configuration code. – Pawan May 01 '14 at 12:36
  • 1
    What's the exception? – Ant P May 01 '14 at 12:39
  • Is Unity resolving controllers for the application? If it is, Unity will be injecting instances of the required components, so long as they are also registered with Unity. If Unity is not resolving controllers for you, take a look at http://stackoverflow.com/a/2797264/1831 that has an example of how to create a UnityControllerFactory – Russ Cam May 01 '14 at 12:41

1 Answers1

0

You need to ensure you Resolve what you need in order for Unity to inject things, at top-level, once you have done this you should not use container again explicitly. Check you are creating like this:

_productController = container.Resolve<IProductController>();

Also, you should be registering your ProductController with Unity too...

container.RegisterType<IProductController, ProductController>(); 
Belogix
  • 8,129
  • 1
  • 27
  • 32