C# 编写您自己的 IOC 容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/386535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code your own IOC Container
提问by Peanut
Has anyone out there written their own IOC Container in C#? Or do the vast majority of folks use the various frameworks such as Spring. What are the pro's and con's of each?
有没有人用 C# 编写自己的 IOC 容器?还是绝大多数人使用各种框架,例如Spring。每个的优点和缺点是什么?
回答by Patrick Desjardins
Someone has wrote one in C# : http://ninject.org/.
有人用 C# 写了一个:http: //ninject.org/。
It's open source so you can get the code and see how this guy did it.
它是开源的,所以你可以得到代码,看看这个人是怎么做的。
回答by Cristian Libardo
回答by t3mujin
Unless there's a very good reason I wouldn't go reinvent the wheel and implement a IoC container myself, specially because there are are a lot of good options like Unity, Ninject or Spring.net.
除非有很好的理由,否则我不会自己重新发明轮子并实现 IoC 容器,特别是因为有很多不错的选择,例如Unity、 Ninject 或Spring.net。
If you need/want to remove the dependency to any of these IoC containers you may try out the Common Service Locatorinterface.
如果您需要/想要删除对任何这些 IoC 容器的依赖,您可以尝试使用Common Service Locator接口。
回答by Thedric Walker
James Kovacs presents a dnrTV episode on this subject here. Here also wrote an article. However during the article he mentions that you would probably want to use one of the pre-built ones. Since there are many diverse looks for them. Ninject, StructureMap, Autofac use a fluent interface. Spring, Castle Windsor, and Unity are more XML config driven. Castle Windsor can also use boo as an interface. Many have hooks to other frameworks such as Unity to EntLib or Castle Windsor to Monorail and the rest of the Castle Project.
James Kovacs 在这里展示了一个关于这个主题的 dnrTV 剧集。这里还写了一篇文章。但是在文章中,他提到您可能希望使用其中一种预构建的。因为它们有许多不同的外观。Ninject、StructureMap、Autofac 使用流畅的界面。Spring、Castle Windsor 和 Unity 更受 XML 配置驱动。Castle Windsor 也可以使用 boo 作为接口。许多都与其他框架挂钩,例如 Unity 到 EntLib 或 Castle Windsor 到 Monorail 以及 Castle 项目的其余部分。
So unless you really need or want something that is not provided by the IOC frameworks available, then why not use one of them.
因此,除非您确实需要或想要一些可用的 IOC 框架未提供的东西,否则为什么不使用其中之一。
回答by James Gregory
Ayendealso wrote about writing your own IoC container in his blog post Building an IoC container in 15 lines of code, I believe he's of the same opinion as everyone else though: don't build your own if you don't have to.
Ayende在他的博客文章用 15 行代码构建一个 IoC 容器中也写到了关于编写你自己的 IoC 容器的内容,但我相信他和其他人的观点是一样的:如果你不需要,不要构建你自己的容器。
回答by Ruben Bartelink
回答by Stephen Baker
I have written an IoC / DI Container in c# that implements the Common Service Locator. I wrote it mostly for learning purposes, but when I completed it, I decided to make it open source. If any of you would like to try out IInject, it can downloaded here.
我已经用 C# 编写了一个 IoC/DI 容器,它实现了公共服务定位器。我写它主要是为了学习目的,但当我完成它时,我决定将它开源。如果您想试用IInject,可以在此处下载。
回答by RameshVel
回答by jgauffin
Autofac is excellent.
Autofac 非常好。
I've written one myself using less than 15 lines. Just two extensionmethods to a dictionary.
我自己用不到 15 行写了一篇。字典的两个扩展方法。
回答by Alex Siepman
I created my own IoC container that makes it easier to debugthe creation of the object (even when you have no access to the container code). When the object is created, when pressing Step into (F11) you see the code to create the object. Full code can be seen here.
我创建了自己的 IoC 容器,它可以更轻松地调试对象的创建(即使您无法访问容器代码)。创建对象后,按 Step into (F11) 时,您会看到创建对象的代码。完整代码可以在这里看到。