2

I am trying to build an AspNetCORE WebAPI that has various endpoints you can post to get information from a database.

The problem I am running into is trying to use dependency injection at the root layer to bind my DAL. Each request on the endpoint will carry metadata for the initial catalog. So the connection string has to be built from the request and then opened.

I have built a generic interface to work with my repos and then built a class that implements the interface, but in that implementation it still requires a data source to be newed up which at that point I don't have the DB to connect to.

So this forces me to new up a connection on every request.

If I abstract the DAL into a manager and then use DI to bind the managers, I still am missing the intial catalog detail

The SQL dbs all have the same schema, the only difference is the db name.

Is there a way to bind my DAL at the root while still being able to specify the connection string?

Trying to do this without EF, currently trying to figure out a way to handle this with Tortuga.Chain.

Tbooty
  • 269
  • 2
  • 14
  • Did you see this: https://learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6. DI is used at time of instantiation of the controller. Seems like you have the info then. The bit of code in ConfigureServices is a bit worrisome, though. – JamieMeyer Mar 23 '18 at 01:49
  • If i'm reading this correctly, you are mapping a request to a catalog, but are you also mapping a request to an implementation of your DAL, or does it all go to the same implementation? – jcwmoore Mar 23 '18 at 02:38
  • Since you build up (part of) the connection string based on request data, it means the connection string _is_ runtime data. Prevent injecting runtime data into the constructors of your components. See [this](https://stackoverflow.com/questions/45850250/dependency-injection-to-resolve-dependency-with-runtime-data) and [this](https://stackoverflow.com/questions/43801465/dependency-injection-in-compile-time) for alternatives. – Steven Mar 23 '18 at 14:45

0 Answers0