-
Notifications
You must be signed in to change notification settings - Fork 1
Configuration
This page contains a description of how configuration classes work within the Mimick framework.
A configuration class is a class marked with the Configuration
attribute which is picked up when the Mimick framework is initialized for the first time, and allows for methods and properties to be decorated with attributes which affect how the framework behaves.
Example
[Configuration]
public class CustomConfiguration
{
}
As described on the Attributes page, the Provide
attribute allows for custom configuration values to be registered without needing to load from a resource file.
Example
[Configuration]
public class CustomConfiguration
{
[Provides("application.name")]
public string ApplicationName => "Test Application";
[Provides("application.id")]
public int GetApplicationId() => 12345;
}
The Component
attribute can be applied to a method or property within a class decorated with Configuration
to indicate that the member produces a value which must be registered as a component of the framework. The value produced from the member will be registered as a singleton instance within the framework.
A method which produces a component which also has a name beginning with the prefix Get
will have the remainder of the method name registered as an additional name for the component. For example, GetMyService()
will register the component under the name MyService
A property which produces a component will also have the component registered under the name of the property. For example, MyService { get; }
will register the component under the name MyService
Example
[Configuration]
public class CustomConfiguration
{
[Component]
public Service CustomService => new Service();
[Component]
public Service GetSecondCustomService() => new Service();
}
A configuration source is a resource descriptor which provides configuration values from an origin. Unlike configuration classes, a configuration source can be loaded from a resource file, and can be reloaded manually or based on an expiry period.
Configuration sources are added through the IConfigurationContext
interface, which is exposed through the IFrameworkContext
interface. Configuration sources can be added at run-time dynamically, but ideally should be added before the framework has been initialized.
The key-value configuration source allows for an IDictionary<string, string>
value to be used as a configuration container. The dictionary is mutable, meaning that any changes made to the dictionary after the configuration source is created will reflect immediately.
var dictionary = new Dictionary<string, string>();
dictionary.Add("Key1", "Value1");
dictionary.Add("Key2", "Value2");
configurations.Register(new KeyValueConfigurationSource(dictionary));
var value1 = configurations.Resolve("Key1"); // Value1
var value2 = configurations.Resolve("Key2"); // Value2
The XML configuration source consumes either an XmlDocument
object, string
file path, or a Stream
resource. The values of the configurations within the source are resolved using standard XPath notation, as described below.
<?xml version="1.0" encoding="utf-8"?>
<Configurations>
<Key1>Value1</Key1>
<Key2>Value2</Key2>
</Configurations>
var document = new XmlDocument();
document.Load("/path/to/document.xml");
configurations.Register(new XmlConfigurationSource(document));
var value1 = configurations.Resolve("//Configurations/Key1"); // Value1
var value2 = configurations.Resolve("//Configurations/Key2"); // Value2
The JSON configuration source consumes either a string
file path or a Stream
resource. The values of the configurations within the source are resolved using property paths separated by period (.
) symbols, as described below.
{ "Configurations": { "Key1": "Value1", "Key2": "Value2" } }
configurations.Register(new JsonConfigurationSource("/path/to/document.json"));
var value1 = configurations.Resolve("Configurations.Key1"); // Value1
var value2 = configurations.Resolve("Configurations.Key2"); // Value2
This requires the add-in
Mimick.Config.Json
which has a dependency onNewtonsoft.Json
The YAML configuration source functions similar to the JSON configuration source, except that the source document must be a valid YAML document. The same configuration selector must be used.
Configurations:
Key1: Value1
Key2: Value2
configurations.Register(new YamlConfigurationSource("/path/to/document.yaml"));
var value1 = configurations.Resolve("Configurations.Key1"); // Value1
var value2 = configurations.Resolve("Configurations.Key2"); // Value2
This requires the add-in
Mimick.Config.Yaml
which has a dependency onYamlDotNet.Signed