Say I have the following class
MyComponent : IMyComponent {
public MyComponent(int start_at) {...}
}
I can register an instance of it with castle windsor via xml as follows
<component id="sample" service="NS.IMyComponent, WindsorSample" type="NS.MyComponent, WindsorSample">
<parameters>
<start_at>1</start_at >
</parameters>
</component>
How would I go about doing the exact same thing but in code? (Notice, the constructor parameter)
You need to pass in an IDictionary when you ask the container for the instance.
You'd use this Resolve overload of the IWindsorContainer:
or the non generic one:
So, for example: (assuming container is an IWindsorContainer)
Note that the key values in the dictionary are case sensitive.
Edit: Used the answers below code with the Fluent Interface :)
You can use the AddComponentWithProperties method of the IWindsorContainer interface to register a service with extended properties.
Below is a 'working' sample of doing this with an NUnit Unit Test.
Have you considered using Binsor to configure your container? Rather than verbose and clumsy XML you can configure Windsor using a Boo based DSL. Here's what your config will look like:
The advantage is that you have a malleable config file but avoid the problems with XML. Also you don't have to recompile to change your config as you would if you configured the container in code.
There's also plenty of helper methods that enable zero friction configuration:
You can get started with it here.
Try this