当前位置:网站首页>The most easy to understand dependency injection and control inversion
The most easy to understand dependency injection and control inversion
2022-04-23 03:06:00 【Dotnet cross platform】
This is one about ASP.NET 6 Dependency injection series .
In this series , We'll learn what dependency injection is 、 Inversion of control , What it can do , And why we use it .
after , We will learn more about ASP.NET 6 The life cycle of dependency injection 、 Important concepts such as service container .
Last , Let's further understand the service scope and more service registration and injection methods .
summary
Dependency injection is ASP.NET The core of the framework .
It mainly provides two core functions :
「 Registration type 」, These types registered to the dependency injection system , stay .NET There is a tall name in the —— service .
Because these types are designed to provide some kind of service to the system , So type registration is also called service registration .
「 Resolving instances 」, service / After the type is registered , It can also be injected into other services in the dependency injection system / Type used .
Such as type A Dependency type B, type A You can get the type through dependency injection B Example .
Want a thorough understanding of dependency injection , We need to know something about inversion of control .
Inversion of control
There is something called a container in dependency injection , Also known as IoC(Inversion of Control - Inversion of control ) Containers .
This container holds all instances created by the dependency injection system , So why is it called IoC And the container ?
「 Inversion of control (IoC) It's a reverse flow 、 How dependencies and interfaces work , It gives the calling right of the object that is traditionally directly controlled by the program code to the third party , The assembly and management of object components are realized through the third party .」
Suppose we have such a scenario , A four 、 Five year old children , Want to eat .
If he finds something to eat from the fridge , May cause some bad results , For example, you may get food that you can't eat directly , Or forget to turn off the refrigerator door and other lights .
You can think about it , A four 、 A five-year-old , Go to the fridge and get your own food , Even to process their own ingredients , Do you think this is not normal ?
So what is the normal situation ?
As a four 、 A five-year-old , Just ask your parents to state your needs , such as " i am hungry 、 I would like to eat XX", This is the time , Parents will prepare the food they need , Then give it to him .
Take the initiative to get it from the fridge ( Positive rotation ) food , Passively get what you want to eat ( reverse ), This is actually a process of transferring object call rights .
The former is to take the initiative to obtain food , The latter is by parents , That is, the third party gives him the food , And he doesn't care where the food comes from , And how to get .
So-called “ Inversion of control ” The concept of 「 Transfer of control over component objects , From the program code itself to a third party outside the code .」
In layman's terms , That is, the user code of the application is not responsible for the creation and maintenance of dependent objects , Instead, it is left to a third party , In this way, control is transferred from the application to a third party , That is, control realizes the so-called reversal .
Such as type A Need to use type B Example , type B Instance creation is not controlled by the type A Take care of yourself , But through a third party .
So what does it have to do with dependency injection ?
Dependency injection
「 Dependency injection is IoC An implementation of , Used to reverse dependencies .」
In the previous scene , A four 、 A five-year-old , Want to eat .
He just needs to state his needs , Parents will prepare the food they need ( rely on ), Then give it to him ( Inject ).
Dependency injection is not the purpose , It's just a way to reverse control .
The purpose of using dependency injection , To manage complex dependencies in applications , It can help us develop loose coupling 、 Maintainable 、 Testable code and programs .
Now? , Let's review the explanation of inversion of control .
A reverse flow 、 How dependencies and interfaces work , It gives the calling right of the object that is traditionally directly controlled by the program code to the third party , The assembly and management of object components are realized through the third party .
The third party in this description is the control system in dependency injection .
IoC Containers
In the scene of children eating , One side plays IoC The role of the container , You analyze who it is ?
I think the first instinct of many people is “ Parents ”?
Actually , Loosely speaking , The refrigerator is playing IoC The role of the container , Parents play the role of control system in dependency injection .
IoC A container is something in dependency injection that holds the created instance , If the instance has been created , And has an effective life cycle , Then the control system will directly from IoC Container get instance , Otherwise, the instance will be created .
What's in the fridge , They were bought and put in by their parents , They also need to take it out of the fridge .
「 The so-called dependency injection system , In essence, it is an implementation of type registration 、 Components of instance resolution and instance lifecycle management .」
About the life cycle , We will discuss in detail in the next article in this series .
If you know the factory model , Then you can simply think of the dependency injection system as a super factory .
stay .NET Dependency injection system , All registered types , Are called services .
ASP.NET Components in , Most of them need to obtain the service instances it depends on through dependency injection .
So the whole ASP.NET frame , Are built on the dependency injection system , It's also development ASP.NET The most basic programming mode of application .
More highlights , Please pay attention to me. ▼▼
If you like my article , that
Watching and forwarding is my greatest support !
( Stamp the blue words below to read )
ASP.NET 6 Middleware family
Recommends WeChat official account : Code Xia Jianghu
I think it's good , Point and watch before you go
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333836.html
边栏推荐
- Blazor University (12)组件 — 组件生命周期
- TP5 customization in extend directory succeeded and failed. Return information
- REINFORCE
- Traversée de l'arbre L2 - 006
- TP5 multi conditional where query (using PHP variables)
- ASP.NET和ASP.NETCore多环境配置对比
- [software testing] understand the basic knowledge of software testing
- Detailed explanation of distributed things
- Distributed system services
- 再战leetcode (290.单词规律)
猜你喜欢
Traversée de l'arbre L2 - 006
Laravel's own paging query
Judge whether there is a leap year in the given year
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
ASP.NET和ASP.NETCore多环境配置对比
TP5 email (2020-05-27)
Depth deterministic strategy gradient (ddpg)
tf. keras. layers. Timedistributed function
MAUI初体验:爽
BLDC double closed loop (speed PI + current PI) Simulink simulation model
随机推荐
最通俗易懂的依赖注入之生命周期
Tips in MATLAB
Opencv fills the rectangle with a transparent color
The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details
宁德时代地位不保?
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
Using stack to solve the problem of "mini parser"
TP5 multi conditional where query (using PHP variables)
交换二叉树中每个结点的左和右
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
JS relearning
使用split来解决“最常见的单词”问题
在.NE6 WebApi中使用分布式缓存Redis
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
.Net Core 限流控制-AspNetCoreRateLimit
Plug in for vscode
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
tf. keras. layers. Inputlayer function
How to write the expected salary on your resume to double your salary during the interview?