关注开源代码的实际应用
最近有点空闲时间和学习的动力了,决定学习一下.NET控制反转框架。目前网上还没见过有较为完整的框架特性比较,我来开个头吧!
我选择以下6个相对流行且文档较为完整的框架进行比较:Castle、Unity、Ninject、Autofac、StructureMap、Spring.Net。如果你认为有其它框架也应当一起比较,留言给我,我会加上的。
| 框架 | 版本 | 授权 | 最少dll引用 | 大小(KB) |
|---|---|---|---|---|
| Castle | RC3 | Apache 2 | 2 | 192 |
| Unity | 1.1 | MS-PL | 2 | 120 |
| Ninject | 1.0 | Apache 2 | 1 (2 : autowiring) |
147 (154) |
| Autofac | 1.2.7 | MIT | 1 | 110 |
| StructureMap | 2.4.9.0 | Apache 2 | 1 | 164 |
| Spring.Net | 1.1.2 | Apache 2 | 2-3 | 728 |
| 框架 | Fluent registration | 自动注册 | 使用Attribute | 使用XML |
|---|---|---|---|---|
| Castle | in trunk only | 有限, 将被废除 | 支持, 非必须 | 支持, 非必须 |
| Unity | yes | 不支持 | 支持, 非必须 | 支持, 非必须 |
| Ninject | yes | 支持 | 支持, 非必须 | 不支持, 可实现 |
| Autofac | yes | 在网站上以范例提供 | 不支持, 不需要 | 支持, 非必须 |
| StructureMap | yes | 支持 | 支持, 非必须 | 支持, 非必须 |
| Spring.Net | method chaining only | 不支持 | 不包含, 可实现, 下个版本支持 | 支持, 非必须, 推荐 |
| 框架 | 属性注入 | 构造器注入 | 多个构造器 | 循环依赖 |
|---|---|---|---|---|
| Castle | 支持 | yes | most resolvable parameters | meaningful error |
| Unity | 需要代码,特性或XML, mandatory dependency | yes | hardcoded, attributed or most parameters | stack overflow |
| Ninject | 需要Attribute, automatic with official extension | yes | attributed or default, most resolvable parameters with official extension | stack overflow |
| Autofac | yes (opt-in) | yes | most resolvable parameters | meaningful error |
| StructureMap | 需要Attribute或XML, mandatory dependency | yes | attributed or most parameters | stack overflow |
| Spring.Net | yes (opt-in) | yes | hardcoded signature, default, if no default then most resolvable parameters | stack overflow |
框架
单例
短暂的
其它/自定义
自定义实例
Castle
yes
yes
yes, pluggable
支持
Unity
yes
yes
yes, pluggable
支持
Ninject
yes
yes
yes, pluggable
支持
Autofac
yes
yes
yes, pluggable
支持
StructureMap
yes
yes
yes, pluggable
支持, 支持prototypes (cloning)
Spring.Net
yes
yes
yes, pluggable
supported
| 框架 | Open generics injection | List injection | Unregistered resolution |
|---|---|---|---|
| Castle | supported | not included, implementable (but hard to implement correctly) | not supported, no good workarounds |
| Unity | supported | not included, probably implementable | yes |
| Ninject | supported | not included, probably implementable | yes |
| Autofac | supported, requires explicit registration | not included, probably implementable | requires special opt-in |
| StructureMap | supported | works at least for arrays, haven't found documentation anywhere | yes |
| Spring.Net | not supported, no easy workaround | documented, not working in current version (bug) | either not supported or not easy |
| 框架 | Auto-mocking | 特殊功能 | 特殊问题 |
|---|---|---|---|
| Castle | not included, implementable | rare releases, some major features trunk-only for some time | |
| Unity | not included, implementable | no | |
| Ninject | not included, probably implementable | contextual bindings | problems referencing under .NET 3.5 (ExtensionAttribute defined twice) |
| Autofac | in Contrib, linked from home page (NMock2) | container hierarchies/tagging, expression constructors | no |
| StructureMap | included (NMock) | documentation/samples a bit obscuring the existence of non-static API | |
| Spring.Net | not included, probably implementable | complex registration API, XML (verbose) encouraged |
(未完成)