AutoFac 比 Microsoft.Extensions.DependencyInjection 好在哪里?
How is AutoFac better than Microsoft.Extensions.DependencyInjection?
AutoFac 比 Microsoft.Extensions.DependencyInjection 好在哪里? Autofac 支持 属性 和方法注入(但是我真的不明白 属性 和方法注入在哪里可能有用)。
我的意思是当我可以通过 Microsoft.Extensions.DependencyInjection
做同样的事情时,我为什么要使用 AutoFac
这是一个公平的问题;从根本上讲,区别在于 Autofac 可以在您需要时为您提供的附加功能。对于简单的应用程序,Microsoft DI 可能提供足够的功能,但我发现随着我的应用程序的增长,我发现自己有一些额外的功能 wanting/needing。
在我看来鼓励我最初开始使用 Aufofac 的功能是:
- 标记的生命周期范围,以及这些标记的范围服务(https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html#instance-per-matching-lifetime-scope)
- 正在解析具有一些关联元数据的服务(https://autofaccn.readthedocs.io/en/latest/advanced/metadata.html)
- 定义服务的 Named/Keyed 个变体 (https://autofaccn.readthedocs.io/en/latest/advanced/keyed-services.html)
- 解析一个你可以随时使用的工厂函数(https://autofaccn.readthedocs.io/en/latest/resolve/relationships.html#dynamic-instantiation-func-b)
- 惰性实例化(https://autofaccn.readthedocs.io/en/latest/resolve/relationships.html#delayed-instantiation-lazy-b)
- 在闲暇时将所有这些结合起来的能力(例如
Meta<Lazy<IMyService>>
)
还有很多,但这些是我最喜欢的一些功能。检查文档以了解所有优点。
此外,您没有理由不开始使用 built-in DI,然后在您意识到需要时添加 Autofac。 Autofac ASP.NET 核心集成 (https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html) 只会获取您所有现有的注册,然后您可以在上面添加这些额外的功能。
为了在这里进行全面披露,我要指出我是 Autofac 的维护者之一。
AutoFac 比 Microsoft.Extensions.DependencyInjection 好在哪里? Autofac 支持 属性 和方法注入(但是我真的不明白 属性 和方法注入在哪里可能有用)。
我的意思是当我可以通过 Microsoft.Extensions.DependencyInjection
做同样的事情时,我为什么要使用 AutoFac这是一个公平的问题;从根本上讲,区别在于 Autofac 可以在您需要时为您提供的附加功能。对于简单的应用程序,Microsoft DI 可能提供足够的功能,但我发现随着我的应用程序的增长,我发现自己有一些额外的功能 wanting/needing。
在我看来鼓励我最初开始使用 Aufofac 的功能是:
- 标记的生命周期范围,以及这些标记的范围服务(https://autofaccn.readthedocs.io/en/latest/lifetime/instance-scope.html#instance-per-matching-lifetime-scope)
- 正在解析具有一些关联元数据的服务(https://autofaccn.readthedocs.io/en/latest/advanced/metadata.html)
- 定义服务的 Named/Keyed 个变体 (https://autofaccn.readthedocs.io/en/latest/advanced/keyed-services.html)
- 解析一个你可以随时使用的工厂函数(https://autofaccn.readthedocs.io/en/latest/resolve/relationships.html#dynamic-instantiation-func-b)
- 惰性实例化(https://autofaccn.readthedocs.io/en/latest/resolve/relationships.html#delayed-instantiation-lazy-b)
- 在闲暇时将所有这些结合起来的能力(例如
Meta<Lazy<IMyService>>
)
还有很多,但这些是我最喜欢的一些功能。检查文档以了解所有优点。
此外,您没有理由不开始使用 built-in DI,然后在您意识到需要时添加 Autofac。 Autofac ASP.NET 核心集成 (https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html) 只会获取您所有现有的注册,然后您可以在上面添加这些额外的功能。
为了在这里进行全面披露,我要指出我是 Autofac 的维护者之一。