Home > OS >  Need help understanding Interfaces and Dependency Injection
Need help understanding Interfaces and Dependency Injection

Time:09-19

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.

CodePudding user response:

The interface IUserService declares members (methods, properties, events) any implementation must provide. Therefore it does not matter which class is passed to UsersController as long as it implements this interface. Any class implementing IUserService will work. The class UserService is said to be assignment compatible to IUserService.

The compiler does not know that UsersController must be initialized with a UserService. The Dependency Injection Container is doing this and it is indeed the line services.AddScoped<IUserService, UserService>(); which tells it to do so.

See also: Dependency injection in ASP.NET Core (Microsoft Learn)

CodePudding user response:

Does it have anything to do with the line below in my Program.cs file?

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 am struggling to understand how the compiler would know the the userService parameter passed to the UsersController constructor

I'm not really sure about this but I guess the dotnet compiler will recognize the pattern

private IUserService _userService;
public UsersController(IUserService userService)
{
   _userService = userService;
}

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

CodePudding user response:

Yes exactly.

This line tells the system to use provide an instance of UserService whenever an implementation for IUserService 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 implemetation. Scoped lifetime services are created once per (http) request. We have AddTransient and AddSingleton as other options

  • Related