Git Product home page Git Product logo

miqo.config's Introduction



Miqo.Config

๐Ÿ“„๐ŸŒŸ Managing application and user settings for your .NET application has never been so easy

Overview

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.

Adding Miqo.Config to Your Project

The library is available as a signed NuGet package.

PM> Install-Package Miqo.Config

Creating the Configuration Class

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 a Configuration File

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).

Writing Settings to a Configuration File

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"
   ]
}

User Specific Settings

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.

Additional features

Miqo.Config has some other nifty features that may be useful to you as a developer.

Logging

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();

Protecting Sensitive Information

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.

Ignoring Properties

If you would like to prevent properties from being serialized to the configuration file, use the [JsonIgnore] attribute.

[JsonIgnore]
public string NotReallyAllThatImportant { get; set; }

Acknowledgements

The encryption code is based on code by Ckode.Encryption by Steffen Skov.

License

Miqo.License is made available under the MIT License.

miqo.config's People

Contributors

natsuo avatar

Watchers

 avatar  avatar

miqo.config's Issues

Add CHANGELOG.md

The project should have a proper CHANGELOG.md so developers can see how the project has changed over time.

Add Conditional TargetFrameworks

The current build process fails when attempting to build in a non-Windows environment.

Miqo.Config should use proper conditional TargetFrameworks to ensure that the library can be compiled on other platforms.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.