What is the constructor resolution order?
Asked Answered
D

2

16

How does Castle Windsor determine which constructor to resolve when there are multiple constructors present?

Dinothere answered 4/9, 2009 at 2:48 Comment(0)
Y
24

All available constructors are modeled as candidates and basically a contest is run among them.

Each parameter in a constructor represents a dependency that can be either satisfied or not. For each constructor candidate, all dependencies are analyzed.

If a dependency can be satisfied, that's two points for the candidate. If it can't be satisfied, two points are subtracted from the candidate.

The constructor candidate with the most points wins and is chosen to instantiate the component.

The code for this algorithm is here.

Yepez answered 4/9, 2009 at 3:54 Comment(3)
Thanks for the info Mauricio. I'm having "problems" with Windsor in a S#arpArch related project and that helps a lot!Euterpe
Very well Said! I found this one more link on something similar which talks about the inherent problem with having overloaded constructors in first place in your components when you are using DI containers to instantiate your class: #7927186Euplastic
behavior is different in my asp.net MVC project. I have set MVC controller builder factory to use windsor DI container. Some how the MVC controller builder forces windsor to instantiate a controller class by calling the constructor with highest number of parameters and NOT the constructor whose maximum arguments are getting satisfied/registered with the DI container. Not sure if MVC controller builder factory somehow forces DI to behave this way or Windsor castle has some specific configuration around this to show such a behavior? In a console app it behaves exactly the way you have mentioned.Euplastic
Z
2

Castle Windsor will use the constructor with the most parameters whose dependencies it can resolve.

When there are two constructors with the same number of parameters that Windsor can resolve, the behavior used to be seemingly arbitrary but may have been fixed.

See this post by Krzysztof Kozmic

Zoography answered 16/7, 2012 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.