Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injecting IEnumerable<Type> to constructor - unnamed registrations #130

Open
PatrikLukac opened this issue Jul 15, 2019 · 1 comment
Open

Comments

@PatrikLukac
Copy link

Hi guys,

I found out the following issue:

When I register multiple implementations for the same interface - but do it individually one by one, the unnamed one (if present) isn't resolved when using constructor injection of IEnumerable. On the other hand, it works when using ResolveAll method.

Consider following registration example:

TinyIoCContainer.Current.Register<Interface, Impl1>(); TinyIoCContainer.Current.Register<Interface, Impl2>("2");

When calling

TinyIoCContainer.Current.ResolveAll<Interface>();

resulting list contains both of registered implementations. But if it's used this way:

TinyIoCContainer.Current.Resolve<CtorTest>();

where class CtorTest looks like this:

`
public class CtorTest
{

	public CtorTest(IEnumerable<Interface> impls)
	{
	}
}

`

resulting list contains only named registrations. When I was digging in sources, I found out that in following method:

`
private object GetIEnumerableRequest(Type type)
{

        #if NETFX_CORE
        		var genericResolveAllMethod = this.GetType().GetGenericMethod("ResolveAll", type.GenericTypeArguments, new[] { typeof(bool) });
        #else
        var genericResolveAllMethod = this.GetType().GetGenericMethod(BindingFlags.Public | BindingFlags.Instance, "ResolveAll", type.GetGenericArguments(), new[] { typeof(bool) });
        //#endif
        return genericResolveAllMethod.Invoke(this, new object[] { false });
    }

`

the last row invokes method with last parameter set to false . This parameter is actually includeUnnamed parameter in ResolveAll method. When ResolveAll is called directly (as in fisrst example) it falls to default call, where includeUnnamed is set to true. That's why it works in first example, but doesn't work in second.

My question is - is that a purpose ? If so, can you examplain me why?

Thanks,
Patrik

@niemyjski
Copy link
Collaborator

I'm really not sure on that one, but to me it should be resolved as that's the behavior I've seen with the new Microsoft di. Can you create some tests covering all scenarios and add a pr for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants