//******************************************************************************************************
// AppSettingsBase.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:
// ----------------------------------------------------------------------------------------------------
// 01/30/2009 - J. Ritchie Carroll
// Generated original version of source code.
// 03/31/2009 - J. Ritchie Carroll
// Made initialize during constructor optional for languages that do not initialize
// member variables before call to constructor (e.g., Visual Basic.NET).
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
//
//******************************************************************************************************
using System;
using System.ComponentModel;
using System.Configuration;
namespace GSF.Configuration
{
///
/// Represents the base class for application settings that are synchronized with the "appSettings" section in a configuration file.
///
///
///
/// In order to make custom types serializable for the configuration file, implement a for the type.
/// See MSDN for details.
///
///
/// Here is an example class derived from that automatically
/// serializes its fields and properties to the configuration file.
///
/// public enum MyEnum
/// {
/// One,
/// Two,
/// Three
/// }
///
/// public class MySettings : AppSettingsBase
/// {
/// // Private property fields (private fields will not be serialized)
/// private double m_doubleVal;
///
/// // Public settings fields
/// public bool BoolVal = true;
/// public int IntVal = 1;
/// public float FloatVal = 3.14F;
/// public string StrVal = "This is a test...";
/// public MyEnum EnumVal = MyEnum.Three;
///
/// [SettingName("UserOptions"), EncryptSetting()]
/// public string Password = "default";
///
/// // Mark this field to not be serialized to INI file...
/// [SerializeSetting(false)]
/// public decimal DecimalVal;
///
/// [Category("OtherSettings"), DefaultValue(1.159D)]
/// public double DoubleVal
/// {
/// get
/// {
/// return m_doubleVal;
/// }
/// set
/// {
/// m_doubleVal = value;
/// }
/// }
///
/// [SerializeSetting(false)]
/// public bool DontSerializeMe { get; set; }
/// }
///
///
///
public abstract class AppSettingsBase : SettingsBase
{
#region [ Members ]
// Fields
private ConfigurationFile m_configFile;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class for the application's configuration file.
///
protected AppSettingsBase()
: this(ConfigurationFile.Current, false, true)
{
}
///
/// Creates a new instance of the class for the application's configuration file.
///
/// Configuration file used for accessing settings.
///
/// Assigns flag that determines if is required
/// to exist before a field or property is serialized to the configuration file.
///
/// Determines if method should be called from constructor.
///
/// Note that some .NET languages (e.g., Visual Basic) will not initialize member elements before call to constuctor,
/// in this case should be set to false, then the
/// method should be called manually after all properties have been initialized. Alternately, consider using the
/// on the fields or properties and this will be used to initialize the values.
///
protected AppSettingsBase(ConfigurationFile configFile, bool requireSerializeSettingAttribute, bool initialize)
: base(requireSerializeSettingAttribute)
{
m_configFile = configFile;
// Make sure settings exist and load current values
if (initialize)
Initialize();
}
#endregion
#region [ Properties ]
///
/// Gets or sets reference to working configuration file.
///
/// value cannot be null.
protected ConfigurationFile ConfigFile
{
get
{
return m_configFile;
}
set
{
if ((object)value == null)
throw new NullReferenceException("value cannot be null");
m_configFile = value;
}
}
#endregion
#region [ Methods ]
///
/// Create setting in configuration file if it doesn't already exist.
/// This method is for internal use.
///
/// Field or property name, if useful (can be different from setting name).
/// Setting name.
/// Setting value.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void CreateSetting(string name, string setting, string value)
{
KeyValueConfigurationCollection settings = m_configFile.Configuration.AppSettings.Settings;
if ((object)settings[setting] == null)
settings.Add(setting, value);
}
///
/// Retrieves setting from configuration file.
/// This method is for internal use.
///
/// Field or property name, if useful (can be different from setting name).
/// Setting name.
/// Setting value.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override string RetrieveSetting(string name, string setting)
{
return m_configFile.Configuration.AppSettings.Settings[setting].Value;
}
///
/// Stores setting to configuration file.
/// This method is for internal use.
///
/// Field or property name, if useful (can be different from setting name).
/// Setting name.
/// Setting value.
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void StoreSetting(string name, string setting, string value)
{
m_configFile.Configuration.AppSettings.Settings[setting].Value = value;
}
///
/// Persist any pending changes to configuration file.
/// This method is for internal use.
///
[EditorBrowsable(EditorBrowsableState.Never)]
protected override void PersistSettings()
{
m_configFile.Save();
}
#endregion
}
}