Getting started with Windsor Castle
Continuing on our quest on the SOLID principle I though I will do a “quick start” tutorial for Windsor Castle as an Inversion of Control Container,as this will touch upon the ‘D‘ i.e.Dependency inversion principle of the SOLID principle.
If you are skeptic about whether to use dependency inversion principle and the IoC Container in your project, just ask yourself these 3 important questions about the design of your application.
1. Is it hard to change because every change affects too many other parts of the system ?
2. When you make a change, unexpected parts of the system break ?
3. Is it hard to reuse your component in another application because it cannot be dismantle from the current application ?
and then think that how best can you solve these problems. If you can solve these problems with improving your design or architecture then you can probably do with out it. Another thing to consider is that how big and complex your system will grow in future.
Before writing the code let’s have a look at some of popular IoC Container.
- Windsor Castle
- Structure Map
Today I am going to use Windsor Castle as I am more familiar with this container. The code for this sample is going to be the Loan Service project which I posted in the 3 part series of “The Art of Mocking .
First download the latest version of Windsor Castle from here unzip the files and add reference to the two assemblies Castle.Core.dll & Castle.Windsor.dll. This is a significant change from the previous version as in the previous version you had to add 4 assemblies.
Here is the full code for the Load Service which we did a full refactoring and redesigning in “The Art of Mocking” post.
The ICreditHistory interface
The CreditHistory implementation which calls the real webservice
If you remember we extracted the interface out of the returning result so that we can fake it and here is the interface for that.
And here is the actual CreditResult implementation
And here is the fake credit result class which we use with Mock library to return the desired object.
And this is the Applicant class whose age we will use to evaluate to approve the loan, I have purposely hide the rest of the property and this is not how one would implement a real life class.
Now we will remove the dependency of even the LoanApprovalClass so that Windsor Castle can resolve the class and it’s dependencies.
And now the actual LoanApprovalService class which implements the ILoanApprovalService interface.
Phew that’s a lot of code and we are almost there …
Now we will create another class which will hide the complexity of creating a dependency container and all the classes it’s going to inject the dependency for and we are encouraging the single responsibility principle.
As you can see this class contains IWindsorContainer inteface and through constructor injection we set it’s value.
The “Register” method of this containers takes 2 parameters, first one is interface and the second parameter is the concrete class which implements this interface.
In the first line inside the register parameter we are telling the DependencyRegister class that whenever you see an interface ICreditHistoryCheck return the CreditHistoryCheck. This way we have remove all the hard dependency of all the classes and can inject new classes in the future.
Also notice that I have made the method as virtual so that I can override the method If I want to register a different set of concrete class with minimal code change.
Another way to register dependencies is using a config file but I think maintaining a xml config file for hundreds of classes is going to be much difficult. Moreover I like it using this way is because of the fluent interface it has got. (If you are doing any framework/usable library development I recommend you provide fluent interface to your library.)
And this is the code in Program.cs
So I hope this quick start tutorial will give you an insight into the IoC/Dependency Injection and will help you to start using it in your projects.