We currently have a CMS that our customers can use to manage the content and certain setting of the websites we make for them. We have one main database that contains the list of all customers. Each customer then has their own database, which contains all their data. We always connect to the main database first, then lookup the customer and use that info to connect to the database of the customer and get their data.
We are working on remaking our CMS (and it's API) in .NET 5. We use dependency injection for all our services and for the database connection. At the moment we have an IDatabaseConnection
with the implementations MySqlDatabaseConnection
and MsSqlConnection
. This has some functions for executing queries, starting transactions etc. The constructor has an IOptions<MySettings>
to inject the settings from the appSettings.json
, which contains the connection string.
This works fine for the main database, but we are trying to find out the best way to connect to the customer's database, since the connection strings for those databases come from the main database, we don't know them beforehand.
We have been thinking about creating a factory for this, but not sure how to do that in this specific case. Another thing we found is that we implement multi tenancy, but also not sure if that is the best way. We can also just add an empty constructor to our implementations and manually create instances and set the correct connection string when we need them, but that seems to be an anti-pattern? Or is it somehow possible to inject 2 instances of our IDatabaseConnection
and then set the connection string manually for one of them?
When we look for information online, we mostly come across solutions for Entity Framework or other object database mappers or by using DbContext, but as far as I know we can't use anything like that, because our CMS also has dynamic queries and things like that (I can explain more about that if you need more context).
So do you have any suggestions/recommendations for handling a use case like this? Do you need any code samples of what we have so far?