views:

1292

answers:

2

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.

+38  A: 

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

And let google guide you onward.

Edit..

Okay.. some more. 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.

Matt Hinze
AWESOME! Well written! NOW I'm excited about it.
David Hill
Damn, I want to upvote your post twice.
David Hill
Thanks. There's a lot more to it. I picked a very simple and painfully concrete example. You can use interfaces to switch out implementations. You can auto-configure entire assemblies. You can specify lifecycles like singleton or per-http-request, etc. Keep going - it will change your work.
Matt Hinze
And to help the Java folks: This is Guice for .NET ;-)
Mark Renouf
I use windsor with NHibernate and the WCF facility - once you start playing with it and you see what you can do with so few lines of code - you never look backit cleans up your codebase too and makes it more readable as there tends to be less wiring and messing about and you're left with the real nuts and bolts :)
cvista
+1  A: 

Castle Windsor appears to be a set of technologies similar to Spring for Java, an inversion-of-control (IOC) container, an MVC framework, a persistence layer, and assorted other goodies.

Joe Skora
Windsor is just the container.
Matt Hinze