Settings Service - adamconnelly/dbversion GitHub Wiki

Settings Service

The settings service can be used to store and retrieve settings. The service uses the .NET framework to ensure that it writes its settings to the correct place. This means that settings files will be stored in the following locations:

Environment Location
Windows 7 C:\Users<username>\AppData\Roaming\dbversion
Windows XP '''TODO: Find out'''
Mono ~/.config/dbversion

Interface

The settings service can be found in the dbversion.Settings namespace, and has the following interface:

public interface ISettingsService
{
    string SettingsDirectory { get; }
    void Write(Stream stream, string fileName);
    Stream Read(string fileName);
    void Serialize(object @object, string fileName);
    T DeSerialize<T>(string fileName);
}

Although the service provides the Read() and Write() methods, you will probably find it easier to use DeSerialize() and Serialize(). These methods handle the serialization process for you so that you don't need to worry about how to save your settings objects into the settings directory.

Example Usage

For both the examples, we will use the following object to store settings:

public class MySettings
{
    public string StringSetting { get; set; }
    public int NumberSetting { get; set; }
}

Saving

The following example creates an instance of the MySettings object and then saves it using the settings service:

using System.ComponentModel.Composition; // Allows us to Import the settings service using MEF.

using dbversion.Settings;

public class SaveSettings
{
    [Import]
    public ISettingsService SettingsService { get; set; }

    public void SaveSettings()
    {
        var settings = new MySettings { StringSetting = "ABC", NumberSetting = 123 };
        this.SettingsService.Serialize(settings, "mySettings.xml");
    }
}

After the SaveSettings method has executed, you should find a file called mySettings.xml in your settings directory that looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <StringSetting>ABC</StringSetting>
    <NumberSetting>123</NumberSetting>
</MySettings>

Loading

The following example loads the saved instance of the MySettings object:

using System.ComponentModel.Composition; // Allows us to Import the settings service using MEF.

using dbversion;
using dbversion.Settings;

public class LoadSettings
{
    [Import]
    public ISettingsService SettingsService { get; set; }

    [Import]
    public IMessageService MessageService { get; set; }

    public void LoadSettings()
    {
        var settings = this.SettingsService.DeSerialize<MySettings>("mySettings.xml");

        this.MessageService.WriteLine(settings.StringSetting);
        this.MessageService.WriteLine(settings.NumberSetting.ToString());
    }
}

After the LoadSettings method has run the following output should be produced:

ABC
123
⚠️ **GitHub.com Fallback** ⚠️