Dependency injection questions are very often during a job interview. In the best case, you should be able to answer why do we use DI container, what is Inversion of Control, and to be familiar with some concrete DI container.

Before .NET Core, we were used to working with some third-party libraries. I preferred Autofac (and I still use it for many none .NET Core projects). With the .NET Core release, the framework came with an integrated DI container.

One of the most asked questions during .NET Core job interview is “What are the scopes of DI container in .NET Core?”.

They are just three and they are not limited only to .NET Core. I mean that similar scopes you will find in almost any DI container. They are:

  • Transient
  • Scoped
  • Singleton

Services registered as transient are created each time an object is requested. The scope of this kind of service is limited to the user’s request. For example, if in a request/connection you have multiple requests for some service, each time in that request/connection a new service object will be created.

Scoped registered services are created only ones per user’s request.  No matter how many requests you do for a service from the DI container, each time you are going to receive the same instance of the service.

Singleton registered service is created the first time it is requested from the DI container. Singleton registered service is not limited to the current request/connection. Services created as a singleton are available to all subsequent requests.

  Best practices: Initialize collections

Write A Comment