我应该使用哪个依赖项注入工具?

时间:2022-09-10 23:09:44

I am thinking about using Microsoft Unity for my Dependency Injection tool in our User Interface.

我正在考虑在我们的用户界面中使用Microsoft Unity作为我的依赖注入工具。

Our Middle Tier already uses Castle Windsor, but I am thinking I should stick with Microsoft.

我们的中间层已经使用了Castle Windsor,但我认为我应该继续使用微软。

Does anyone have any thoughts about what the best Dependency Injection tool is?

有没有人想过最好的依赖注入工具是什么?

10 个解决方案

#1


11  

Sticking to one container is not really important, if your system has been designed with the IoC/DI in mind. With the proper approach you can easily change the IoC library down the road.

如果您的系统是按照IoC/DI设计的,那么坚持使用一个容器并不重要。使用适当的方法,您可以很容易地更改IoC库。

And, of course, the container has to provide enough flexibility to support common widely used scenarios (lifecycle management, proper container nesting, XML and code configuration, interception, fast resolution).

当然,容器必须提供足够的灵活性来支持通用的广泛使用场景(生命周期管理、适当的容器嵌套、XML和代码配置、拦截、快速解析)。

I'd recommend to pick between Castle (widely used and have a lot of integration libraries) and Autofac (lightweight, fast and has proper container nesting, but is not that widely used)

我建议在Castle(广泛使用并且有很多集成库)和Autofac(轻量级、快速、具有适当的容器嵌套,但是没有那么广泛使用)之间进行选择。

There is a comprehensive list of IoC containers by Hanselman

有一份汉斯曼综合的国际奥委会集装箱清单

PS: You do not want to use Unity

PS:你不想使用Unity

#2


61  

Having recently spiked the use of 6 of these (Windsor, Unity, Spring.Net, Autofac, Ninject, StructureMap) I can offer a quick summary of each, our selection criteria and our final choice.

最近加入了其中的6个(温莎,统一,春天)。Net, Autofac, Ninject, StructureMap)我可以提供一个快速的总结,我们的选择标准和最终的选择。

Note: we did not look at PicoContainer.Net as one of our team considered the .Net port to be quite poor from the Java version. We also did not look at ObjectBuilder, as Unity is built on top of ObjectBuilder2 and was considered to be a superior choice by default.

注意:我们没有看PicoContainer。Net作为我们团队的一员,认为。Net端口与Java版本相比非常差。我们也没有查看ObjectBuilder,因为Unity构建在ObjectBuilder2之上,默认情况下被认为是一个更好的选择。

Firstly, I can say that more or less all of them are all pretty similar and it really comes down to what really works best for you, and your specific requirements. Our requirements included:

首先,我可以说,它们或多或少都是非常相似的,这真的取决于什么最适合你,以及你的具体要求。我们的要求包括:

Requirements

  • Constructor based injection (we intend not to use property, field or method injection)
  • 基于构造函数的注入(我们不打算使用属性、字段或方法注入)
  • Programmable configuration (not XML)
  • 可编程配置(而不是XML)
  • container hierarchies (one per application, per request and per session to more implicitly bind component lifetimes scope to container)
  • 容器层次结构(每个应用程序、每个请求和每个会话一个,以更隐式地将组件生存期范围绑定到容器)
  • component lifetime management (for more granular scoping eg transient / singleton)
  • 组件的生命周期管理(对于更细粒度的scoping(如瞬态/单例)
  • injection from interface to type or concrete instance (eg ILogger -> typeof(FileLogger) or ILogger -> new FileLogger() )
  • 从接口到类型或具体实例的注入(如ILogger -> typeof(FileLogger)或ILogger -> new FileLogger())
  • advanced component creation / "creaton event mechanism" for pre/post initialisation
  • 高级组件创建/“creaton事件机制”用于预初始化/后初始化
  • correct disposal of IDisposable components on container tear down
  • 正确处理集装箱拆卸上的一次性部件
  • well documented and/or online information readily available

    有很好的文件和/或在线信息

    Note: whilst performance was a requirement it was not factored in the selection as it seemed that all containers reviewed were similar according to this benchmark

    注意:虽然性能是一个要求,但是在选择中并没有考虑它,因为根据这个基准,似乎所有评审的容器都是相似的

Test

Each container was used in a typical Asp.Net webforms project (as this was our target application type). We used a single simple page with with a single simple user control, each inheriting from a base page / base control respectively. We used 1 container on the BasePage for a "per request" scope container and 1 on the global.asax for an "application" scope and attempted to chain them together so dependencies could be resolved from both containers.

每个容器都使用在一个典型的Asp中。Net webforms项目(因为这是我们的目标应用程序类型)。我们使用一个简单的页面和一个简单的用户控件,每个用户控件分别继承一个基本页面/基本控件。我们在BasePage上对“每个请求”范围容器使用了一个容器,在全局容器上使用了一个容器。asax用于“应用程序”范围,并试图将它们链接在一起,以便可以从两个容器中解析依赖性。

Each web application shared a contrived set of domain objects simulating multi-levels of dependency, scope type (singleton/transient) and also of managed and unmanaged classes (IDisposable required). The "top level" dependency components were manually injected from the methods on the BasePage.

每个web应用程序都共享一组虚构的域对象,它们模拟了多级别的依赖关系、范围类型(单例/暂态),以及托管和非托管类(即必需的)。“顶层”依赖项组件是从BasePage上的方法手动注入的。

Results

Windsor - Satisfied all the criteria and has a good case history, blogger community and online documentation. Easy to use and probably the defacto choice. Advanced component creation through Factory facility. Also allowed chaining of individually created containers.

温莎-满足所有的标准,有一个良好的案例历史,博客社区和在线文档。易于使用,可能是事实上的选择。通过工厂设备创建先进组件。还允许对单独创建的容器进行链接。

Spring.Net - Verbose and unhelpful documentation and no-obvious / easy for programmable configuration. Did not support generics. Not chosen

春天。Net -详细和无帮助的文档和不明显/易于编程的配置。不支持泛型。没有选择

Ninject - Easy to use with good clear documentation. Powerful feature set fulfilling all our requirements except container hierarchies so unfortunately was not chosen.

Ninject:易于使用,具有清晰的文档说明。强大的特性集满足我们的所有需求,除了容器层次结构,因此不幸的是没有选择。

StructureMap - Poorly documented, although had quite an advanced feature set that met all of our requirements, however there was no built-in mechanism for container hierarchies although could be hacked together using for loops see here The lambda expression fluent interface did seem a little over complicated at first, although could be encapsulated away.

StructureMap——糟糕的记录,虽然经历了一个相当先进的特性集,满足我们所有的要求,但是没有内置机制容器层次结构虽然可以通过for循环看到lambda表达式的连贯接口也似乎有点太过复杂了,虽然可以封装。

Unity - Well documented, easy to use and met all our selection criteria and has an easy extension mechanism to add the pre/post creation eventing mechanism we required. Child containers had to be created from a parent container.

统一-有良好的文档,易于使用和满足我们所有的选择标准,并有一个简单的扩展机制来添加我们需要的预/后创建事件机制。必须从父容器创建子容器。

Autofac - Well documented and relatively easy to use, although lambda expression configuration does seem a little over complicated however, again, can be easily encapsulated away. Component scoping is achieved through a "tagging" mechanism and all components are configured up front using a builder which was a little inconvenient. Child containers were created from a parent and assigned components from a "tag". Allowed generic injection.

Autofac—文档化良好,使用起来相对容易,尽管lambda表达式配置看起来确实有点复杂,但是同样,可以很容易地封装。组件确定范围是通过“标签”机制实现的,所有组件都是使用构建器预先配置的,这有点不方便。子容器由父容器创建,并从“标记”中分配组件。允许通用注入。

Conclusion

Our final choice was between Windsor and Unity, and this time around we chose Unity due to its ease of use, documentation, extension system and with it being in "production" status.

我们最后的选择是温莎和Unity之间,这次我们选择了Unity,因为它易于使用、文档、扩展系统,并且它处于“生产”状态。

#3


12  

Here is a good article which compares .NET IoC containers. http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

这里有一篇比较。net IoC容器的好文章。http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

#4


5  

I started using Autofac a year ago and haven't looked back since..

一年前我开始使用Autofac,从那以后我再也没有回过头来看过它。

#5


4  

I'm an Autofac fan, but both Windsor and Unity will do a good job (though Windsor is more capable than unity and doesn't require attributing your code). There's plenty of good non technical reasons for sticking to a single container in a system though.

我是Autofac的粉丝,但温莎和Unity都能做得很好(尽管温莎比Unity更有能力,也不需要为你的代码赋值)。但是,在系统中坚持使用单个容器有很多好的非技术原因。

#6


2  

Use what works. Most have features that are unique to them, and almost all are more feature-rich than Unity. If unity is all that you need, you can certainly use it.

使用作品。大多数都有自己独有的特性,几乎所有的特性都比统一丰富。如果统一是你所需要的,你当然可以使用它。

Using Microsoft's unity just because it's from Microsoft is a poor way to make a decision. Think about what you need and why, and choose the one that fits your needs.

仅仅因为来自微软而使用微软的unity是一个糟糕的决策方式。想想你需要什么,为什么需要,选择适合你需要的。

However, I second the notion of sticking to a single container if possible.

但是,如果可能的话,我支持只使用一个容器的概念。

#7


2  

I've been using the Managed Extensibility Framework and found it quite easy to work with. It's been integrated into .NET 4.

我一直在使用管理的可扩展性框架,并且发现它很容易使用。它被集成到。net 4中。

#8


1  

https://*.com/questions/71041/which-single-iocdi-container-would-you-recommend-using-and-why

https://*.com/questions/71041/which-single-iocdi-container-would-you-recommend-using-and-why

#9


0  

Unless you already have experience and a personal preferance for a particular sub-technology utilized by one of the possible IoC container solutions, they all function well and I don't see any one in particular with a "killer function" that makes it stand out from the others. Unity is probably the best fit for solutions already utilizing the P&P Enterprise Library 4.x...

除非您已经有经验,并对一种可能的IoC容器解决方案所使用的特定子技术有个人偏好,否则它们都能很好地工作,而且我没有看到任何一种具有“杀手功能”的子技术能让它脱颖而出。Unity可能是已经使用P&P企业库4.x的解决方案的最佳选择。

#10


0  

IoC Container Benchmark - Performance comparison has performance and features comparison tables for 20+ products and keep them up-to-date.

IoC容器基准——性能比较具有20多种产品的性能和特性比较表,并保持它们是最新的。

The conclusion from the article:

本文的结论是:

SimpleInjector, Hiro, Funq, Munq and Dynamo offer the best performance, they are extremely fast. Give them a try!

SimpleInjector, Hiro, Funq, Munq和Dynamo提供了最好的性能,它们非常快。给他们一个尝试!

Especially Simple Injector seems to be a good choice. It's very fast, has a good documentation and also supports advanced scenarios like interception and generic decorators.

特别简单的喷射器似乎是一个不错的选择。它非常快,有很好的文档,并且支持高级场景,如拦截和通用装饰器。

#1


11  

Sticking to one container is not really important, if your system has been designed with the IoC/DI in mind. With the proper approach you can easily change the IoC library down the road.

如果您的系统是按照IoC/DI设计的,那么坚持使用一个容器并不重要。使用适当的方法,您可以很容易地更改IoC库。

And, of course, the container has to provide enough flexibility to support common widely used scenarios (lifecycle management, proper container nesting, XML and code configuration, interception, fast resolution).

当然,容器必须提供足够的灵活性来支持通用的广泛使用场景(生命周期管理、适当的容器嵌套、XML和代码配置、拦截、快速解析)。

I'd recommend to pick between Castle (widely used and have a lot of integration libraries) and Autofac (lightweight, fast and has proper container nesting, but is not that widely used)

我建议在Castle(广泛使用并且有很多集成库)和Autofac(轻量级、快速、具有适当的容器嵌套,但是没有那么广泛使用)之间进行选择。

There is a comprehensive list of IoC containers by Hanselman

有一份汉斯曼综合的国际奥委会集装箱清单

PS: You do not want to use Unity

PS:你不想使用Unity

#2


61  

Having recently spiked the use of 6 of these (Windsor, Unity, Spring.Net, Autofac, Ninject, StructureMap) I can offer a quick summary of each, our selection criteria and our final choice.

最近加入了其中的6个(温莎,统一,春天)。Net, Autofac, Ninject, StructureMap)我可以提供一个快速的总结,我们的选择标准和最终的选择。

Note: we did not look at PicoContainer.Net as one of our team considered the .Net port to be quite poor from the Java version. We also did not look at ObjectBuilder, as Unity is built on top of ObjectBuilder2 and was considered to be a superior choice by default.

注意:我们没有看PicoContainer。Net作为我们团队的一员,认为。Net端口与Java版本相比非常差。我们也没有查看ObjectBuilder,因为Unity构建在ObjectBuilder2之上,默认情况下被认为是一个更好的选择。

Firstly, I can say that more or less all of them are all pretty similar and it really comes down to what really works best for you, and your specific requirements. Our requirements included:

首先,我可以说,它们或多或少都是非常相似的,这真的取决于什么最适合你,以及你的具体要求。我们的要求包括:

Requirements

  • Constructor based injection (we intend not to use property, field or method injection)
  • 基于构造函数的注入(我们不打算使用属性、字段或方法注入)
  • Programmable configuration (not XML)
  • 可编程配置(而不是XML)
  • container hierarchies (one per application, per request and per session to more implicitly bind component lifetimes scope to container)
  • 容器层次结构(每个应用程序、每个请求和每个会话一个,以更隐式地将组件生存期范围绑定到容器)
  • component lifetime management (for more granular scoping eg transient / singleton)
  • 组件的生命周期管理(对于更细粒度的scoping(如瞬态/单例)
  • injection from interface to type or concrete instance (eg ILogger -> typeof(FileLogger) or ILogger -> new FileLogger() )
  • 从接口到类型或具体实例的注入(如ILogger -> typeof(FileLogger)或ILogger -> new FileLogger())
  • advanced component creation / "creaton event mechanism" for pre/post initialisation
  • 高级组件创建/“creaton事件机制”用于预初始化/后初始化
  • correct disposal of IDisposable components on container tear down
  • 正确处理集装箱拆卸上的一次性部件
  • well documented and/or online information readily available

    有很好的文件和/或在线信息

    Note: whilst performance was a requirement it was not factored in the selection as it seemed that all containers reviewed were similar according to this benchmark

    注意:虽然性能是一个要求,但是在选择中并没有考虑它,因为根据这个基准,似乎所有评审的容器都是相似的

Test

Each container was used in a typical Asp.Net webforms project (as this was our target application type). We used a single simple page with with a single simple user control, each inheriting from a base page / base control respectively. We used 1 container on the BasePage for a "per request" scope container and 1 on the global.asax for an "application" scope and attempted to chain them together so dependencies could be resolved from both containers.

每个容器都使用在一个典型的Asp中。Net webforms项目(因为这是我们的目标应用程序类型)。我们使用一个简单的页面和一个简单的用户控件,每个用户控件分别继承一个基本页面/基本控件。我们在BasePage上对“每个请求”范围容器使用了一个容器,在全局容器上使用了一个容器。asax用于“应用程序”范围,并试图将它们链接在一起,以便可以从两个容器中解析依赖性。

Each web application shared a contrived set of domain objects simulating multi-levels of dependency, scope type (singleton/transient) and also of managed and unmanaged classes (IDisposable required). The "top level" dependency components were manually injected from the methods on the BasePage.

每个web应用程序都共享一组虚构的域对象,它们模拟了多级别的依赖关系、范围类型(单例/暂态),以及托管和非托管类(即必需的)。“顶层”依赖项组件是从BasePage上的方法手动注入的。

Results

Windsor - Satisfied all the criteria and has a good case history, blogger community and online documentation. Easy to use and probably the defacto choice. Advanced component creation through Factory facility. Also allowed chaining of individually created containers.

温莎-满足所有的标准,有一个良好的案例历史,博客社区和在线文档。易于使用,可能是事实上的选择。通过工厂设备创建先进组件。还允许对单独创建的容器进行链接。

Spring.Net - Verbose and unhelpful documentation and no-obvious / easy for programmable configuration. Did not support generics. Not chosen

春天。Net -详细和无帮助的文档和不明显/易于编程的配置。不支持泛型。没有选择

Ninject - Easy to use with good clear documentation. Powerful feature set fulfilling all our requirements except container hierarchies so unfortunately was not chosen.

Ninject:易于使用,具有清晰的文档说明。强大的特性集满足我们的所有需求,除了容器层次结构,因此不幸的是没有选择。

StructureMap - Poorly documented, although had quite an advanced feature set that met all of our requirements, however there was no built-in mechanism for container hierarchies although could be hacked together using for loops see here The lambda expression fluent interface did seem a little over complicated at first, although could be encapsulated away.

StructureMap——糟糕的记录,虽然经历了一个相当先进的特性集,满足我们所有的要求,但是没有内置机制容器层次结构虽然可以通过for循环看到lambda表达式的连贯接口也似乎有点太过复杂了,虽然可以封装。

Unity - Well documented, easy to use and met all our selection criteria and has an easy extension mechanism to add the pre/post creation eventing mechanism we required. Child containers had to be created from a parent container.

统一-有良好的文档,易于使用和满足我们所有的选择标准,并有一个简单的扩展机制来添加我们需要的预/后创建事件机制。必须从父容器创建子容器。

Autofac - Well documented and relatively easy to use, although lambda expression configuration does seem a little over complicated however, again, can be easily encapsulated away. Component scoping is achieved through a "tagging" mechanism and all components are configured up front using a builder which was a little inconvenient. Child containers were created from a parent and assigned components from a "tag". Allowed generic injection.

Autofac—文档化良好,使用起来相对容易,尽管lambda表达式配置看起来确实有点复杂,但是同样,可以很容易地封装。组件确定范围是通过“标签”机制实现的,所有组件都是使用构建器预先配置的,这有点不方便。子容器由父容器创建,并从“标记”中分配组件。允许通用注入。

Conclusion

Our final choice was between Windsor and Unity, and this time around we chose Unity due to its ease of use, documentation, extension system and with it being in "production" status.

我们最后的选择是温莎和Unity之间,这次我们选择了Unity,因为它易于使用、文档、扩展系统,并且它处于“生产”状态。

#3


12  

Here is a good article which compares .NET IoC containers. http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

这里有一篇比较。net IoC容器的好文章。http://blog.ashmind.com/index.php/2008/08/19/comparing-net-di-ioc-frameworks-part-1/

#4


5  

I started using Autofac a year ago and haven't looked back since..

一年前我开始使用Autofac,从那以后我再也没有回过头来看过它。

#5


4  

I'm an Autofac fan, but both Windsor and Unity will do a good job (though Windsor is more capable than unity and doesn't require attributing your code). There's plenty of good non technical reasons for sticking to a single container in a system though.

我是Autofac的粉丝,但温莎和Unity都能做得很好(尽管温莎比Unity更有能力,也不需要为你的代码赋值)。但是,在系统中坚持使用单个容器有很多好的非技术原因。

#6


2  

Use what works. Most have features that are unique to them, and almost all are more feature-rich than Unity. If unity is all that you need, you can certainly use it.

使用作品。大多数都有自己独有的特性,几乎所有的特性都比统一丰富。如果统一是你所需要的,你当然可以使用它。

Using Microsoft's unity just because it's from Microsoft is a poor way to make a decision. Think about what you need and why, and choose the one that fits your needs.

仅仅因为来自微软而使用微软的unity是一个糟糕的决策方式。想想你需要什么,为什么需要,选择适合你需要的。

However, I second the notion of sticking to a single container if possible.

但是,如果可能的话,我支持只使用一个容器的概念。

#7


2  

I've been using the Managed Extensibility Framework and found it quite easy to work with. It's been integrated into .NET 4.

我一直在使用管理的可扩展性框架,并且发现它很容易使用。它被集成到。net 4中。

#8


1  

https://*.com/questions/71041/which-single-iocdi-container-would-you-recommend-using-and-why

https://*.com/questions/71041/which-single-iocdi-container-would-you-recommend-using-and-why

#9


0  

Unless you already have experience and a personal preferance for a particular sub-technology utilized by one of the possible IoC container solutions, they all function well and I don't see any one in particular with a "killer function" that makes it stand out from the others. Unity is probably the best fit for solutions already utilizing the P&P Enterprise Library 4.x...

除非您已经有经验,并对一种可能的IoC容器解决方案所使用的特定子技术有个人偏好,否则它们都能很好地工作,而且我没有看到任何一种具有“杀手功能”的子技术能让它脱颖而出。Unity可能是已经使用P&P企业库4.x的解决方案的最佳选择。

#10


0  

IoC Container Benchmark - Performance comparison has performance and features comparison tables for 20+ products and keep them up-to-date.

IoC容器基准——性能比较具有20多种产品的性能和特性比较表,并保持它们是最新的。

The conclusion from the article:

本文的结论是:

SimpleInjector, Hiro, Funq, Munq and Dynamo offer the best performance, they are extremely fast. Give them a try!

SimpleInjector, Hiro, Funq, Munq和Dynamo提供了最好的性能,它们非常快。给他们一个尝试!

Especially Simple Injector seems to be a good choice. It's very fast, has a good documentation and also supports advanced scenarios like interception and generic decorators.

特别简单的喷射器似乎是一个不错的选择。它非常快,有很好的文档,并且支持高级场景,如拦截和通用装饰器。

相关文章