Castle Windsor: Between multiple constructors why does Castle pick the one with incorrect argument types?

246 Views Asked by At

In a scenario where a class has multiple constructors I understand that Castle Windsor has an algorithm used to detect the 'greediest' constructor and pick that one to resolve a component.

But why would a constructor be picked to start with if it doesn't match the parameter names and types specified in config.

For example, assume the following config:

<configuration>
<component id="MyComponent"  type="MyType"  service="IMyInterface">
  <parameters>
    <firstParam>${firstComponent}</firstParam>
    <secondParam>${secondComponent}</secondParam>
    <thirdParam>${thirdComponent}</thirdParam>
  </parameters>
</component>

<component id="MyOtherComponent"  type="MyType"  service="IMyInterface">
  <parameters>
    <firstParam>${firstComponent}</firstParam>
    <secondParam>${secondComponent}</secondParam>
    <forthParam>${forthComponent}</forthParam>
  </parameters>
</component>

and class MyType:

public class MyType : IMyInterface {

public MyType(IFirstComponent firstParam, ISecondComponent secondParam, IThirdComponent thirdParam){
    // do stuff
}

public MyType(IFirstComponent firstParam, ISecondComponent secondParam, IForthComponent forthParam){
    // do other stuff
}

}

What I'd expect the container to do is invoke the first constructor for MyComponent and the second constructor for MyOtherComponent. Instead, the second constructor is getting invoked for both components.

Why does Castle invoke a constructor that clearly does not match the parameter names (and types) I have specified in config?

0

There are 0 best solutions below