views:

395

answers:

3

I am learning castle.windsor following the tutorial online. this is the simple sample code:

public class Form1 {
    private readonly HttpServiceWatcher serviceWatcher;
    private System.ComponentModel.Container components = null;

public Form1()
{
    InitializeComponent();
}

public Form1(HttpServiceWatcher serviceWatcher) : this()
{
    this.serviceWatcher = serviceWatcher;
}
}

HttpServiceWatcher is in the xml conf file. My question is: who is calling the constructor that has the parameter: public Form1(Http....) ? at the program.cs i have this:

container.AddComponent("form.component",typeof(Form1));

Form1 form1 = (Form1) container["form.component"];

Application.Run(form1);
A: 

The dependency container itself creates the object (and thus calls the constructor).

Sander
A: 

but why does it call the constructor that has the parameter but not that parameterless constructor? what if i had 3 different constructors with 3 different parameters?

+1  A: 

The container calls the constructor when it creates the requested object. The constructor that gets called is the constructor with the most arguments that the container can satisfy.

Mikael Sundberg

related questions