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

Func<T,U,...> should not be resolvable if any parameter types are duplicated #269

Closed
alexmg opened this issue Jan 22, 2014 · 7 comments
Closed
Labels

Comments

@alexmg
Copy link
Member

alexmg commented Jan 22, 2014

From [email protected] on October 23, 2010 06:55:43

What steps will reproduce the problem? 1. Have a class you want to inject that has a constructor with three or more parameters, at least two having the same type. For example: public InjectMe(string sJuan, int iJuan, string sToo, int iToo){}

  1. Try to inject it using the Func<X,Y,B> relationship type. for example: public Program(Func<string, int, string, int, InjectMe> createInjectMe){}
  2. In the Program from the example above, call createInjectMe("hello", 11, "world", 99); What is the expected output? What do you see instead? I expect it to fail. Or, I expect the constructor of InjectMe to get "hello" for sJuan and "world" for sToo. 11 for iJuan and 99 for iToo.
    what I see instead is the constructor of InjectMe gets "hello" for sJuan AND sToo... and 11 for iJuan AND iToo. What version of the product are you using? On what operating system? 2.2.4.900. Windows 7 Please provide any additional information below. I love the Func<> feature!

Original issue: http://code.google.com/p/autofac/issues/detail?id=269

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From [email protected] on October 22, 2010 22:55:29

Thanks for the report - I think failing is the appropriate thing to do here, ordering doesn't make a lot of sense in loosely-coupled scenarios.

To make your scenario work, declare and resolve a custom delegate type - for custom delegates, parameters are matched by name rather than by type.

Regards,
Nick

Summary: Func<T,U,...> should not be resolvable if any parameter types are duplicated
Status: Accepted

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From [email protected] on October 25, 2010 10:40:24

That works... Awesome! Thanks!

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From [email protected] on June 19, 2011 10:18:53

To throw in my 2 cents... We ran into this too and it was rather frustrating. The instinct of everyone was that this scenario should work, and it took us quite a bit of debugging before we just tried registering a custom delegate. As a result, we have over 10 delegates registered this way, and it's rather annoying.

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on September 21, 2012 09:29:31

Labels: Module-Core

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on May 13, 2013 18:00:05

Issue 429 has been merged into this issue.

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on December 17, 2013 12:12:28

I have a solution working where:

  • Func<T, U, ...> will throw a DependencyResolutionException when invoked IF it is a generated/implicit Func<T, U...> relationship delegate AND there are input parameter types duplicated.
  • You can explicitly register a Func<T, U...> implementation of your own to override the auto-generated registration if you really want to.
  • Custom delegates with duplicate type parameters can be used if you need to duplicate types.

I didn't have the DependencyResolutionException thrown until the Resolve call happens so you can register an override. Throwing too early made it so you couldn't do that.

The only potentially weird thing is that if you have duplicate types in the constructor of your object but not in the requested generated Func, you'll get odd behavior.

For example...

public class DuplicateParams
{
public DuplicateParams(int a, int b, string c) {...}
}

var func = container.Resolve<Func<int, string>>();

When you do that, the function WILL resolve the class, it'll just pass the same value in for the integer type parameter. So...

func(1, "2")

is the same as calling

new DuplicateParams(1, 1, "2")

That is the default behavior for Autofac, though, so I thought that was OK.

I'll write a few more tests up for this and commit it.

Status: Started
Owner: travis.illig
Cc: alex.meyergleaves [email protected]

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on December 17, 2013 12:30:36

This issue was closed by revision b23f07398a4d .

Status: Fixed

@alexmg alexmg closed this as completed Jan 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant