public interface IServiceLocator
{
...
T Resolve<T>(Type type) where T : class;
...
}
My question is, should this method return object instead of T?
Currently, the only role that T seems to play is a cast that happens
*after* the resolution of the type, but I don't know if that's
something this method should do.
It confused me a little bit, at least after reading the test calling
it:
[Test]
[Ignore("Need to figure out why this is not working for all
containers")]
public void
Register_With_Specified_Service_And_Type_Should_Return_Same_Type() {
Type serviceType = typeof(ILogger);
Type implType = typeof(SimpleLogger);
using (locator.Batch()) {
locator.Register(serviceType, implType);
}
var logger = locator.Resolve<ILogger>(implType);
Assert.AreEqual(implType, logger.GetType());
}
When it comes to what each of the IServiceLocator implementors
actually do, all of the lines above "var logger =
locator.Resolve<ILogger>(implType);" are irrelevant because the
container is resolving implType, not ILogger. No registration should
be necessary when resolving SimpleLogger.
To put this another way, I can call
"locator.Resolve<string>(implType)" and the container will resolve
implType through the container as expected... it will just throw when
it tries to cast it to a string.
Any thoughts?
You're 100% correct that the method should return object instead of T
since it's more useful that way. Should we mark it obsolete and add
the object Resolve(Type type) method? Seems to make sense, also we can
now have those unit tests be valid for the type.
Thoughts?
Darren