Since the question is debatable I'm not going to say "use this or that"
Looks like using Service Locator is not a bad thing if you are OK to depend on it (and we usually do anyway on some DI framework). With DI we can easily change the framework, with Service Locator we create coupling to SL part of the framework.
In regards to Bryan Watts answer when you read later on in
Service Locator vs Dependency Injection
...With [constructor] injection there is no explicit request, the service appears in the application class - hence the inversion of control.
Inversion of control is a common feature of frameworks, but it's something that comes at a price. It tends to be hard to understand and leads to problems when you are trying to debug. So on the whole I prefer to avoid it unless I need it. This isn't to say it's a bad thing, just that I think it needs to justify itself over the more straightforward alternative.
And then if you read later is another justification to actually use constructor injection (Inversion of control).
My opinion is that in small projects it's OK to use SL, as the main thing is not to create coupling between our custom developed classes.
Using StructureMap exmaple this should be acceptable:
public class Demo
{
private ISomething something = ObjectFactory.GetInstance<ISomething>();
private IFoo foo = ObjectFactory.GetInstance<IFoo>();
}
Yes the code is depending on SM Frx, but how often do you change DI Frx anyway?
And for unit testing a mock can be setup
public class SomeTestClass
{
public SomeTest()
{
ObjectFactory.Inject<ISomething>(SomeMockGoesHere);
ObjectFactory.Inject<IFoo>(SomeMockGoesHere);
Demo demo = new Demo() //will use mocks now
}
}
the advantages of using Resolve instead of Constructor Injection is that you don't need to create classes that have 5 parameters in the constructor
but you may end up with making more "plumbing" for unit testing.