What is Castle Windsor, and why should I care?

C#Dependency InjectionInversion of-ControlCastle Windsor

C# Problem Overview


I'm a long-time Windows developer, having cut my teeth on win32 and early COM. I've been working with .NET since 2001, so I'm pretty fluent in C# and the CLR. I'd never heard of Castle Windsor until I started participating in Stack Overflow. I've read the Castle Windsor "Getting Started" guide, but it's not clicking.

Teach this old dog new tricks, and tell me why I should be integrating Castle Windsor into my enterprise apps.

C# Solutions


Solution 1 - C#

Castle Windsor is an inversion of control tool. There are others like it.

It can give you objects with pre-built and pre-wired dependencies right in there. An entire object graph created via reflection and configuration rather than the "new" operator.

Start here: http://tech.groups.yahoo.com/group/altdotnet/message/10434


Imagine you have an email sending class. EmailSender. Imagine you have another class WorkflowStepper. Inside WorkflowStepper you need to use EmailSender.

You could always say new EmailSender().Send(emailMessage);

but that - the use of new - creates a TIGHT COUPLING that is hard to change. (this is a tiny contrived example after all)

So what if, instead of newing this bad boy up inside WorkflowStepper, you just passed it into the constructor?

So then whoever called it had to new up the EmailSender.

new WorkflowStepper(emailSender).Step()

Imagine you have hundreds of these little classes that only have one responsibility (google SRP).. and you use a few of them in WorkflowStepper:

new WorkflowStepper(emailSender, alertRegistry, databaseConnection).Step()

Imagine not worrying about the details of EmailSender when you are writing WorkflowStepper or AlertRegistry

You just worry about the concern you are working with.

Imagine this whole graph (tree) of objects and dependencies gets wired up at RUN TIME, so that when you do this:

WorkflowStepper stepper = Container.Get<WorkflowStepper>();

you get a real deal WorkflowStepper with all the dependencies automatically filled in where you need them.

There is no new

It just happens - because it knows what needs what.

And you can write fewer defects with better designed, DRY code in a testable and repeatable way.

Solution 2 - C#

Mark Seemann wrote and excellent book on DI (Dependency Injection) which is a subset of IOC. He also compares a number of containers. I cannot recommend this book enough. The book's name is: "Dependency Injection in .Net" https://www.manning.com/books/dependency-injection-in-dot-net

Solution 3 - C#

I think IoC is a stepping stone in the right direction on the path towards greater productivity and enjoyment of development team (including PM, BA an BOs). It helps to establish a separation of concerns between developers and for testing. It gives peace of mind when architecting which allows for flexibility as frameworks may come in and out.

The best way to accomplish the goal that IoC (CW or Ninject etc..) takes a stab at is to eliminate politics #1 and #2 remove need for developers to put on the facade of false understanding when developing. Do these two solutions not seem related to IoC? They are :)

Solution 4 - C#

Castle Windsor is Dependency Injection container. It means with the help of this you can inject your dependencies and use them without creating them with the help of new keyword. e.g. Consider you have written a repository or a service and you wish to use it at many places, you need to first register your service / repository and you can start using it after injecting it on the required place. You can take a look at the below tutorial which I followed to learn castle windsor.

link.

Hope it will help you.

Solution 5 - C#

Put simply. Imagine you have some class buried in your code that needs a few simple config values to do its job. That means everything that creates an instance of that class needs to get those dependencies, so you usually end up having to refactor loads of classes along the way to just pass a bit of config down to where the instance gets created.

So either lots of classes are needlessly altered, you bunch the config values into one big config class which is also bad... or worst still go Service Locator!

IoC allows your class to get all its depencencies without that hassle, and manages lifetimes of instances more explicitly too.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid HillView Question on Stackoverflow
Solution 1 - C#Matt HinzeView Answer on Stackoverflow
Solution 2 - C#IUnknownView Answer on Stackoverflow
Solution 3 - C#Michael SochaView Answer on Stackoverflow
Solution 4 - C#Rakesh BurbureView Answer on Stackoverflow
Solution 5 - C#andrew pateView Answer on Stackoverflow