企业库统一VS其他IOC容器企业库、容器、VS、IOC

2023-09-02 21:08:23 作者:ヤ①木成山

什么是使用企业库统一VS其他的IoC容器(温莎,Spring.Net,Autofac ..)?的利弊

What's pros and cons of using Enterprise Library Unity vs other IoC containers (Windsor, Spring.Net, Autofac ..)?

推荐答案

我是preparing一个presentation的用户组。因此我刚刚经历了一群人去了。即:AutoFac,MEF,Ninject,Spring.Net,StructureMap,团结,温莎

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.

我想炫耀的90%的情况下(构造函数注入,这是人们主要使用什么是国际奥委会反正)。 在这里您可以检查出的解决方案(VS2008)

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)

这样,有几个关键的差异:

As such, there are a few key differences:

在初始化 对象检索

每个人有其他的功能,以及(有些AOP和更好的小玩意,但一般都是我想国际奥委会做的是创建和检索对象对我来说)

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)

注意:不同库之间的差异对象检索可以通过使用CommonServiceLocator被否定:http://www.$c$cplex.com/CommonServiceLocator

Note: the differences between the different libraries object retrieval can be negated by using the CommonServiceLocator: http://www.codeplex.com/CommonServiceLocator

这给我们留下了初始化,这是做有两种方式:通过code或通过XML配置(App.config中/ web.config中/ custom.config)。一些同时支持,有些只支持之一。我应该注意到:一些使用属性来帮助国际奥委会以及

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.

因此​​,这里是我的不同评价:

So here is my assessment of the differences:

code初始化只(带属性)。我希望你喜欢lam​​bda表达式。初始化code是这样的:

Code initialization only (with attributes). I hope you like lambdas. Initialization code looks like this:

 IKernel kernel = new StandardKernel(
                new InlineModule(
                    x => x.Bind<ICustomerRepository>().To<CustomerRepository>(),
                    x => x.Bind<ICustomerService>().To<CustomerService>(),
                    x => x.Bind<Form1>().ToSelf()
                    ));

StructureMap

初​​始化code或XML或属性。 V2.5也很lambda'y。总而言之,这是我的最爱之一。各地StructureMap如何使用属性的一些非常有趣的想法。

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.

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>();
 });

统一

初​​始化code和XML。尼斯库,但XML配置是一个痛苦的对接。大库Microsoft或公路商店。 code初始化很简单:

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:

 container.RegisterType<ICustomerRepository, CustomerRepository>()
          .RegisterType<ICustomerService, CustomerService>();

Spring.NET

XML只是尽可能接近我可以告诉。但对于功能Spring.Net做一个IoC可以做阳光下的一切。但因为统一化的唯一途径是通过XML它通常是由.NET商店避免。虽然,因为Spring.Net的.NET版本和Java春天项目之间的相似性,许多.NET / Java的店使用Spring.Net。

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.

注意:配置在code是现在可以引进春天。 NET codeConfig 。

Note: Configuration in the code is now possible with the introduction of Spring.NET CodeConfig.

XML和code。像Spring.Net,温莎会做任何你能想到的东西做的事。温莎可能是最流行的IoC容器之一左右。

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.

IWindsorContainer container = new WindsorContainer();
container.AddComponentWithLifestyle<ICustomerRepository, CustomerRepository>("CustomerRepository", LifestyleType.Singleton);
container.AddComponentWithLifestyle<ICustomerService, CustomerService>("CustomerService",LifestyleType.Singleton);
container.AddComponent<Form1>("Form1");

Autofac

可以混合XML和code(含V1.2)。尼斯简单的IoC库。似乎做了基本没有太大大惊小怪。支持嵌套容器部件的本地范围和定义良好的生命周期管理。

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.

下面是你如何初始化:

var builder = new ContainerBuilder();
builder.Register<CustomerRepository>()
        .As<ICustomerRepository>()
        .ContainerScoped();
builder.Register<CustomerService>()
        .As<ICustomerService>()
        .ContainerScoped();
builder.Register<Form1>();

如果我今天不得不选择:我可能会去与StructureMap。它有C#3.0语言功能最好的支持,和最灵活的初始化。

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.

注意:克里斯Brandsma把他原来的答复成的博客文章。

Note: Chris Brandsma turned his original answer into a blog post.

 
精彩推荐
图片推荐