Permalink
Browse files

Added support for optional settings with default values via SettingPr…

…operty attribute
  • Loading branch information...
tparvi committed Aug 28, 2011
1 parent 68e91a7 commit 10951371414ed82145eca951e8d115586a81174d
@@ -429,7 +429,7 @@ public virtual void WriteInto(object instance)
{
if (null == instance)
{
- throw new ArgumentNullException("instance", "Cannot write configuration values into null object");
+ throw new ArgumentNullException("instance", "Cannot write configuration values into null object.");
}
var properties = instance.GetType().GetProperties();
@@ -438,10 +438,10 @@ public virtual void WriteInto(object instance)
{
var settingName = this.GetSettingName(propertyInfo);
- if (this.CanWriteInto(propertyInfo, settingName))
+ if (this.CanWriteInto(propertyInfo))
{
var type = propertyInfo.PropertyType;
- var rawValue = this.GetValue(settingName);
+ var rawValue = this.GetSettingsValueForProperty(propertyInfo, settingName);
var formatProvider = this.GetFormatProvider(propertyInfo);
var value = this.ConvertValue(type, settingName, rawValue, formatProvider);
@@ -530,29 +530,55 @@ protected static Configuration OpenConfigurationFile(FileOption fileOption)
}
/// <summary>
- /// Checks if property can be written into.
+ /// Gets the value for the property. The value is mandatory except if property
+ /// is associated with <see cref="SettingProperty"/> attribute in which case
+ /// it can be optional.
/// </summary>
/// <param name="propertyInfo">
/// The property info.
/// </param>
/// <param name="settingName">
- /// The setting Name.
+ /// The setting name.
+ /// </param>
+ /// <returns>
+ /// Value for the property.
+ /// </returns>
+ protected virtual string GetSettingsValueForProperty(PropertyInfo propertyInfo, string settingName)
+ {
+ var attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(SettingProperty)) as SettingProperty;
+ if (null != attribute && attribute.IsOptional)
+ {
+ return this.GetOptionalValue(settingName, attribute.DefaultValue);
+ }
+
+ return this.GetValue(settingName);
+ }
+
+ /// <summary>
+ /// Checks if property can be written into. If the property is read only or
+ /// it is ignored then we cannot write into it.
+ /// </summary>
+ /// <param name="propertyInfo">
+ /// The property info.
/// </param>
/// <returns>
/// True if property can be written into.
/// </returns>
- protected virtual bool CanWriteInto(PropertyInfo propertyInfo, string settingName)
+ protected virtual bool CanWriteInto(PropertyInfo propertyInfo)
{
- if (propertyInfo.CanWrite && this.HasAppSetting(settingName))
+ if (!propertyInfo.CanWrite)
{
- var attribute = Attribute.GetCustomAttribute(propertyInfo, typeof(IgnoreProperty));
- if (null == attribute)
- {
- return true;
- }
+ return false;
+ }
+
+ // If the property is ignored then we don't write into it.
+ var ignoredAttribute = Attribute.GetCustomAttribute(propertyInfo, typeof(IgnoreProperty));
+ if (null != ignoredAttribute)
+ {
+ return false;
}
- return false;
+ return true;
}
/// <summary>
@@ -9,6 +9,14 @@
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class SettingProperty : Attribute
{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="SettingProperty"/> class.
+ /// </summary>
+ public SettingProperty()
+ {
+ this.IsOptional = false;
+ }
+
/// <summary>
/// Gets or sets CultureName which is used when converting
/// value. Maps to <see cref="CultureInfo.Name"/>. If this is
@@ -21,5 +29,18 @@ public class SettingProperty : Attribute
/// setting. If it is null then name of the property is used.
/// </summary>
public string SettingName { get; set; }
+
+ /// <summary>
+ /// Gets or sets a value indicating whether the setting is optional.
+ /// If the setting is not found then <see cref="DefaultValue"/> is
+ /// used.
+ /// </summary>
+ public bool IsOptional { get; set; }
+
+ /// <summary>
+ /// Gets or sets DefaultValue which is used when <see cref="IsOptional"/>
+ /// is <true/> and setting does not exist.
+ /// </summary>
+ public string DefaultValue { get; set; }
}
}
@@ -56,8 +56,45 @@ public void And_CultureName_is_null_Then_InvariantCulture_is_used()
Assert.AreEqual(11.0d, mySettings.FinnishLocaleDoubleValue);
}
+ [Test]
+ public void And_IsOptional_is_true_and_value_is_found_DefaultValue_is_not_used()
+ {
+ var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
+
+ var mySettings = new SettingWithOptionalExistingProperty();
+ settings.WriteInto(mySettings);
+
+ Assert.AreEqual(1, mySettings.IntValue);
+
+ }
+
+ [Test]
+ public void And_IsOptional_is_true_and_value_is_not_found_DefaultValue_is_used()
+ {
+ var settings = new AppSettings(SimpleConfig.AbsolutePathToConfigFile);
+
+ var mySettings = new SettingWithOptionalNonExistingProperty();
+ settings.WriteInto(mySettings);
+
+ Assert.AreEqual(123, mySettings.IntValue);
+ }
+
+ }
+
+ public class SettingWithOptionalNonExistingProperty
+ {
+ [SettingProperty(SettingName = "NonExistingProperty", IsOptional = true, DefaultValue = "123")]
+ public int IntValue { get; set; }
}
+
+ public class SettingWithOptionalExistingProperty
+ {
+ [SettingProperty(IsOptional = true, DefaultValue = "123")]
+ public int IntValue { get; set; }
+ }
+
+
public class SettingsWithNullCultureName
{
[SettingProperty(SettingName = "DoubleWithFinnishLocale")]

0 comments on commit 1095137

Please sign in to comment.