microsoft/MinIoC

Consider non-generic methods/IServiceProvider

Closed this issue · 1 comments

Internally, I believe Container has everything as Type and object. Suppose a caller has a list of types to register and/or resolve, like this:

Type[] typesToRegister = new Type { typeof(A), typeof(B) };

foreach (Type toRegister in typesToRegister)
{
    container.Register(toRegister, toRegister);
}

Currently, I believe the caller would be required to use reflection to MakeGenericMethod. What's the ideal "Min" version of the container surface area? Perhaps:

IRegisteredType Register(Type interfaceType, Type implementationType);
IRegisteredType Register(Type interfaceType, Func<object> factory);

And for resolve:

object Resolve(Type interfaceType);

Does that feel like the right "Min" set? Extension methods could be built on top to support:

Register<T>(Type type); // current form
Register<T, TImplementation>(); // generic version for the second argument
Register(Type type); // class without a separate interface
Register<T>(); // generic version of above form
Register<T>(Func<T> factory); // current form
T Resolve<T>(); // current form

Thoughts?

On the resolve side, that signature also makes it easy to implement .NET's IServiceProvider. Maybe IScope becomes either:

public interface IScope : IServiceProvider, IDisposable
{
}

or

public interface IScope : IServiceProvider, IDisposable
{
    object Resolve(Type interfaceType);
}

(Just to keep the nice Resolve name also by default)

And then extension methods could do the rest.

Assuming an IServiceProvider implementation is properly "Min" … if not, it's easy to create a wrapper on top to do that as long as the non-generic method is there.