I'm starting to use MEF with my MVC application, so that it follows SOLID priciples.
I asked this question last week: Should a dependency be injected many "levels" up than it is needed? and part of what I asked (in the comments) was, if you have two constructors;
One constructor for Dependancy Injection
Another construtor with no parameters
And the contstructor with no parameters instantiates instances of the dependancies needed, then this should make a Service Locator moot. The response was that a class should not instantiate it's own dependancies. OK, I get this, but looking at this MS tutorial on MEF:
http://msdn.microsoft.com/en-us/library/hh708870(v=vs.110).aspx
They say you should have two constructors:
public HomeController() : this(new TraceLogger())
{
}
public HomeController(ILogger logger)
{
_logger = logger;
}
But from what I've been told, the first constructor is a no-no as the class is instantiating it's own dependacy?
Also, in the tutorial, MS just say; replace the second construtor with this:
public HomeController([Import("myTraceLogger")]ILogger logger)
{
_logger = logger;
}
What's the point in that? I still have to supply an instance of of the ILogger... unless I'm missing something? I removed the default constructor:
public HomeController() : this(new TraceLogger())
{
}
And the application just says: "No parameterless constructor defined for this object." So I must have to supply an instance, because if MEF was creating a new instance, surly it would work without a default constructor... and if not, what's the point in using MEF? I might as well just do the dependancy injection myself.