Click or drag to resize

IConfigurationProvider Interface

Represents a configuration provider.

Namespace:  Dynamicweb.Configuration
Assembly:  Dynamicweb.Configuration (in Dynamicweb.Configuration.dll) Version: 4.1.3
Syntax
public interface IConfigurationProvider

The IConfigurationProvider type exposes the following members.

Methods
  NameDescription
Public methodContains
Determines whether the configuration contains an entry with the specified key.
Public methodGetKeys
Gets keys for all existing settings.
Public methodPersist
Persists the configuration data.
Public methodReload
Reloads the configuration data.
Public methodSetValue
Sets the value of an entry with the specified key.
Public methodTryGet
Attempts to get the value associated with the specified key.
Top
Examples
How to implement a custom configuration provider
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;
        }
    }
}
See Also