A lightweight library for creating, loading, and saving application configuration files with support for multiple serialization formats via the ISerializationManager interface.
NuGet: https://www.nuget.org/packages/SimpleConfigs
Target framework: .NET 8
Install the core package:
dotnet add package SimpleConfigsFor JSON support:
dotnet add package SimpleConfigs.JSONCore library that provides functionality for creating, saving, and loading configs.
Has no external dependencies.
Does not include an implementation of the ISerializationManager interface.
Provides a JSON implementation of ISerializationManager.
Dependencies: SimpleConfigs, Newtonsoft.Json.
Contains usage examples.
Depends on all project assemblies except SimpleConfigs.Test.
Contains tests for all assemblies.
Depends on all assemblies except SimpleConfigs.Examples.
Example configuration class:
public class MainConfig
{
public string SomeUrl = "http://www.example.com/";
public int SomeValue = 456;
}To save this config to a file or populate the object from a file, use ConfigsService.
It requires an ISerializationManager as the first parameter.
You can implement the interface yourself or use an existing implementation, such as SimpleConfigs.JSON.
ConfigsService can register one or multiple config types (passed as Type arguments).
var configsService = new ConfigsService(
new JsonSerializationManager(), // ISerializationManager
new LocalFileSystem(), // IFileSystem
typeof(MainConfig), // registered config type
typeof(SomeOtherConfig) // another registered config type
);
// Creates missing config files
// and loads data from existing files
await configsService.InitializeAllConfigsAsync();var mainConfigInstance = configsService.GetConfig<MainConfig>();
Console.WriteLine($"{mainConfigInstance.SomeValue}"); // 456
mainConfigInstance.SomeValue = 15;
await configsService.SaveConfigToFileAsync<MainConfig>();If not specified, the default folder is the directory of the running assembly.
[RelativePath("Configs/Main")]
public class MainConfig
{
}File path:
<application folder>/Configs/Main/MainConfig.cfg
If the attribute is not specified, the file will be created in the running assembly directory with the default name MainConfig.cfg.
Default: TypeName.cfg
[RelativePath("Configs/Main"), ConfigName("MyConfigName.myFileExtension")]
public class MainConfig
{
}File path:
<application folder>/Configs/Main/MyConfigName.myFileExtension
configsService.SetPathOverrideSettings<MainConfig>(
new PathSettings("Configs/Overrided", "MyConfigName.myFileExtension"));
// Reset to default
configsService.SetPathOverrideSettings<MainConfig>(
new PathSettings(null, ""));You can register config types together with custom path settings using the second constructor:
var configsService = new ConfigsService(
new JsonSerializationManager(),
new LocalFileSystem(),
(typeof(MainConfig), new PathSettings("Configs/Main", "main.cfg")),
(typeof(SomeOtherConfig), null) // uses default settings
);Configs can be registered or unregistered at runtime:
configsService.RegisterConfigType(typeof(MainConfig).FullName, typeof(MainConfig).Assembly);
configsService.UnregisterConfigType(typeof(MainConfig).FullName);You can specify a common directory prefix for all config files:
configsService.CommonRelativeDirectoryPath = "AllConfigs";Final path pattern:
<ApplicationDirectory>/AllConfigs/<ConfigRelativePath>
await configsService.CreateConfigFileAsync(typeof(MainConfig).FullName);
await configsService.DeleteConfigFileAsync(typeof(MainConfig).FullName);If a config implements IDataCorrectnessChecker, validation will be executed automatically during save/load operations.
public class MainConfig : IDataCorrectnessChecker
{
public int Port = 8080;
public Task CheckDataCorrectnessAsync()
{
if (Port <= 0)
throw new Exception("Invalid port");
return Task.CompletedTask;
}
}You can disable validation:
await configsService.SaveConfigToFileAsync(typeof(MainConfig).FullName, checkDataCorrectness: false);Configs implementing ISerializationListner receive callbacks:
public class MainConfig : ISerializationListner
{
public void OnBeforeSerialize()
{
// called before saving
}
public void OnAfterDeserialized()
{
// called after loading
}
}All operations have configurable timeouts:
configsService.SerializationTimeoutInMilliseconds = 10000;
configsService.DeserializationTimeoutInMilliseconds = 10000;
configsService.FileWriteTimeoutInMilliseconds = 10000;bool exists = configsService.IsConfigExist(typeof(MainConfig).FullName);
var config = configsService.GetConfig(typeof(MainConfig).FullName);Responsible for converting a config object into a byte array to be written to a file.
The implementation is not included in SimpleConfigs to avoid unnecessary dependencies.
You can implement it yourself or use an existing one, such as SimpleConfigs.JSON.