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>();
}
}