0

I have a doubt regarding the correct DI pattern to be used in order to build a decoupled application.

My project structure is:

enter image description here

I want to inject a implementation of the UserManager class, and to achieve this I've added the dependency on Startup.cs of my web app using this statement:

services.AddScoped<IUserManager, UserManager>();

For this, I have to add a project reference for BestBurgerManager.Business project and also use its implementation.

Is that correct from the DI point of view? It seems that it won't has a decoupled design doing this way.

The implementation of my UserManager class is:

    public class UserManager : IUserManager
{
    private readonly IUserManager _userManager;
    public UserManager(IUserManager userManager)
    {
        _userManager = userManager;
    }

    public void AddUser(User user)
    {
        // Adds a user.
    }

    public User GetUser(int userId)
    {
        return new User(); // Just to test DI.
    }
}

Any help to clarify this is appreciated.

Fabiano
  • 5,001
  • 2
  • 28
  • 31
  • 1
    The composition root needs to be aware of all the components in order to be able to be able to map abstractions to their implementations. Your startup in this case is the composition root. – Nkosi May 14 '19 at 16:27
  • So in this case it is correct to add the project reference to the web API, right? – Fabiano May 14 '19 at 16:34
  • 1
    Yes. Based on most documentations around asp.net core and clean architecture. – Nkosi May 14 '19 at 16:36
  • 1
    Related: https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application – Steven May 14 '19 at 20:44
  • "Correct" is a relative term. Based on your setup here, yes, you would have to take a dependency on your business layer in your API. Whether or not that is "correct" from a requirements perspective is an entirely different question we can't answer for you. – Chris Pratt May 14 '19 at 20:51
  • Thanks for the answer, and I agree that there may be more things if we look to the requirements. But for this case, I just want to not hurt the DI principles... that is my point. If I am not violating any DI principle, then I can consider it correct. :) – Fabiano May 14 '19 at 21:13

0 Answers0