The most common issue we have is getting a connection string from our appsettings.json or webconfig file into a class that we will use to call the database. This is even more difficult when we start getting lots of integration systems like Amazon S3 or Google Storage. No matter what, we don’t want those secure keys in our source control, so we put the credentials into Appsettings.json. But, then you need to make sure to keep them all out of source control and make sure every damn developer you ever hire only has access to certain datasets, etc… it gets to be quite the chore.
So, for me, I keep a settings table in the database and the database connection string on the server’s appsetting. But, for usage, I keep all of that in a singleton class.
During the program’s start I pull the connection string from appsettings.json, then initialize a class I wrote called “AppSettings”, like this appset = new AppSettings(connectionString); The class itself will then pull all the setting information out of the database and populate the class properties as appropriate. Then, I register that class as a singleton.
At this point, I can inject that singleton class into any controller and all my settings will be available with no reading of files or additional calls to the database.
Moreover, I can actually SET the values dynamically if I choose. So, I can open the program and get to the part where I’d store a google file and it would say, “oh. you don’t have a credentials for that. Type them in here in this nice form”, which will populate the database table AND set the values in the AppSettings singleton class in real time without rebooting the program.
This is great, except sometimes you just want to “get the data”. So, suppose you had a class called “MyAmazonS3” and you have your credentials in AppSettings.AWS. I would set it up like this: new MyAmazonS3(AppSettings.AWS) and then register THAT as a singleton as well. So you can use the same class with the credentials filled out wherever you’d like simply by injecting it into the controller.
Whether you choose to make your classes singletons or simply make new ones and pass in the credentials, I think this is better than keeping all your credentials in a text file.