Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Castle Windsor, and why should I care?

People also ask

What is Castle Windsor?

Home to royalty and 1,000 years of royal history. Windsor Castle is the oldest and largest occupied castle in the world. It is open to visitors throughout the year. Celebrate the Platinum Jubilee with us this year. Founded by William the Conqueror in the 11th century, it has since been the home of 39 monarchs.

Can you stay at Windsor Castle?

EXPLORE AND STAYA stay at Castle Hotel will place you in the heart Windsor and opposite Windsor Castle. This delightful package includes an afternoon tea, accommodation, breakfast, car parking (subject to availability), two tickets for entry into the delightful Savill Garden and bus tour tickets.

What is .NET Castle?

Castle Project (or Castle for short) is an open-source application framework for CLI platform implementations (e.g., . NET Framework). Castle Project. Stable release.


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.


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


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 :)


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.


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.