Closed GoogleCodeExporter closed 8 years ago
Using Autofac 2.4.5.724 with .NET 4.
Original comment by travis.illig
on 31 Mar 2011 at 11:14
Interesting - might need to give this one some thought.
Autofac currently doesn't distinguish between supplied and resolved parameters,
so it really just sees that two constructors can be satisfied and rolls a dice
to pick one.
Given the parameter architecture I don't think this could be changed very
easily, but I see how behaviour in this situation is tricky. Perhaps we should
just throw when multiple constructors with the same number of parameters match?
The extra line of configuration with UsingConstructor(x) would be a small pain,
but the unpredictable choice is probably worse...
Original comment by nicholas...@gmail.com
on 4 Apr 2011 at 10:20
I must just be getting lucky then, or maybe I'm not running into this as often
as I think I am. I always just assumed the behavior was something like:
* Get all the possible constructors.
* Get all the container-supplied arguments.
* Get all the manually-supplied parameters.
* From all the constructors, see which one matches the most manually-supplied
parameters.
* Failing that, fall back from most-available-parameters to
least-available-parameters. (That is, "most specific to least specific.")
The need to use UsingConstructor wasn't clear from
http://code.google.com/p/autofac/wiki/Autowiring - it looked like that was only
necessary to override the default behavior of "most to least specific
constructor." My misunderstanding.
In the short term, I'd propose two things:
1) Enhance the docs to explain the logic of which constructor is chosen,
specifically when there's a "tie" and one gets randomly chosen.
2) Throw an exception when there's an ambiguous constructor selection to force
folks to use UsingConstructor to choose.
Longer term, it'd be nice to have behavior like I described - where manually
supplied parameters take precedence over container parameters and influence the
logic that infers which constructor to select. I'm guessing folks wouldn't go
to the trouble of manually specifying parameters if they didn't intend for them
to be used.
Original comment by travis.illig
on 4 Apr 2011 at 2:56
Sounds good. I'm looking at #2 now - in the process optimising a bit of the hot
path through constructor selection, so worth spending some time in there anyway.
Original comment by nicholas...@gmail.com
on 10 Apr 2011 at 6:16
Exception now thrown in the case of ambiguity.
Original comment by nicholas...@gmail.com
on 10 Apr 2011 at 10:34
Original issue reported on code.google.com by
travis.illig
on 31 Mar 2011 at 11:13