This section covers dependency Injection in ASP.NET Core.
- [Instructor] Now while dependency injection is not a new concept, and certainly not new for MBC, what is new is the fact that it is completely built in to the framework. So you don't have to bring in Ninject or Autofac or some other framework, although you can use custom frameworks as well. Items are added to the services container in the Startup.cs, and then you can access them through constructor injection, method injection with the FromServices attribute or through view injection with the @inject razor command, and you can also retrieve services through the ApplicationServices object as well as the RequestServices method off of the HttpContext.
However, inject is the preferred mechanism if you can make that work. You can also register custom services, and there's four main ways that you can register them. Transient will create a new instance every time an object is asked for. Scoped is created once per request, and that's HTTP request. Singleton will have a max of one instance per application, and instance is also a singleton, but it's not created until the first instance is called for.
When registering a custom class as a singleton, it's important to note that you do not have to implement the singleton design pattern. The service container will take care of that for you. So let's look at this in the code. So here I am in the startup class in the configure services method, and you see on line 51 we're adding the application DB context into the services container. So anytime you need an instance of the EF context all you have to do is put application DB context as the type in the constructor or method and it'll be injected in.
Now it's an interface as part of what you're used to with dependency inject, but the DB context is handled very well by the services container. Here we're adding identity. That's all done for us by the template. In 1.0 and 1.1 I always add in a singleton for the configuration and that's a configuration root, which allows me to inject the configuration class into my controllers.
We'll see more about configuration later on in this chapter. I'm adding my own on line 67 and 68. Here's where I am creating a category repo or a product repo based on those interfaces for use in my application. And then lastly I am creating an instance of a custom settings class that can then be injected in through the configuration process, which we will also discuss later in this chapter.
If we look at the layout we see an example of injecting into a view with the add inject, and if we go into the ProductController, go up to the constructor, I'm injecting a productRepo into the class, and then I'm using method injection on the ProductList cause I only need it for this particular action method where I'm injecting in a categoryRepo.
The other way to access something in a container is through the application services, and here on lines 85 and 86 I am passing in an instance of the ApplicationDbContext into my initializer. I did this for demonstration purposes. I could've just added the ApplicationDbContext in the constructor and the framework would've loaded it, but I wanted to make sure you saw how to use that method.
- Running and debugging ASP.NET Core applications
- Pros and cons of migrating existing applications to ASP.NET Core.
- Built-in dependency injection
- Environment awareness and app configuration
- Web host configuration and SSL
- View components invoked as tag helpers
- Configuration and logging
- Using Razor Pages