0

I am trying to create a multi-layer .NET Core API project - API project, Application Layer, Domain Layer, Data Access Layer and an IoC layer where all dependencies are resolved and finally, it is called in ConfigureServices() method of Startup.cs class.

In IoC project, a reference to data access layer and domain layer are added and dependencies are resolved as below:

 namespace ToDo.Infrastructure.IoC
{
    public class DependencyContainer
    {
        public static void RegisterDependencies(IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext<ToDoDbContext>(op => op.UseSqlServer(configuration.GetConnectionString("dbConnection")));
            services.AddScoped<IUserRepository, UserRepository>();
        }
    }
}

In Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            DependencyContainer.RegisterDependencies(services, Configuration);
        }

It works well but the problem is that now the API project has access to all the public classes of Data Access Layer which I do not want. Is there any other way to resolve the dependencies such that the API project does not have access to data access layer/domain layer?

  • Duplicate of https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-applications-entry? – Steven Feb 14 '21 at 15:24
  • Also see: https://freecontent.manning.com/dependency-injection-in-net-2nd-edition-understanding-the-composition-root/ – Steven Feb 14 '21 at 15:26

0 Answers0