I have for the first time started using services and dependency injection in my c# project.
I have the following Interface and class:
public interface IUserService
{
...
}
public class UserService : IUserService
{
...
}
so far nothing confusing.
but then I take advantage of DI as follows:
public class UsersController : ControllerBase
{
private IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
}
This works as expected. However I am struggling to understand how the compiler would know the the userService parameter passed to the UsersController constructor should be an instance of the UserService class, as the type for the parameter is IUserService. Adding furthermore to my confusion is the fact that I can make use of _userService as though it is an instance of UserClass even though once again it is declared as IUserService.
Does it have anything to do with the line below in my Program.cs file?
services.AddScoped<IUserService, UserService>();
I’m hoping for a simple explanation that isn’t too technical as I am still familiarizing myself with some of the very basic concepts of the language and .NET framework.
3
Answers
Yes exactly.
This line tells the system to provide an instance of
UserService
whenever an implementation forIUserService
is requested. So, during runtime, an instance of UserService will be passed in to the constructor and you can call properties and methods on UserService with this supplied instance.And
AddScoped
indicates the scope of the implementation. Scoped lifetime services are created once per (http) request. We haveAddTransient
andAddSingleton
as other optionsThe interface
IUserService
declares members (methods, properties, events) any implementation must provide. Therefore, it does not matter which class is passed toUsersController
as long as it implements this interface. Any class implementingIUserService
will work. The classUserService
is said to be assignment compatible toIUserService
.The compiler does not know that
UsersController
must be initialized with aUserService
. The Dependency Injection Container is doing this and it is indeed the lineservices.AddScoped<IUserService, UserService>();
which tells it to do so.The point of dependency injection is to make the consuming code independent of a concrete implementation. In times of procedural programming every piece of code tended to depend on every other one, making it extremely hard to find an eliminate bugs and to evolve the code. Dependency injection, on the other hand, leads to a loose coupling between the classes.
See also:
Yes!, ‘services’ in Program.cs is IServiceCollection wich behaves like a service registry in wich you register all your services and their interfaces respectively.
I’m not really sure about this but I guess the dotnet compiler will recognize the pattern
search it if it exists in IServiceCollection and then it’ll instantiate it as instructed.
Another very important thing You need to know are methods of instantiateing your services. These are AddSingleton(), AddTransient and AddScoped()
more about those you can find on this post
https://stackoverflow.com/a/38138494/5735035