Table of contents
Configuring components parameters
Every component added to the container might have its dependencies. When it depends on a service which is registered on the container, you do not need to do anything, the MicroKernel is able to auto wire the dependency. But if it depends on a string (ie a connection string), or a int value (ie a port number), then you have to specify the value yourself as the MicroKernel will not be able to infer it.
There are also cases where you want to override a specific wire. This is also going to be explained in this document.
The formal reference can be found on Windsor Configuration Reference document.
Using the parameters node
On the configuration file, for each component node you can add a parameters node. The nodes enclosed by the parameters node shall be named after the constructor parameters or properties names on the component implementation. For example:
public class SmtpMailSender : IEmailSender { public SmtpMailSender(int port, string host) { ... } ... }
To configure the component above in the configuration file, use:
<configuration> <components> <component id="smtp.sender" service="Namespace.IEmailSender, AssemblyName" type="Namespace.SmtpMailSender, AssemblyName"> <parameters> <port>10</port> <host>smtphost</host> </parameters> </component> </components> </configuration>
The configuration above would still be valid if the component was coded using properties:
public class SmtpMailSender : IEmailSender { private int port; private string host; public SmtpMailSender() { } public int Port { get { return port; } set { port = value; } } public int Host { get { return host; } set { host = value; } } ... }
The MicroKernel uses TypeConverters to perform the conversions. We will describe TypeConverters in detail in the next document in the User's Guide.
Service override
A service override allows the programmer to interfere with the wiring process. Consider the following situation: there is a IEmailSender service contract and two implementations. A NewsLetterSenderService depends on an IEmailSender implementation. Which one the MicroKernel should use?
In this case use a service override to specify the implementation you want. Consider the classes and interfaces:
public interface IEmailSender { void Send(Message message); } public class SmtpEmailSender : IEmailSender { // Implements the IEmailSender using the SMTP protocol } public class SendMailEmailSender : IEmailSender { // Implements the IEmailSender connecting to a sendmail through pipes } public class NewsLetterSenderService { public NewsLetterSenderService(IEmailSender sender) { } ... }
The configuration for all components above would be:
<configuration> <components> <component id="smtp.sender" service="Namespace.IEmailSender, AssemblyName" type="Namespace.SmtpMailSender, AssemblyName" /> <component id="sendmail.sender" service="Namespace.IEmailSender, AssemblyName" type="Namespace.SendMailEmailSender, AssemblyName" /> <component id="newsletter" type="Namespace.NewsLetterSenderService, AssemblyName"> <parameters> <sender>${smtp.sender}</sender> </parameters> </components> </components> </configuration>
Note the parameters node for the newsletter:
<sender>${smtp.sender}</sender>
This forces the MicroKernel to use the component id specified within the ${} which is a service look up notation.