I have to make a project with a N-tier architecture using a repository pattern without entity framework (School assignment). I want to be able to get configuration settings from a configuration file independent from the UI so I can get the right connectionstring to use as it has to support multiple database types.
I want to use a WPF UI and an ASP.NET website on my n-tier architecture and read a configuration file in the exact same way
In a WPF project it uses App.config, but in ASP.NET Core 2.0 it uses Appsettings.json.
This is my App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<appSettings>
<!-- Context has to be set to use the right database-->
<add key="Context" value="MSSQL"/>
</appSettings>
<connectionStrings>
<add name="MSSQL" connectionString="my connectionstring here"/>
<add name="ORACLE" connectionString="my connectionstring here"/>
</connectionStrings>
</configuration>
Here is appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"Context": {
"ContextType": "MSSQL"
},
"ConnectionStrings": {
"MSSQL": "connectionstring",
"ORACLE": "connectionstring",
"MYSQL": "connectionstring"
}
}
I'm getting the context and the connectionstrings from app.config like this:
Context = ConfigurationManager.AppSettings["Context"];
ConnString = ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["Context"]].ConnectionString;
I'm aware that ASP.NET Core 2.0 doesn't have the ConfigurationManager class. But isn't there a way to get the configuration settings from both UI's (WPF and ASP.NET) in the exact same way? Ideally I'd prefer a similar approach as ConfigurationManager
Would appreciate it if anyone could point me in the right direction on how to approach this.