Reacting to my previous post, Krzysztof Koźmic was so kind to point out to me that I really should be using an IWindsorInstaller instead of writing the registration code in a static helper method (it did make me cringe a bit).

As it turns out, IWindsorInstaller is not a particularly well-described feature of Castle Windsor, so here's a quick introduction. Fortunately, it is very easy to understand.

The idea is simply to package configuration code in reusable modules (just like the Guice modules from Uncle Bob's post).

Refactoring the bootstrap code from my previous post, I can now move all the container configuration code into a reusable module:

public class BillingContainerInstaller : IWindsorInstaller
{
    #region IWindsorInstaller Members
 
    public void Install(IWindsorContainer container,
        IConfigurationStore store)
    {
        container.AddComponent<TransactionLog, 
            DatabaseTransactionLog>();
        container.AddComponent<CreditCardProcessor,
            MyCreditCardProcessor>();
        container.AddComponent<BillingService>();
    }
 
    #endregion
}

While I was at it, I also changed from the fluent registration API to the generic registration methods as I didn't really need the full API, but I could have left it as it was.

BillingContainerInstaller implements IWindsorInstaller, and I can now configure my container instance like this:

var container = new WindsorContainer();
container.Install(new BillingContainerInstaller());

The Install method takes a parameter array of IWindsorInstaller instances, so you can pass as many as you'd like.


Comments

Thomas #
It seemes that this feature is something like Registry in StructureMap?
2010-01-26 21:35 UTC
I have to admit that I'm not yet entirely up to speed on all the different DI Containers out there, but it's a common feature that several DI Containers have.
2010-01-26 22:04 UTC
This may be trivial, but remember to implement a ReleasePolicy when using transient objects. Otherwise you will use an unheard amount of RAM to hold all instances created, because .Release() will not be called on each one of them.
2010-01-27 07:14 UTC
Thanks for pointing that out - did you have this particular blog post in mind?

I'll have to investigate whether that still holds true. If so, I would be inclined to consider it a bug on Windsor's part, but it may be by design, influenced by considerations I simply have yet to realize.
2010-01-27 08:17 UTC
Windsor committer Krzysztof Koźmic was so kind to investigate this, and he left a comment at the blog post to the effect that this was a bug in Windsor. It is now fixed in Windsor 2.1, so having dependencies implement IDisposable should be enough.
2010-01-27 09:02 UTC
urkurk #
hi mark,

was wondering where i could find a sample application that uses castle windsor and asp.net webforms?
2010-04-04 09:52 UTC
Hi urkurk

Thank you for writing. Unfortunately, I'm not aware of such a sample application. Have you tried asking on the Castle Windsor forum?
2010-04-04 14:49 UTC
The system is not modular when you have to explicitly instanciate the BillingContainerInstaller on your code and pass the instance to the install method, I haven't investigate on the IWindsorInstaller yet, but I'm hoping that Windsor during the normal registration process of components canintercept those that implement that interface, and automatically call the Install method.

This sounds very similar to the startable facility though, with the important difference that in this case you have some more arguments.
2010-09-22 09:25 UTC
AFAIR the new version of Castle Windsor (2.5) enables you to specify installers in app.config.
2010-09-22 09:47 UTC
Sasireka #
Hi,

I am new to castle windsor. Could you please tell me the difference between container.AddComponent(...) & container.Register(component.For<...>..).

When we need to use AddComponent() / Register()?

Thanks in advance !
2011-08-11 09:39 UTC
AFAIK AddComponent is a legacy method and will be removed in future versions of Castle Windsor, but that I didn't know when I wrote that specific example :$
2011-08-11 09:44 UTC
Sasireka #
Hi Mark,

Thank you for your answer. I have an another clarification. What are the advantages offer the castle windsor framework rather than using the normal coding.

2011-08-12 05:34 UTC
The same advantage as any other DI Container.

In addition to that, Castle Windsor (and Unity and Spring.NET) also supports interception.
2011-08-12 06:44 UTC
sasireka #
Hi Mark,

I need another help about castle windsor.

Could you please tell about how to check whether the particular assembly registered or not in the castle container.

Actually i am using the below code for register the assembly. i need to avoid the registering process, if it was already registered.

container.Register(AllTypes.FromAssemblyNamed(pAssemblyName).Pick().WithService.DefaultInterface());
Thanks in advance.
2011-09-19 09:43 UTC
I can't recall off the top of my head - may I suggest that you ask on Stack Overflow?
2011-09-19 10:12 UTC


Wish to comment?

You can add a comment to this post by sending me a pull request. Alternatively, you can discuss this post on Twitter or somewhere else with a permalink. Ping me with the link, and I may respond.

Published

Tuesday, 26 January 2010 19:24:51 UTC

Tags



"Our team wholeheartedly endorses Mark. His expert service provides tremendous value."
Hire me!
Published: Tuesday, 26 January 2010 19:24:51 UTC