0

I am creating simple WebAPI and I would like to use 3 layer architecture

DAL -> Services -> API

DAL - DB context, entityes

Services - repositories, services and bussiness logic

API - API.Net Core 2 WebAPI

my question is how should I register DB Context into my Dependency Injector?

I have this line of code in startup.cs

var connectionString = Configuration["connectionStrings:libraryDBConnectionString"];
services.AddDbContext<LibraryContext>(o => o.UseSqlServer(connectionString));

but LibraryContext is placed in DAL project so I have to reference DAL project into my API project.

Is it correct implementation or should I use interface project with ILibraryContext?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Podlipny
  • 75
  • 3
  • 9
  • 1
    Classic DI related question. All projects should be bind in the composition root of the entry project i.e. the api. You should reference all project to the web project and bind them there. Here's a answer which covers why https://stackoverflow.com/questions/40401900/bootstrapping-unity-composition-root-location/40403875#40403875 – Marcus Höglund Mar 12 '18 at 19:25
  • See [Ioc/DI - Why do I have to reference all layers/assemblies in entry application?](https://stackoverflow.com/questions/9501604/ioc-di-why-do-i-have-to-reference-all-layers-assemblies-in-entry-application) – NightOwl888 Mar 12 '18 at 19:25

0 Answers0