Unity config newbie, I am trying to implement this in my project. However, I am stuck.
I receive the following error: The current type, System.Web.Mvc.IControllerFactory, is an interface and cannot be constructed. Are you missing a type mapping?
ContainerClass
public class ContainerBootstrapper
{
public static void ConfigureUnityContainer()
{
//simple registeration
container.RegisterType<IProduct, ProductHelper>(); //maps interface to a concrete type
System.Web.Mvc.DependencyResolver.SetResolver(new MyProjectControllerDependency(container));
}
}
DependencyResolver
public class MyProjectControllerDependency : IDependencyResolver
{
private IUnityContainer _container;
public MyProjectControllerDependency(IUnityContainer container)
{
this._container = container;
}
public object GetService(Type serviceType)
{
return _container.Resolve(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.ResolveAll(serviceType);
}
Controller:
public class ProductController : ApiController
{
private readonly IProduct _iproduct;
public ProductController(IProduct product)
{
this._iproduct = product;
}
//controller methods
}
Interface
public interface IProduct
{
List<ProductViewModel> GetProductByBarcode(string value);
string GetProductPrice(string value);
}
Helper
public class ProductHelper : IProduct
{
//private readonly IProduct _iproduct;
//public ProductHelper(IProduct iproduct)
//{
// this._iproduct = iproduct;
//}
public List<ProductViewModel> GetProductByBarcode(string value)
{
throw new NotImplementedException();
}
public string GetProductPrice(string value)
{
throw new NotImplementedException();
}
}
I don't understand, what I am missing? Can anyone point me in the right direction.