Asp. Net5 's Dependency Injection

Source: Internet
Author: User

Reading Asp.net5 's doc yesterday, when I saw the configuration of the Configure, it was mentioned that accessing the configuration in the controller is through dependency injection. ASP.NET5 Many of the functions are through the dependency injection to achieve, you can look at the Startup.cs, how much is the interface it! This concept I also know for a long time, how to achieve has not been clear, and in the. NET environment, there are several mature scenarios, but because it is not part of the. NET Framework, so I have never used, the piece has been a blur. That is, you want to use ASP.NET5 as your next development environment, or chew all of a sudden!

Conceptually, dependency injection solves the problem of strong coupling. Previously written code used the framework of. NET and third-party libraries, all to provide a good class, and then we are instantiating this class, call its various methods to write the program. Is that a problem? No problem, I like it. But some people do not like, do not want to "inject" a bit. So the "interface", the constructor injection, and the attribute injection are produced.

Let's take a look at how to write code based on ASP.NET5 dependency injection, and the injection of other frameworks should be different, regardless of it.

First, in the Startup.cs,

public void Configureservices (iservicecollection services) {//based on ASP. NET documents, this adds a single object.            The entire project can be used. Services.        Addsingleton (_ = = Configuration); }

Where services come from, are run-time incoming,

Configureation from where it is (new Configurationbuilder ()). Build () An object out of. Contains configuration information for the project, such as author,conntectstring, so that we can access it from somewhere else.

Let's go to the controller and add the following code even if the injection succeeds. We can use _config to access configuration information at any time.

Private ReadOnly iconfiguration _config;        Public HomeController (iconfiguration config) {_config = config; }

At this time should go to ASP.NET5 official document check, but so far, the dependency injection chapter marked as unfinished, that is, no one wrote it.

Then go to GitHub to find https://github.com/aspnet/DependencyInjection, which says the purpose of this package is:

"Contains the common DI abstractions that ASP. 5 and EF 7 use, as well as adapters for some IoC containers".

Download the source code first! Copy the project address, and then go to vs2015 and clone it.

Go back to Asp.net5 's project template and look at these two tips

Indeed, the Iservicecollection namespace is the name of the dependency injection, and the Addsingleton method is just an extension of that interface.

Right-click on the Addsingleton method to view meta information. All the information is right.

So this solves me a big doubt: I always thought iservicecollection is a lot of services offered by ASP.NET5, such as Ef,mvc,route.

If you want to use MVC, you can add it to the service. Who the fuck can think, this service means to inject service. And this interface is for injection.

This is the clone of the source code, contains 3 items, you can find in the above interfaces and classes, can be found.

As a result, iservicecollection is an interface that relies on injection.


I carefully read the source code, and then continue to analyze it!












Asp. Net5 's Dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.