Detailed introduction of framework-level dependency injection in ASP. NET Core, asp. netcore

Source: Internet
Author: User

Detailed introduction of framework-level dependency injection in ASP. NET Core, asp. netcore

1. Dependency injection in ASP. NET Core

This example shows how frame-level dependency injection works in ASP. NET Core. It is simple but powerful enough to complete most of the dependency injection work. Framework-level dependency injection supports the following scope:

  1. Singleton-always returns the same instance
  2. Transient-a new instance is returned each time.
  3. Scoped-returns the same instance within the current (request) Range

Suppose we have two artifacts to work through dependency injection:

  1. PageContext-custom request Context
  2. Settings-global application Settings

Both are very simple classes. The PageContext class provides the title label of the current page title for the layout page.

public class Settings { public string SiteName; public string ConnectionString;}public class PageContext{  private readonly Settings _settings;  public PageContext(Settings settings)  {    _settings = settings;  }  public string PageTitle;  public string FullTitle  {    get    {      var title = (PageTitle ?? "").Trim();       if(!string.IsNullOrWhiteSpace(title) &&        !string.IsNullOrWhiteSpace(_settings.SiteName))      {        title += " | ";      }      title += _settings.SiteName.Trim();      return title;    }  }}

2. Register Dependencies

Before using these classes in the UI build block, you must register these classes when the application starts. This can be done in the ConfigureServices () method of the Startup class.

public void ConfigureServices(IServiceCollection services){  services.AddMvc();  var settings = new Settings();  settings.SiteName = Configuration["SiteName"];  services.AddSingleton(settings);  services.AddScoped<PageContext>();}

These classes can now be injected into controllers and other UI components that support dependency injection.

3. Inject instances into the Controller

We use the PageContext class in the Home controller to assign the page title.

public class HomeController : Controller{  private readonly PageContext _pageContext;  public HomeController(PageContext pageContext)  {    _pageContext = pageContext;  }  public IActionResult Index()  {    _pageContext.PageTitle = "";    return View();  }  public IActionResult About()  {    _pageContext.PageTitle = "About";    return View();  }  public IActionResult Error()  {    _pageContext.PageTitle = "Error";     return View();  }}

This method is good for assigning page titles because ViewData is not required, which is more easily supported by multi-language applications.

4. Inject instances into the view

Now the page title is assigned to the Controller action. It is time to use the title in the layout page. I added a title in the content area of the page, so it is also easy to see in the tech. io environment. In order to use PageContext on the layout page, I used view injection (the first line in the following code snippet ).

@inject PageContext pageContext<!DOCTYPE html>

5. References

Dependency injection in ASP. NET 5 (Gunnar Peipman)
ASP. NET Core: Use view injection (Gunnar Peipman)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.