//****************************************************************************************************** // SettingsCollection.cs - Gbtc // // Copyright © 2012, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 08/19/2010 - Pinal C. Patel // Generated original version of source code. // 12/13/2012 - Starlynn Danyelle Gilliam // MOdified Header. // //****************************************************************************************************** using System; using System.Collections.Generic; using GSF.Validation; namespace GSF.Collections { /// /// A collection of settings that can be represented as a string of key-value pairs for easy persistence and also provide built-in validation support for the settings. /// /// /// This example shows how to use for key-value pair type settings and apply validation to them: /// /// using System; /// using System.Collections.Generic; /// using GSF.Collections; /// using GSF.Validation; /// /// class Program /// { /// static void Main(string[] args) /// { /// // Initialize settings. /// SettingsCollection settings = "Server=localhost;Port=5000"; /// // Add validation rules. /// settings.Validation.AddValidation("Server", new NonNullStringValidator()); /// settings.Validation.AddValidation("Port", new NonNullStringValidator()); /// settings.Validation.AddValidation("Port", new NumericRangeValidator(1000, 2000)); /// /// // Validate settings. /// string errors; /// if (!settings.Validation.Validate(out errors)) /// { /// // Show validation errors. /// Console.WriteLine(string.Format("Settings: {0}\r\n", settings)); /// Console.WriteLine(errors); /// } /// else /// { /// // Show stored settings. /// foreach (KeyValuePair<string, string> setting in settings) /// { /// Console.WriteLine(string.Format("Key={0}; Value={1}", setting.Key, setting.Value)); /// } /// } /// /// Console.ReadLine(); /// } /// } /// /// /// [Serializable] public class SettingsCollection : Dictionary { #region [ Members ] // Fields private ValidationService m_validationService; #endregion #region [ Constructors ] /// /// Initializes a new instance of the class. /// public SettingsCollection() : this(comparer: null) { } /// /// Initializes a new instance of the class. /// /// The to use when comparing keys or null to use the default . public SettingsCollection(IEqualityComparer comparer) : base(comparer) { Initialize(); } /// /// Initializes a new instance of the class. /// /// The whose elements are to be copied to this . public SettingsCollection(IDictionary dictionary) : this(dictionary, null) { } /// /// Initializes a new instance of the class. /// /// The whose elements are to be copied to this . /// The to use when comparing keys or null to use the default . public SettingsCollection(IDictionary dictionary, IEqualityComparer comparer) : base(dictionary, comparer) { Initialize(); } #endregion #region [ Properties ] /// /// Gets the object used to perform validation on the . /// public ValidationService Validation { get { return m_validationService; } } #endregion #region [ Methods ] /// /// Adds an element with the specified and if an element is not present with the specified . /// /// The key of the element to add. /// The value of the element to add. /// true if an element is added; otherwise false. public bool TryAdd(string key, string value) { if (base.ContainsKey(key)) return false; else base.Add(key, value); return true; } /// /// Gets the representation of . /// /// A that represents . public override string ToString() { return this.JoinKeyValuePairs(); } private void Initialize() { m_validationService = new ValidationService(Lookup); } private object Lookup(string source) { string value; if (base.TryGetValue(source, out value)) return value; else return string.Empty; } #endregion #region [ Operators ] /// /// Implicitly converts to a . /// /// The to convert. /// A value representing the result. public static implicit operator string(SettingsCollection value) { return value.ToString(); } /// /// Implicitly converts to . /// /// The to convert. /// A object representing the result. public static implicit operator SettingsCollection(string value) { return new SettingsCollection(value.ParseKeyValuePairs(), StringComparer.CurrentCultureIgnoreCase); } #endregion } }