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:
- Singleton-always returns the same instance
- Transient-a new instance is returned each time.
- Scoped-returns the same instance within the current (request) Range
Suppose we have two artifacts to work through dependency injection:
- PageContext-custom request Context
- 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.