Table of Contents

Interface IConfigurationProvider

Namespace
Dynamicweb.Configuration
Assembly
Dynamicweb.Core.dll

Represents a configuration provider.

public interface IConfigurationProvider

Examples

namespace Dynamicweb.Configuration.Examples
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Example that shows how to use a custom configuration provider.
    /// </summary>
    class CustomConfigurationProviderSample
    {
        static void Configure()
        {
            // create an instance of your custom configuration provider and pass any parameter you need.
            var provider = new MyConfigurationProvider("C:\\config.txt");

            // add the provider to the configuration manager
            var configuration = new ConfigurationManager();
            configuration.AddProvider(provider);

            // read a value
            var value = configuration.GetInt32("mysetting");

            // write a value
            configuration.SetValue("mysetting", 123);
        }
    }

    /// <summary>
    /// Class MyConfigurationProvider is just a sample provider that reads configuration values from a simple text file.
    /// A configuration provider must implement the interface defined in Dynamicweb.Configuration.IConfigurationProvider.
    /// Caching and concurrency handling has been left out for the sake of simplicity, but this is something that you should consider in your own implementation. 
    /// </summary>
    class MyConfigurationProvider : IConfigurationProvider
    {
        private IDictionary<string, string> Data { get; set; }

        private string Path { get; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MyConfigurationProvider"/> class.
        /// </summary>
        /// <param name="fullpath">The absolute path to the configuration file.</param>
        public MyConfigurationProvider(string fullpath)
        {
            Path = fullpath;
            Reload();
        }

        /// <summary>
        /// Determines whether the configuration contains an entry with the specified key.
        /// </summary>
        /// <param name="key">The key to locate in the configuration.</param>
        /// <returns><c>true</c> if the configuration contains the specified configuration key; otherwise, <c>false</c>.</returns>
        public bool Contains(string key)
        {
            return Data.ContainsKey(key);
        }

        /// <summary>
        /// Persists the configuration data.
        /// </summary>
        public void Persist()
        {
            // write all configuration settings to the specified file

            var lines = new List<string>();
            foreach (KeyValuePair<string, string> pair in Data)
            {
                lines.Add(pair.Key + "|" + pair.Value);
            }

            System.IO.File.WriteAllLines(Path, lines);
        }

        /// <summary>
        /// Reloads the configuration data.
        /// </summary>
        public void Reload()
        {
            // read all configuration settings from the specified file

            Data = new Dictionary<string, string>();

            if (!string.IsNullOrEmpty(Path) && System.IO.File.Exists(Path))
            {
                var lines = System.IO.File.ReadAllLines(Path);
                foreach (var line in lines)
                {
                    var pair = line.Split('|');
                    var key = pair[0];
                    var value = pair[1];

                    Data.Add(key, value);
                }
            }
        }

        /// <summary>
        /// Sets the value of an entry with the specified key.
        /// </summary>
        /// <param name="key">The key of the entry to set a value for.</param>
        /// <param name="value">A string that represents the value of an entry.</param>
        public void SetValue(string key, string value)
        {
            // store the value somewhere.
            Data[key] = value;

            // Note: You do not need to actually persist the data here (write to file/database/whatever), since the configuration manager will call the Persist() method after setting the values.
        }

        /// <summary>
        /// Attempts to get the value associated with the specified key.
        /// </summary>
        /// <param name="key">Key of the value to get.</param>
        /// <param name="value">When this method returns, contains the value associated with the specified key, or null if the operation failed.</param>
        /// <returns><c>true</c> if the specified key was found; otherwise, <c>false</c>.</returns>
        public bool TryGet(string key, out string value)
        {
            return Data.TryGetValue(key, out value);
        }

        /// <summary>
        /// Gets all keys that can be used to get or set data.
        /// </summary>
        /// <returns>keys</returns>
        public ICollection<string> GetKeys()
        {
            return Data.Keys;
        }
    }
}

Methods

Contains(string)

Determines whether the configuration contains an entry with the specified key.

bool Contains(string key)

Parameters

key string

The key to locate in the configuration.

Returns

bool

true if the configuration contains the specified configuration key; otherwise, false.

GetKeys()

Gets keys for all existing settings.

ICollection<string> GetKeys()

Returns

ICollection<string>

keys

Persist()

Persists the configuration data.

void Persist()

Reload()

Reloads the configuration data.

void Reload()

SetValue(string, string)

Sets the value of an entry with the specified key.

void SetValue(string key, string value)

Parameters

key string

The key of the entry to set a value for.

value string

A string that represents the value of an entry.

TryGet(string, out string?)

Attempts to get the value associated with the specified key.

bool TryGet(string key, out string? value)

Parameters

key string

Key of the value to get.

value string

When this method returns, contains the value associated with the specified key, or null if the operation failed.

Returns

bool

true if the specified key was found; otherwise, false.

To top