C# 企业库 Unity 与其他 IoC 容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/411660/
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
Enterprise Library Unity vs Other IoC Containers
提问by Yoann. B
What's pros and cons of using Enterprise Library Unity vs other IoC containers (Windsor, Spring.Net, Autofac ..)?
使用 Enterprise Library Unity 与其他 IoC 容器(Windsor、Spring.Net、Autofac ..)的优缺点是什么?
采纳答案by Chris Brandsma
I am preparing a presentation for a usergroup. As such I just went through a bunch of them. Namely: AutoFac, MEF, Ninject, Spring.Net, StructureMap, Unity, and Windsor.
我正在为用户组准备演示文稿。因此,我只是经历了其中的一堆。即:AutoFac、MEF、Ninject、Spring.Net、StructureMap、Unity 和 Windsor。
I wanted to show off the 90% case (constructor injection, which is mainly what people use an IOC for anyway). You can check out the solution here (VS2008)
我想展示 90% 的情况(构造函数注入,这主要是人们使用 IOC 的目的)。 您可以在此处查看解决方案(VS2008)
As such, there are a few key differences:
因此,有一些主要区别:
- Initialization
- Object retrieval
- 初始化
- 对象检索
Each of them have other features as well (some have AOP, and better gizmos, but generally all I want an IOC to do is create and retrieve objects for me)
他们每个人也有其他功能(有些有 AOP 和更好的小玩意,但通常我希望 IOC 做的就是为我创建和检索对象)
Note: the differences between the different libraries object retrieval can be negated by using the CommonServiceLocator: http://www.codeplex.com/CommonServiceLocator
注意:可以使用 CommonServiceLocator 否定不同库对象检索之间的差异:http: //www.codeplex.com/CommonServiceLocator
That leaves us with initialization, which is done in two ways: via code or via XML configuration (app.config/web.config/custom.config). Some support both, some support only one. I should note: some use attributes to help the IoC along.
这给我们留下了初始化,这通过两种方式完成:通过代码或通过 XML 配置 (app.config/web.config/custom.config)。有的同时支持,有的只支持一个。我应该注意:有些使用属性来帮助 IoC。
So here is my assessment of the differences:
所以这是我对差异的评估:
Ninject
宁捷
Code initialization only (with attributes). I hope you like lambdas. Initialization code looks like this:
仅代码初始化(带属性)。我希望你喜欢 lambda。初始化代码如下所示:
IKernel kernel = new StandardKernel(
new InlineModule(
x => x.Bind<ICustomerRepository>().To<CustomerRepository>(),
x => x.Bind<ICustomerService>().To<CustomerService>(),
x => x.Bind<Form1>().ToSelf()
));
StructureMap
结构图
Initialization code or XML or Attributes. v2.5 is also very lambda'y. All in all, this is one of my favorites. Some very interesting ideas around how StructureMap uses Attributes.
初始化代码或 XML 或属性。v2.5 也非常 lambda'y。总而言之,这是我的最爱之一。关于 StructureMap 如何使用属性的一些非常有趣的想法。
ObjectFactory.Initialize(x =>
{
x.UseDefaultStructureMapConfigFile = false;
x.ForRequestedType<ICustomerRepository>()
.TheDefaultIsConcreteType<CustomerRepository>()
.CacheBy(InstanceScope.Singleton);
x.ForRequestedType<ICustomerService>()
.TheDefaultIsConcreteType<CustomerService>()
.CacheBy(InstanceScope.Singleton);
x.ForConcreteType<Form1>();
});
Unity
统一
Initialization code and XML. Nice library, but XML configuration is a pain in the butt. Great library for Microsoft or the highway shops. Code initialization is easy:
初始化代码和 XML。不错的库,但 XML 配置很麻烦。适用于 Microsoft 或高速公路商店的绝佳图书馆。代码初始化很简单:
container.RegisterType<ICustomerRepository, CustomerRepository>()
.RegisterType<ICustomerService, CustomerService>();
Spring.NET
春网
XML only as near as I can tell. But for functionality Spring.Net does everything under the sun that an IoC can do. But because the only way to unitize is through XML it is generally avoided by .net shops. Although, many .net/Java shop use Spring.Net because of the similarity between the .net version of Spring.Net and the Java Spring project.
XML 只在我能说的那样接近。但是对于功能而言,Spring.Net 可以在阳光下完成 IoC 可以做的所有事情。但是因为统一的唯一方法是通过 XML,.net 商店通常避免使用它。尽管如此,由于 Spring.Net 的 .net 版本与 Java Spring 项目之间的相似性,许多 .net/Java 商店使用 Spring.Net。
Note: Configuration in the code is now possible with the introduction of Spring.NET CodeConfig.
注意:随着Spring.NET CodeConfig的引入,现在可以在代码中进行配置。
Windsor
温莎
XML and code. Like Spring.Net, Windsor will do anything you could want it to do. Windsor is probably one of the most popular IoC containers around.
XML 和代码。像 Spring.Net 一样,Windsor 会做任何你想做的事情。Windsor 可能是最流行的 IoC 容器之一。
IWindsorContainer container = new WindsorContainer();
container.AddComponentWithLifestyle<ICustomerRepository, CustomerRepository>("CustomerRepository", LifestyleType.Singleton);
container.AddComponentWithLifestyle<ICustomerService, CustomerService>("CustomerService",LifestyleType.Singleton);
container.AddComponent<Form1>("Form1");
Autofac
汽车制造商
Can mix both XML and code (with v1.2). Nice simple IoC library. Seems to do the basics with not much fuss. Supports nested containers with local scoping of components and a well-defined life-time management.
可以混合使用 XML 和代码(使用 v1.2)。不错的简单 IoC 库。似乎没有大惊小怪地做基础。支持具有组件本地范围和明确定义的生命周期管理的嵌套容器。
Here is how you initialize it:
以下是初始化它的方法:
var builder = new ContainerBuilder();
builder.Register<CustomerRepository>()
.As<ICustomerRepository>()
.ContainerScoped();
builder.Register<CustomerService>()
.As<ICustomerService>()
.ContainerScoped();
builder.Register<Form1>();
If I had to choose today: I would probably go with StructureMap. It has the best support for C# 3.0 language features, and the most flexibility in initialization.
如果我今天必须选择:我可能会选择 StructureMap。它对 C# 3.0 语言特性的支持最好,初始化的灵活性最大。
Note: Chris Brandsma turned his original answer into a blog post.
注意:Chris Brandsma 将他的原始答案变成了一篇博客文章。
回答by rodbv
As far as I've seen they are pretty much the same, except for a few implementation details here and there. The biggest advantage that Unity has over the competition is that it is provided by Microsoft, there are lots of companies out there that are afraid of OSS.
据我所知,它们几乎相同,除了这里和那里的一些实现细节。Unity 在竞争中最大的优势是它是由微软提供的,有很多公司害怕 OSS。
One disadvantage is that it's rather new so it might have bugs that the older players have already sorted out.
一个缺点是它相当新,所以它可能有老玩家已经解决的错误。
Having said that, you might want to check this out.
话虽如此,你可能想看看这个。
回答by Chris Brandsma
Correct me if I'm mistaken but I think Autofac itself supports XML Configuration as listed in this link: Autofac XML Configuration
如果我弄错了,请纠正我,但我认为 Autofac 本身支持此链接中列出的 XML 配置:Autofac XML 配置
回答by Anthony
Spring has one feature that it can inject parameters to constructor or property based on the parameter name or position. This is very useful if the parameter or property is a simple type (e.g. an integer, a boolean). See the example here. I don't think that this really makes up for Spring's inability to do config in code.
Spring 有一个特性,它可以根据参数名称或位置向构造函数或属性注入参数。如果参数或属性是简单类型(例如整数、布尔值),这将非常有用。请参阅此处的示例。我认为这并不能真正弥补 Spring 无法在代码中进行配置的问题。
Windsor can also do this, and can do it in code not config. (correct me if I'm wrong, I'm just going via what I've heard here).
Windsor 也可以做到这一点,并且可以在代码而不是配置中做到这一点。(纠正我,如果我错了,我只是通过我在这里听到的)。
I would like to know if Unity can do this.
我想知道 Unity 是否可以做到这一点。
回答by ehsanullahjan
One thing to note: Ninject is the only IoC container that supports contextual dependency injections (as per their Web site). However, because I don't have experience with other IoC containers, I can't tell if that holds.
需要注意的一件事:Ninject 是唯一支持上下文依赖注入的 IoC 容器(根据他们的网站)。但是,因为我没有使用其他 IoC 容器的经验,所以我不知道这是否成立。
回答by Richard
Old thread but since this is the first thing that Google showed me when I typed in unity vs spring.net...
旧线程,但由于这是 Google 在我输入 unity vs spring.net 时向我展示的第一件事......
Spring does do CodeConfig now if you don't like XML config
如果您不喜欢 XML 配置,Spring 现在会执行 CodeConfig
http://www.springframework.net/codeconfig/doc-latest/reference/html/
http://www.springframework.net/codeconfig/doc-latest/reference/html/
Also, Spring is much more than just an DI container, if you look at the 'Modules' section in the docs, the DI container is the foundation of the huge stack of things it does.
此外,Spring 不仅仅是一个 DI 容器,如果您查看文档中的“模块”部分,DI 容器是它所做的大量事情的基础。
回答by Jacobs Data Solutions
Just to add my 2 cents, I've tried both StructureMap and Unity. I found StructureMap to be poorly/misguidingly documented, a pain in the butt to configure, and clunky to use. Likewise, it doesn't seem to support scenarios like constructor argument overrides at resolution time, which was a key usage point for me. So I dropped it and went with Unity, and had it doing what I wanted in about 20 minutes.
为了增加我的 2 美分,我尝试了 StructureMap 和 Unity。我发现 StructureMap 的记录很差/误导性,配置起来很麻烦,而且使用起来很笨拙。同样,它似乎不支持解析时构造函数参数覆盖等场景,这对我来说是一个关键的使用点。所以我放弃了它并使用 Unity,让它在大约 20 分钟内完成我想做的事情。
回答by Josh Mouch
I personally use Unity, but only because it is from Microsoft. I regret the decision for one reason: the biggest thing it has against it has one big "bug" that causes it to constantly throws exceptions. You can ignore the exceptions while debugging. However it slows down your application tremendouslyif you run across it, since throwing an exception is an expensive operation. For example, I'm currently "fixing" this exception in one spot in my code where Unity's exceptions adds an extra 4 secondsto a page's render time. For more details and a workaround, see:
我个人使用 Unity,但只是因为它来自 Microsoft。我后悔这个决定有一个原因:它对它的最大反对有一个大“错误”,导致它不断抛出异常。您可以在调试时忽略异常。但是,如果您遇到它,它会极大地减慢您的应用程序的速度,因为抛出异常是一项昂贵的操作。例如,我目前正在我的代码中的一个地方“修复”这个异常,其中 Unity 的异常为页面的渲染时间增加了4 秒。有关更多详细信息和解决方法,请参阅:
Can Unity be made to not throw SynchronizationLockException all the time?