This article is a mirror article of machine translation, please click here to jump to the original article.

View: 6244|Reply: 6

[Source] ASP.NET Core (16) Dependent on injection of dynamic registration services

[Copy link]
Posted on 2023-4-13 18:32:56 | | | |
Requirements: In ASP.NET Core, we usually use AddTransient, AddScoped, and AddSingleton to register services with different life cycles according to our own business, if the project needs to register 100 services (database access layer, regular object naming rules), we need to write 100 lines of code to register, how to register these services dynamically or using reflection?

Through dynamic reflection, we only need a few lines of code to complete, and when we create a new service (because it will be scanned for new services when it is dynamically registered), we don't need to register it.

The dynamic registration service principle is to call builder. Services.AddServiceDescriptorway.


Our database access layer, interface name: IClassificationService, the object name of the implementation interface: ClassificationService, the database access layer name is followed by "Service", you can register all the database access layer services through this rule, the code is as follows:

IClassificationService interface code:

ClassificationService code:




(End)




Previous:[Actual combat]. NET/C# to get JD.com's product details pictures
Next:【Turn】C# multi-threaded concurrency under various locks
 Landlord| Posted on 2023-4-13 18:36:12 |
review

ASP.NET Core (XV) uses HttpClient to send HTTP requests
https://www.itsvse.com/thread-10311-1-1.html

ASP.NET Core (fourteen) is based on the SkiaSharp image captcha
https://www.itsvse.com/thread-10287-1-1.html

ASP.NET Core (XIII) to determine whether it is an Ajax request or not
https://www.itsvse.com/thread-10284-1-1.html

ASP.NET Core (twelve) front-end JS, CSS bundling, and compression
https://www.itsvse.com/thread-10282-1-1.html

ASP.NET Core (XI) endpoint route adds middleware to display all DI services
https://www.itsvse.com/thread-10269-1-1.html

ASP.NET Detailed explanation of Configuration priorities in Core(10).
https://www.itsvse.com/thread-10265-1-1.html

ASP.NET Detailed explanation of the Middleware middleware of Core (9).
https://www.itsvse.com/thread-9647-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
Posted on 2023-4-13 19:22:40 |
Learn to learn.
Posted on 2023-4-23 09:07:26 |
Learn
Posted on 2023-5-4 08:55:52 |
Learn to learn
Posted on 2023-5-5 11:59:19 |
1asdasdasdasd
Posted on 2023-5-16 11:21:52 |
16) Dependent Injection Dynamic Registration Service [Amended]
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com