Question background:
I am using unity to resolve a class and all its dependencies.
The issue:
I want to pass into the constructor of the classes an interface type through Unity, I'm attempting to achieve this as follows:
public static IFacade UnityNewsFacadeResolver()
{
IUnityContainer unityContainer = new UnityContainer();
unityContainer.RegisterType<IFacade, NewsFacade>();
IFacade newsFacadeInstance = unityContainer.Resolve<NewsFacade>();
return newsFacadeInstance;
}
The following shows the 'NewsFacade' class that Unity sets the constructor parameter. Note it is passing in a 'NewsStoryHandler' concrete type, I want to pass in the interface this class is based on i.e INewsStoryHandler:
private INewsStoryHandler _NewsStoryHandler;
//*****Unity will only pass in a concrete implementation, I want to pass it as an interface but cannot:******
public NewsFacade(NewsStoryHandler newsStoryHandler)
{
if (newsStoryHandler == null)
{
throw new Exception("newsStoryHandler is null at the constrcutor");
}
_NewsStoryHandler = newsStoryHandler;
}
Currently Unity will not resolve and pass in an Interface type, it has to be a concrete implementation as shown above. This means I cannot Mock the interface when I unit test.
Can someone tell me why Unity will not resolve and pass in a type based on its interface?