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:
- Register – This means telling Castle.Windsor the services and the respective components implementing these services.
- Resolve – i.e. specify the root service and Castle.Windsor will load all the related dependencies.
- 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.
- Instantiate your IWindsor container
- “Execute” the actual code to do the work
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.