温莎城堡是什么,我为什么要关心?

我是一名长期的Windows开发人员,在Win32和早期的COM上初露锋芒。自2001年以来,我一直在使用.NET,所以我对C#和CLR非常熟悉。在我开始参加Stack Overflow之前,我从未听说过温莎城堡。我已经阅读了温莎城堡“入门”指南,但它没有点击。

教这只老狗新的技巧,并告诉我为什么我应该将温莎城堡集成到我的企业应用程序中。

75045 次浏览

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.

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

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

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.