什么是城堡温莎,为什么要在乎? [英] What is Castle Windsor, and why should I care?

查看:172
本文介绍了什么是城堡温莎,为什么要在乎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个长期的Windows开发者,已经削减了Win32和早期的COM我的牙齿。我一直与.net自2001年以来,所以我在C#和CLR相当流畅。我从来没有听说过温莎城堡的,直到我开始参加堆栈溢出。我读过温莎城堡入门指南,但它不是点击。

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.

推荐答案

温莎城堡是管制刀具的反转。还有其他类似的。

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.

从这里开始:的http://tech.groups.yahoo.com/group/altdotnet/message/10434

假设你有一个电子邮件发送级别。 EmailSender。想象一下,你有另一个类WorkflowStepper。 ,里面WorkflowStepper你需要使用EmailSender

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

您总是可以说新EmailSender()发送(emailMessage);

但 - 使用 - 创建紧密耦合,是很难改变。 (这是一个很小的人为的例子毕竟)

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

那么,如果,不是newing这个坏小子的内心WorkflowStepper,你刚刚通过它到构造?

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

于是谁把它称为有新的了EmailSender。

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

新WorkflowStepper(emailSender) .STEP()

假设你有数百个这样的小类是只有一个责任(谷歌SRP)..和你用几他们WorkflowStepper的:

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

新WorkflowStepper(emailSender,alertRegistry,的DatabaseConnection).STEP()

想象一下,担心的不是细节 EmailSender 当你写 WorkflowStepper AlertRegistry

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步进= Container.Get< WorkflowStepper>();

你会得到一个真正的交易 WorkflowStepper 与自动填写在您需要的所有依存关系。

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

这只是发生的 - 因为它知道什么需要的东西。

It just happens - because it knows what needs what.

,你可以写在一个可测试的,可重复的方式更好地设计,DRY代码更少的缺陷。

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

这篇关于什么是城堡温莎,为什么要在乎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆