concept DI container in category asp.net

appears as: DI container, DI containers, The DI container
ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

It’s common to refer to services when talking about DI containers, which is slightly unfortunate as it’s one of the most overloaded terms in software engineering! In this context, a service refers to any class or interface that the DI container creates when required.

The beauty of this approach is that by using explicit dependencies, you never have to write the mess of code you saw in listing 10.3. The DI container can inspect your service’s constructor and work out how to write the majority of the code itself. DI containers are always configurable, so if you want to describe how to manually create an instance of a service you can, but by default you shouldn’t need to.

Listing 10.6. Registering the MVC services with the DI container
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); #1 } #1 The AddMvc extension method adds all necessary services to the IServiceCollection. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":33},{\"line\":2,\"ch\":35}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"} !@%STYLE%@!

It’s as simple as that. Under the hood, this call is registering multiple components with the DI container, using the same APIs you’ll see shortly for registering your own services.

Most nontrivial libraries that you add to your application will have services that you need to add to the DI container. By convention, each library that has necessary services should expose an Add*() extension method that you can call in ConfigureServices.

Figure 10.17. In ASP.NET Core 2.0, the DI container will throw an exception when it creates a service with a captured dependency. By default, this check is only enabled for development environments.