惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
博客园_首页
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
GbyAI
GbyAI
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
H
Help Net Security
T
Tailwind CSS Blog
B
Blog RSS Feed
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 叶小钗
雷峰网
雷峰网
量子位

博客园 - Yu

Illegal characters in path vs2015 编译报错:The project references NuGet package(s) that are missing on this computer... discuz 迁移 uc_server 报错 The timeout period elapsed prior to completion of the operation or the server is not responding. 微信小程序笔记 xmlns 与 targetNamespace 的解释 Ubuntu64 apache2+lvs+Keepalived NPOI CellStyle 设置 mysql --initialize specified but the data directory has files in it select2 多选设置默认值 VS 2015 IDE 不支持 MS SQL 2000 生成 dbml Ajax 如何执行 Response.Redirect ng 发生 Error: ELOOP: too many symbolic links encountered... jquery call cross-domain webapi owin self-host HtmlAgilityPack 使用 微信 oauth2 两次回调 EF 热加载 Winform/Asp.net vs2015 使用 Eazfuscator.NET 3.3 The requested page cannot be accessed because the related configuration data for the page is invalid.
依赖注入的 Singleton、Scoped、Transient
Yu · 2021-08-13 · via 博客园 - Yu

依赖注入可以这么理解,就是一个盒子预先注入了很多服务。
用户每次HTTP请求都会从盒子里拿相关服务(即实例),都是新的服务(Singleton的服务除外),然后当前请求进入某个controller中某个方法体内进行操作。
在这个方法体内或之间,如果再次向盒子要服务,这样就会出现三种情况:
1. Singleton的服务,每次拿都一样,无论那次请求
2. Scoped的服务,也是每次拿都一样,但只局限在本次请求中
3. Transient的服务,即每次拿都不一样

盒子的作用:

盒子获得了服务的控制权,即控制反转(Ioc),服务不再由程序主动去创建了,而是在盒子里注册。

盒子能主动使用已经注册的服务,为依赖服务的类进行实例注入,即依赖注入(DI)。