Getting started with Castle Windsor tutorial – Part 1


EDIT:

It would appear that this particular blog post is by far the most popular of my ramblings. Having now gone through the pain, I would say that if you need to get up and going quickly with using Castle Windsor as your IoC container, by all means read on.

However, chances are that like me, you were thrown in at the deep end and don’t fully understand what you’re doing when it comes to IoC. So do yourself a favour and get yourself a copy of Dependency Injection in .NET if you aspire to be a so called craftsman rather than being a hacker.

Although I have corresponded with the author on various occasions, I have no financial stake in you getting the book and I’d urge you to actually buy it to show your appreciation for the hard work that the author has obviously put into his book.

——————————–

This tutorial will show you how to set up Castler Windsor for a very simple project. I am basing this tutorial on the one that Simone Busoli wrote a few years ago.

Before proceeding, you should read his tutorial as he explains what Inversion of Control (IoC) means and why you should be using some form of IoC framework, of which Castle.Windsor is one. After you’ve grasped the concepts, come back to this tutorial which will walk you through how to update his sample project using the latest incarnation of Castle.Windsor (2.5.2).

Now that you understand what IoC is, and how Castle.Windsor can help you to use that design pattern, open up Simone Busoli’s sample project and reference the latest versions (2.5.2 as of this writing) of Castle.Core and Castle.Windsor.

Before delving into the code, I’d like to refer you to Castle Windsor’s documentation on “Using the container – how and where to call it“. It is important that you read this because I’ll be making use of “The Three Container Calls Pattern”, which really is not as complicated as it sounds.

The salient features of the three container calls pattern are:

  1. Register – This means telling Castle.Windsor the services and the respective components implementing these services.
  2. Resolve – i.e. specify the root service and Castle.Windsor will load all the related dependencies.
  3. Release – i.e. release the container.

Now to use Castle.Windsor there are in fact two other steps that need to be added to this pattern.

  1. Instantiate your IWindsor container
  2. Register
  3. Resolve
  4. “Execute” the actual code to do the work
  5. Release

In order to update Simone’s console application, replace the Program.Main method with the following:

————————————————-

————————————————-

I believe that the code is pretty much self explanatory but should there be any questions or comments, then please don’t hesitate to drop me a line. You can find the updated files here.

I hope that you will have found this tutorial useful and that you are now more confident in using Castle.Windsor. In the next post in this series, I’ll show you how to set up Castle.Windsor so that it can “automatically” register the classes.

Before ending this post, a big thanks to Simone Busoli, whose code I’ve “lifted” and adapted, Krzysztof Koźmic of Castle.Windsor for answering my questions and to Maikel Lehtveer for getting me over the line.

Advertisements
Tagged with:
Posted in Castle.Windsor, IoC
7 comments on “Getting started with Castle Windsor tutorial – Part 1
  1. David,

    good start 🙂

    In cases like yours when you explicitly specify types for your components and their services you can use generic overload of the methods, like this:

    Component.For<IHtmlTitleRetriever>().ImplementedBy<HtmlTitleRetriever>()

    this has two advantages:
    1. it is less typing 🙂
    2. Generic constraints on the API will enforce that your class HtmlTitleRetriever does indeed implement the IHtmlTitleRetriever interface

    I’m sure you did this just to keep the sample as simple and as basic as possible, but I would also encapsulate the registration code in an installer.

    cheers,

    looking forward to your follow up.

    You may also find it beneficial to keep the code for your tutorial on Github.

    • DavidS says:

      Hi Krzysztof,

      Thank you for reading my blog post and also for commenting.

      You brought up a very good point and yes you were right in saying that I wanted to keep this tutorial as simple and explicit as possible.

  2. Misha says:

    Hi David,

    This must be the best example I found for Windsor. It’s simple, and it works. A good place for me to start and finally understand what’s going on!

  3. […] led me onto David Siew’s article on getting started with Castle Windsor which in turn led me to this magnificent piece of work by Simone Busoli. Thanks guys, I have […]

  4. Arjunan says:

    Thanks a lot for sharing nice articles.I have gone through many articles on IOC.This one is great with simple example.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: