IServiceLocator.Resolve<T>(Type type)

12 views
Skip to first unread message

Darren

unread,
Mar 20, 2010, 6:05:43 PM3/20/10
to MVC Turbine
I was looking at some of the ignored tests for the service locator to
see why they were failing for various IoC containers, and I have a
question about one of the methods:

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?

Javier Lozano

unread,
Mar 20, 2010, 10:07:19 PM3/20/10
to MVC Turbine
Honestly, I don't recall why the T Resolve<T>(Type type) method is in
there...I'm guessing that at one point, it seemed like a good idea. :P

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 Cauthon

unread,
Mar 21, 2010, 10:23:07 AM3/21/10
to MVC Turbine

Marking it as obsolete and adding a method works for me. I'll make
that update today and push it.


Darren

Javier Lozano

unread,
Mar 21, 2010, 10:28:03 AM3/21/10
to MVC Turbine
Awesome, we can add this to the v2.1 release since I'm still working
on it
Reply all
Reply to author
Forward
0 new messages