I have three identical database servers (DDL wise) that my application connects to.
I want let application users decide which database they want to connect to.
This is a ASP.NET Core 2.1 API project, which implements both service and repository patterns (database queries are called using Dapper, but this can be changed).
I could simply put server name as a parameter in my Domain project, but I don't want this to know anything about database servers we have, neither I want my Repository to know that there could are multiple servers.
So it's just application side that can decide which database server to connect to at run-time.
I was able to find samples and suggestions for EF Core, but not much for anything else, particularly Dapper.
So my question is. How would I let users change database server at run time while keeping domain and repository unaware that multiple servers are there?
So in this case I would suggest having your connection strings either stored in environment variables or appsettings.json files. Lets say the user wants to access "server1" , this would come in from your api and you could then get out the relevant connection string in your repository layer like so
var connectionString = Environment.GetEnvironmentVariable(serverToUse);
Where serverToUse
is "server1" passed in from the api. I prefer storing my connection strings in environment variables as its stored directly on the host machine, instead of the application. You could also do this via appsettings
"Server1" : "Your connection string here"
in your appsettings.json
You could also do this in your service layer and have the service layer pass the connection string to the repository . Pick your flavor. In this case the repositories are not concerned with what connection you are using as long as its present in either environment variable or appsettings.