|
From: <fli...@us...> - 2008-10-17 19:00:42
|
Revision: 183
http://structuremap.svn.sourceforge.net/structuremap/?rev=183&view=rev
Author: flimflan
Date: 2008-10-17 19:00:37 +0000 (Fri, 17 Oct 2008)
Log Message:
-----------
Added ability to provide a default value to EqualToAppSetting if the appSetting key does not exist
Modified Paths:
--------------
trunk/Source/StructureMap/Pipeline/PropertyExpression.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs
Modified: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs
===================================================================
--- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2008-10-17 14:40:26 UTC (rev 182)
+++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2008-10-17 19:00:37 UTC (rev 183)
@@ -31,13 +31,26 @@
/// Sets the value of the constructor argument to the key/value in the
/// AppSettings
/// </summary>
- /// <param name="appSettingKey"></param>
+ /// <param name="appSettingKey">The key in appSettings for the value to use.</param>
/// <returns></returns>
public T EqualToAppSetting(string appSettingKey)
{
+ return EqualToAppSetting(appSettingKey, null);
+ }
+
+ /// <summary>
+ /// Sets the value of the constructor argument to the key/value in the
+ /// AppSettings when it exists. Otherwise uses the provided default value.
+ /// </summary>
+ /// <param name="appSettingKey">The key in appSettings for the value to use.</param>
+ /// <param name="defaultValue">The value to use if an entry for <paramref name="appSettingKey"/> does not exist in the appSettings section.</param>
+ /// <returns></returns>
+ public T EqualToAppSetting(string appSettingKey, string defaultValue)
+ {
string propertyValue = ConfigurationManager.AppSettings[appSettingKey];
+ if (propertyValue == null) propertyValue = defaultValue;
_instance.SetProperty(_propertyName, propertyValue);
- return (T) _instance;
+ return (T)_instance;
}
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2008-10-17 14:40:26 UTC (rev 182)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2008-10-17 19:00:37 UTC (rev 183)
@@ -35,7 +35,13 @@
.WithName("AppSetting")
.WithProperty("color").EqualToAppSetting("Color");
+ // Pull a property from the App config
+ registry.InstanceOf<IWidget>()
+ .Is.OfConcreteType<NotPluggableWidget>()
+ .WithName("UsesDefaultValue")
+ .WithProperty("name").EqualToAppSetting("WidgetName", "TheDefaultValue");
+
registry.InstanceOf<IWidget>().Is.OfConcreteType<AWidget>();
});
}
@@ -71,7 +77,7 @@
.Widget.IsType<AWidget>();
}
- [Test, Explicit]
+ [Test]
public void CreateAnInstancePullAPropertyFromTheApplicationConfig()
{
Assert.AreEqual("Blue", ConfigurationManager.AppSettings["Color"]);
@@ -80,6 +86,14 @@
}
[Test]
+ public void CreateAnInstanceUsingDefaultPropertyValueWhenSettingDoesNotExistInApplicationConfig()
+ {
+ Assert.AreEqual(null, ConfigurationManager.AppSettings["WidgetName"]);
+ var widget = (NotPluggableWidget)container.GetInstance<IWidget>("UsesDefaultValue");
+ Assert.AreEqual("TheDefaultValue", widget.Name);
+ }
+
+ [Test]
public void SimpleCaseWithNamedInstance()
{
container = new Container(
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|