Writing repetitive code to manage, read and write configuration files for every project is tedious. Let Miqo.Config take care of the heavy lifting of managing configuration files for you, so you can focus on your project.
Miqo.Config is a .NET Standard 2.0 library that helps translate your strongly typed object to a JSON configuration file.
The library is available as a signed NuGet package.
PM> Install-Package Miqo.Config
Start by creating a class to hold your configurations.
public class Configuration {
public string Server { get; set; }
public int Port { get; set; }
public List<string> IndexFiles { get; set; }
}
Reading the application settings from a JSON file is done in the following way:
using Miqo.Config;
var config = new MiqoConfig()
.Load()
.ApplicationSettings()
.FromFile<Configuration>("Spiffy.json");
Console.Writeline(config.Server);
Miqo.Config can also load a configuration from a JSON based string:
using Miqo.Config;
var string json = "{ \"Server\": \"localhost\" }";
var config = new MiqoConfig()
.Load()
.ApplicationSettings()
.FromString<Configuration>(json);
Console.Writeline(config.Server);
Application wide configurations are stored in the same directory as the application. A custom location can be specified using ApplicationSettings(string directory)
.
using Miqo.Config;
var config = new Configuration {
Server = "localhost",
Port = 8080,
IndexFiles = new List<string> {"index.html", "index.htm", "index.php"}
};
new MiqoConfig()
.Save(config)
.ApplicationSettings()
.ToFile("Spiffy.json");
The following file will be created in the application's folder:
{
"server": "localhost",
"port": 8080,
"indexFiles": [
"index.html",
"index.htm",
"index.php"
]
}
You can have application and user specific settings that are unrelated to each other. For instance, you can save the main window's position and size in it's own configuration file.
new MiqoConfig()
.Save(config)
.UserSettings("SpiffyApp")
.ToFile("Spiffy.json");
Use UserSettings(string appName)
instead of ApplicationSettings()
to save the configuration to the currently logged in user's ApplicationData folder. You can specify a subfolder for your particular application's data.
Miqo.Config has some other nifty features that may be useful to you as a developer.
You can add logging capabilities to Miqo.Config. Serilog can be added as such:
var logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
var json = new MiqoConfig(logger)
.Save(config)
.ApplicationSettings()
.ToString();
If you are storing usernames, password, connection strings, API keys or any other such sensitive data, you should consider encrypting the property. Add the [JsonConverter(typeof(EncryptedPropertyConverter), key)]
attribute to the property.
Example:
[JsonConverter(typeof(EncryptedPropertyConverter), "cfVMjtOJ8/eJx0037MHNym3awHj9iAUBdM/bmiLUvlc=")]
public string ConnectionString { get; set; }
Miqo.Config will encrypt the information before writing the property to the configuration file, and decrypt the information back into the property upon reading the configuration file. You can set your own key on a project to project basis. Miqo.Config uses AES to encrypt sensitive information.
Use the helper method StringCipher.CreateRandomKey()
to create a random AES key.
If you would like to prevent properties from being serialized to the configuration file, use the [JsonIgnore]
attribute.
[JsonIgnore]
public string NotReallyAllThatImportant { get; set; }
The encryption code is based on code by Ckode.Encryption by Steffen Skov.
Miqo.License is made available under the MIT License.