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.