Why would one use a third-party DI Container over the built-in ASP.NET Core DI Container?

Why would one use a third-party DI Container over the built-in ASP.NET Core DI Container?

回答1

For product development of any reasonably sized application that practice loose coupling and follows the SOLID principles, .NET Core‘s DI container is unsuited, because:

  • It doesn‘t help you in verifying your configuration, making it really hard to diagnose problems that come from common misconfigurations. In a reasonably sized application, it‘s actually quite hard to spot these mistakes yourself.
  • It is impossible to apply Cross-Cutting Concerns using interceptors or decorators in a maintainable fashion. This makes maintaining any reasonably sized application really expensive.
  • Although it supports mapping of open-generic abstractions to open-generic implementations, its implementation is rather naive and is unable to work with generic types with type constraints, more complex generic type mappings, and variance.
  • It is impossible to make conditional/contextual registrations, in such way that registrations only get injected to a certain set of consumers, while using Auto-Wiring. e.g. when having two components Service1 and Service2 that both depend on ILogger, you might want to inject Service1 with NullLogger and Service2 with FileLogger, or you want Service1 to be injected with Logger<Service1> and Service2 with Logger<Service2>.

The main reason for those limitations to exist is because it‘s the goal of the built-in container to provide DI capabilities to especially the framework itself, while keeping its feature set to a minimum in the hope that more mature DI containers would be able to integrate with it. In other words, it tries to act as an Least-Common Denominator (LCD). Because of its LCD function, it can never grow to a full-fletched DI Container that is practical for application development (not without breaking the promise of being an LCD).

If you start with a new and simple project, my advice is to apply Pure DI (which means hand-wired components inside the Composition Root without using a container) and resolve your types by plugging in your custom IControllerActivator. Later on, when features such as Auto-Registration and Interception would improve maintainability of your Composition Root, switch to one of the established DI libraries that fits your requirements.

回答2

Here it is explained :

  • Transient - A new instance is created every time
  • Scoped - A single instance is created inside the current scope. It is equivalent to Singleton in the current scope
  • Singleton - A single instance is created and it acts like a singleton
  • Instance - A specific instance is given all the time. You are responsible for its initial creation

Alpha version had this limitations :

  • It only supports constructor injection
  • It can only resolve types with one and only one public constructor
  • It doesn’t support advanced features (like per thread scope or auto discovery)

If you aren‘t writing really complicated product default DI container should be sufficient for you. In other cases you can try libraries you already mentioned that have advanced features.

My advice would be to start with the default one and change implementation when(if) you hit something you can‘t do with it.

原文地址:https://www.cnblogs.com/chucklu/p/12583846.html

时间: 2024-10-03 17:31:14

Why would one use a third-party DI Container over the built-in ASP.NET Core DI Container?的相关文章

ASP.NET Core DI 手动获取注入对象

原文:ASP.NET Core DI 手动获取注入对象 ASP.NET Core DI 一般使用构造函数注入获取对象,比如在ConfigureServices配置注入后,通过下面方式获取: private IValueService _valueService; public ValueController(IValueService valueService) { _valueService = valueService; } 那如果手动获取注入对象呢? 第一种获取方式(有时会获取不到,不推荐

ASP.NET Core DI 手动获取注入对象 (转)

ASP.NET Core DI 手动获取注入对象:https://www.cnblogs.com/xishuai/p/asp-net-core-ioc-di-get-service.html 方法1(获取Transient和Singleton注入的对象):      在 Startup 类的 Configure 方法中,获取 DI 容器,然后保留在一个静态类的静态属性中.      补充:需要注意的是,使用ServiceLocator.Instance.GetService<T>();,只能获

ASP.NET Core DI概述

众所周知,ASP.NET Core有一个DI框架,应用程序启动时初始化. 预定义依赖 1: IApplicationBuilder:提供了配置应用程序的请求管道机制 2:ILoggerFactory:次类型提供了创建记录器组件的模式 3:LHostinEnvironment:此类型提供管理应用程序运行的Web宿主环境的信息. 注册自定义依赖 为了注册类型,需要让系统知道如何将一个抽象类型解析为一个具体类型,这种映射可以是静态设定,也可以是动态的. public void ConfigureSer

【无私分享:ASP.NET CORE 项目实战(第二章)】添加EF上下文对象,添加接口、实现类以及无处不在的依赖注入(DI)

目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 上一章,我们介绍了安装和新建控制器.视图,这一章我们来创建个数据模型,并且添加接口和实现类. 添加EF上下文对象 按照我们以前的习惯,我们还是新建几个文件夹 Commons:存放帮助类 Domians:数据模型 Services:接口和实现类 我们在Domains文件夹下添加一个类库 Domain 我们新建一个类 ApplicationDbContext 继承 DbContext 1 using Microsoft.Ent

ASP.NET Core中的依赖注入(2):依赖注入(DI)

参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/project-layout.html http://www.yuanjiaocheng.net/ASPNET-CORE/projectjson.html http://www.yuanjiaocheng.net/ASPNET-CORE/core-configuration.html http://www.yuanjiaocheng.net/ASPNET-CORE/core-middleware.htm

如何在ASP.NET Core应用中实现与第三方IoC/DI框架的整合?

我们知道整个ASP.NET Core建立在以ServiceCollection/ServiceProvider为核心的DI框架上,它甚至提供了扩展点使我们可以与第三方DI框架进行整合.对此比较了解的读者朋友应该很清楚,针对第三方DI框架的整合可以通过在定义Startup类型的ConfigureServices方法返回一个ServiceProvider来实现.但是真的有这么简单吗? 一.ConfigureServices方法返回的ServiceProvider貌似没有用!? 我们可以通过一个简单的

ASP.NET Core中的ActionFilter与DI

一.简介 前几篇文章都是讲ASP.NET Core MVC中的依赖注入(DI)与扩展点的,也许大家都发现在ASP.NET CORE中所有的组件都是通过依赖注入来扩展的,而且面向一组功能就会有一组接口或抽象工厂来扩展功能,就如IControllerActivator这样的功能点在上篇文章(查看.NET Core源代码通过Autofac实现依赖注入到Controller属性)中也提到了,今天我们主要介绍一个大类似的扩展点,ASP.NET Core MVC中为我们提供了新的机制为Action Filt

ASP.NET Core开发-获取所有注入(DI)服务

获取ASP.NET Core中所有注入(DI)服务,在ASP.NET Core中加入了Dependency Injection依赖注入. 我们在Controller,或者在ASP.NET Core程序中的其他地方使用注入的服务,如logging 等. 我们要怎样获取ASP.NET Core中所有注入(DI)服务呢,下面我们来一探究竟, 也可以来看看ASP.NET Core到底注入了哪些服务. 依赖注入简单介绍: 依赖注入(Dependency injection , DI)是一种实现对象及其合作

实战Asp.Net Core:DI生命周期

原文:实战Asp.Net Core:DI生命周期 title: 实战Asp.Net Core:DI生命周期 date: 2018-11-30 21:54:52 --- 1.前言 Asp.Net Core 默认支持 DI(依赖注入) 软件设计模式,那使用 DI 的过程中,我们势必会接触到对象的生命周期,那么几种不同的对象生命周期到底是怎么样的呢?我们拿代码说话. 关于 DI 与 IOC: 个人理解:IOC(控制反转) 是目的(降低代码.服务间的耦合),而 DI 是达到该目的的一种手段(具体办法).