From: <jer...@us...> - 2007-03-26 15:33:17
|
Revision: 30 http://structuremap.svn.sourceforge.net/structuremap/?rev=30&view=rev Author: jeremydmiller Date: 2007-03-26 07:31:28 -0700 (Mon, 26 Mar 2007) Log Message: ----------- Deep testing on the Fluent Interface API Modified Paths: -------------- trunk/Source/StructureMap/Attributes/PluggableAttribute.cs trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/MementoSource.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/ChildInstanceExpressionTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/InstanceExpressionTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing.Widget/IWidget.cs Added Paths: ----------- trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs Modified: trunk/Source/StructureMap/Attributes/PluggableAttribute.cs =================================================================== --- trunk/Source/StructureMap/Attributes/PluggableAttribute.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Attributes/PluggableAttribute.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -47,7 +47,7 @@ /// <returns></returns> public static PluggableAttribute InstanceOf(Type objectType) { - return (PluggableAttribute) GetCustomAttribute(objectType, typeof (PluggableAttribute), false); + return GetCustomAttribute(objectType, typeof (PluggableAttribute), false) as PluggableAttribute; } /// <summary> Modified: trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Configuration/DSL/ChildInstanceExpression.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -41,8 +41,9 @@ Type pluggedType = typeof (T); ExpressionValidator.ValidatePluggabilityOf(pluggedType).IntoPluginType(_childType); + InstanceExpression child = new InstanceExpression(_childType); - child.UsingConcreteType<T>(); + child.TypeExpression().UsingConcreteType<T>(); _children.Add(child); _builder = child; Modified: trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -13,6 +13,7 @@ private Type _pluginType; private List<AlterPluginFamilyDelegate> _alterations = new List<AlterPluginFamilyDelegate>(); private InstanceScope _scope = InstanceScope.PerRequest; + private List<IExpression> _children = new List<IExpression>(); public CreatePluginFamilyExpression(Type pluginType) { @@ -25,6 +26,11 @@ InterceptorChainBuilder builder = new InterceptorChainBuilder(); family.InterceptionChain = builder.Build(_scope); + foreach (IExpression child in _children) + { + child.Configure(graph); + } + foreach (AlterPluginFamilyDelegate alteration in _alterations) { alteration(family); @@ -40,6 +46,7 @@ { builder.ValidatePluggability(_pluginType); + _children.Add(builder); _alterations.Add(delegate(PluginFamily family) { InstanceMemento memento = builder.BuildMemento(family); Modified: trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Configuration/DSL/IMementoBuilder.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -3,7 +3,7 @@ namespace StructureMap.Configuration.DSL { - public interface IMementoBuilder + public interface IMementoBuilder : IExpression { InstanceMemento BuildMemento(PluginFamily family); InstanceMemento BuildMemento(PluginGraph graph); Modified: trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Configuration/DSL/InstanceExpression.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -53,22 +53,13 @@ } - public InstanceExpression UsingConcreteType<T>() - { - _pluggedType = typeof (T); - return this; - } + public PropertyExpression WithProperty(string propertyName) { return new PropertyExpression(this, _memento, propertyName); } - public InstanceExpression UsingConcreteTypeNamed(string concreteKey) - { - _memento.ConcreteKey = concreteKey; - return this; - } public ChildInstanceExpression Child<T>(string propertyName) { @@ -109,5 +100,34 @@ ExpressionValidator.ValidatePluggabilityOf(_pluggedType).IntoPluginType(pluginType); } + + internal InstanceTypeExpression TypeExpression() + { + return new InstanceTypeExpression(this); + } + + + public class InstanceTypeExpression + { + private readonly InstanceExpression _parent; + + internal InstanceTypeExpression(InstanceExpression parent) + { + _parent = parent; + } + + public InstanceExpression UsingConcreteType<T>() + { + _parent._pluggedType = typeof(T); + return _parent; + } + + public InstanceExpression UsingConcreteTypeNamed(string concreteKey) + { + _parent._memento.ConcreteKey = concreteKey; + return _parent; + } + + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -70,16 +70,17 @@ return new InstanceManager(_graph); } - public InstanceExpression AddInstanceOf<T>() + public InstanceExpression.InstanceTypeExpression AddInstanceOf<T>() { InstanceExpression expression = new InstanceExpression(typeof (T)); addExpression(expression); - return expression; + return expression.TypeExpression(); } - public static InstanceExpression Instance<T>() + public static InstanceExpression.InstanceTypeExpression Instance<T>() { - return new InstanceExpression(typeof (T)); + InstanceExpression expression = new InstanceExpression(typeof (T)); + return expression.TypeExpression(); } public static PrototypeExpression<T> Prototype<T>(T prototype) @@ -87,6 +88,11 @@ return new PrototypeExpression<T>(prototype); } + public static LiteralExpression<T> Object<T>(T instance) + { + return new LiteralExpression<T>(instance); + } + public LiteralExpression<T> AddInstanceOf<T>(T target) { LiteralExpression<T> literal = new LiteralExpression<T>(target); Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -35,7 +35,7 @@ private string _defaultKey = string.Empty; private Type _pluginType; - private MementoSource _source = new MemoryMementoSource(); + private MementoSource _source; private DefinitionSource _definitionSource = DefinitionSource.Implicit; private string _pluginTypeName; private InterceptionChain _interceptionChain; @@ -53,6 +53,8 @@ _plugins = new PluginCollection(this); _interceptionChain = new InterceptionChain(); + + Source = new MemoryMementoSource(); } public PluginFamily(Type pluginType, string defaultInstanceKey, MementoSource source) @@ -89,6 +91,8 @@ _pluginTypeName = path.AssemblyQualifiedName; _interceptionChain = new InterceptionChain(); initializeExplicit(path, defaultKey); + + Source = new MemoryMementoSource(); } Modified: trunk/Source/StructureMap/InstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/InstanceMemento.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/InstanceMemento.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -164,18 +164,7 @@ if (memento == null) { - try - { - returnValue = manager.CreateInstance(typeName); - } - catch (StructureMapException) - { - throw; - } - catch (Exception ex) - { - throw new StructureMapException(209, ex, key, typeName); - } + returnValue = buildDefaultChild(key, manager, typeName); } else { @@ -186,7 +175,25 @@ return returnValue; } + private static object buildDefaultChild(string key, InstanceManager manager, string typeName) + { + object returnValue; + try + { + returnValue = manager.CreateInstance(typeName); + } + catch (StructureMapException) + { + throw; + } + catch (Exception ex) + { + throw new StructureMapException(209, ex, key, typeName); + } + return returnValue; + } + /// <summary> /// Not used yet. /// </summary> Modified: trunk/Source/StructureMap/MementoSource.cs =================================================================== --- trunk/Source/StructureMap/MementoSource.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/MementoSource.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using StructureMap.Configuration.Tokens; using StructureMap.Graph; using StructureMap.Source; @@ -15,7 +16,7 @@ { private PluginFamily _family; private InstanceMemento _defaultMemento; - private Hashtable _externalMementos = new Hashtable(); + private Dictionary<string, InstanceMemento> _externalMementos = new Dictionary<string, InstanceMemento>(); protected MementoSource() : base() { @@ -32,7 +33,7 @@ if (_externalMementos.ContainsKey(instanceKey)) { - returnValue = (InstanceMemento) _externalMementos[instanceKey]; + returnValue = _externalMementos[instanceKey]; } else if (containsKey(instanceKey)) { @@ -88,6 +89,11 @@ else if (memento.IsReference) { returnValue = GetMemento(memento.ReferenceKey); + + if (returnValue == null) + { + throw new StructureMapException(200, memento.ReferenceKey, Family.PluginTypeName); + } } return returnValue; Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap/StructureMap.csproj 2007-03-26 14:31:28 UTC (rev 30) @@ -215,6 +215,7 @@ </Compile> <Compile Include="Configuration\DSL\ChildInstanceExpression.cs" /> <Compile Include="Configuration\DSL\CreatePluginFamilyExpression.cs" /> + <Compile Include="Configuration\DSL\ExpressionValidator.cs" /> <Compile Include="Configuration\DSL\IExpression.cs" /> <Compile Include="Configuration\DSL\IMementoBuilder.cs" /> <Compile Include="Configuration\DSL\InstanceDefaultExpression.cs" /> Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/AddInstanceTester.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -21,20 +21,20 @@ // Add an instance with properties registry.AddInstanceOf<IWidget>() + .UsingConcreteType<ColorWidget>() .WithName("DarkGreen") - .UsingConcreteType<ColorWidget>() .WithProperty("Color").EqualTo("DarkGreen"); // Add an instance by specifying the ConcreteKey registry.AddInstanceOf<IWidget>() + .UsingConcreteTypeNamed("Color") .WithName("Purple") - .UsingConcreteTypeNamed("Color") .WithProperty("Color").EqualTo("Purple"); // Pull a property from the App config registry.AddInstanceOf<IWidget>() + .UsingConcreteType<ColorWidget>() .WithName("AppSetting") - .UsingConcreteType<ColorWidget>() .WithProperty("Color").EqualToAppSetting("Color"); @@ -184,8 +184,8 @@ // Add an instance by specifying the ConcreteKey registry.AddInstanceOf<IWidget>() + .UsingConcreteTypeNamed("Color") .WithName("Purple") - .UsingConcreteTypeNamed("Color") .WithProperty("Color").EqualTo("Purple"); // Specify a new Instance, override a dependency with a named instance @@ -217,6 +217,20 @@ { get { return _widget; } } + + + public override bool Equals(object obj) + { + if (this == obj) return true; + WidgetRule widgetRule = obj as WidgetRule; + if (widgetRule == null) return false; + return Equals(_widget, widgetRule._widget); + } + + public override int GetHashCode() + { + return _widget != null ? _widget.GetHashCode() : 0; + } } public class WidgetThing : IWidget Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/ChildInstanceExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/ChildInstanceExpressionTester.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/ChildInstanceExpressionTester.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -40,7 +40,8 @@ MemoryInstanceMemento memento = new MemoryInstanceMemento(); ChildInstanceExpression expression = new ChildInstanceExpression(instance, memento, "a property", typeof(IType)); - InstanceExpression child = new InstanceExpression(typeof(IType)).UsingConcreteType<AbstractType>(); + InstanceExpression child = Registry.Instance<IType>().UsingConcreteType<AbstractType>(); + expression.Is(child); } } Added: trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/DeepInstanceTester.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -0,0 +1,180 @@ +using NUnit.Framework; +using Rhino.Mocks; +using StructureMap.Configuration.DSL; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Configuration.DSL +{ + [TestFixture] + public class DeepInstanceTester + { + private Thing _prototype = new Thing(4, "Jeremy", .333, new WidgetRule(new ColorWidget("yellow"))); + + private void assertThingMatches(Registry registry) + { + IInstanceManager manager = registry.BuildInstanceManager(); + Thing actual = manager.CreateInstance<Thing>(); + Assert.AreEqual(_prototype, actual); + } + + [Test] + public void DeepInstanceTest1() + { + Registry registry = new Registry(); + InstanceExpression widgetExpression = Registry.Instance<IWidget>() + .UsingConcreteType<ColorWidget>() + .WithProperty("Color").EqualTo("yellow"); + + InstanceExpression ruleExpression = Registry.Instance<Rule>() + .UsingConcreteType<WidgetRule>() + .Child<IWidget>().Is(widgetExpression); + + registry.BuildInstancesOf<Thing>().TheDefaultIs( + Registry.Instance<Thing>() + .UsingConcreteType<Thing>() + .WithProperty("name").EqualTo("Jeremy") + .WithProperty("count").EqualTo(4) + .WithProperty("average").EqualTo(.333) + .Child<Rule>().Is( + ruleExpression + ) + ); + + assertThingMatches(registry); + } + + [Test] + public void DeepInstance2() + { + Registry registry = new Registry(); + registry.BuildInstancesOf<IWidget>().TheDefaultIs( + Registry.Instance<IWidget>().UsingConcreteType<ColorWidget>() + .WithProperty("Color").EqualTo("yellow") + ); + + registry.BuildInstancesOf<Rule>().TheDefaultIsConcreteType<WidgetRule>(); + + registry.BuildInstancesOf<Thing>().TheDefaultIs( + Registry.Instance<Thing>() + .UsingConcreteType<Thing>() + .WithProperty("average").EqualTo(.333) + .WithProperty("name").EqualTo("Jeremy") + .WithProperty("count").EqualTo(4) + ); + + assertThingMatches(registry); + } + + [Test] + public void DeepInstance3() + { + Registry registry = new Registry(); + registry.BuildInstancesOf<IWidget>().TheDefaultIs( + Registry.Object(new ColorWidget("yellow")) + ); + + registry.BuildInstancesOf<Rule>().TheDefaultIsConcreteType<WidgetRule>(); + + registry.BuildInstancesOf<Thing>().TheDefaultIs( + Registry.Instance<Thing>() + .UsingConcreteType<Thing>() + .WithProperty("average").EqualTo(.333) + .WithProperty("name").EqualTo("Jeremy") + .WithProperty("count").EqualTo(4) + ); + + assertThingMatches(registry); + } + + + [Test] + public void DeepInstance4() + { + Registry registry = new Registry(); + registry.BuildInstancesOf<IWidget>().TheDefaultIs( + Registry.Prototype(new ColorWidget("yellow")) + ); + + registry.BuildInstancesOf<Rule>().TheDefaultIsConcreteType<WidgetRule>(); + + registry.BuildInstancesOf<Thing>().TheDefaultIs( + Registry.Instance<Thing>() + .UsingConcreteType<Thing>() + .WithProperty("average").EqualTo(.333) + .WithProperty("name").EqualTo("Jeremy") + .WithProperty("count").EqualTo(4) + ); + + assertThingMatches(registry); + } + + + + [Test] + public void DeepInstance5() + { + Registry registry = new Registry(); + + registry.AddInstanceOf<IWidget>() + .UsingConcreteType<ColorWidget>() + .WithName("Yellow") + .WithProperty("Color").EqualTo("yellow"); + + registry.AddInstanceOf<Rule>() + .UsingConcreteType<WidgetRule>() + .WithName("TheWidgetRule") + .Child<IWidget>().IsNamedInstance("Yellow"); + + registry.BuildInstancesOf<Thing>().TheDefaultIs( + Registry.Instance<Thing>() + .UsingConcreteType<Thing>() + .WithProperty("average").EqualTo(.333) + .WithProperty("name").EqualTo("Jeremy") + .WithProperty("count").EqualTo(4) + .Child<Rule>().IsNamedInstance("TheWidgetRule") + ); + + assertThingMatches(registry); + } + + } + + public class Thing + { + private int _count; + private string _name; + private double _average; + private Rule _rule; + + + public Thing(int count, string name, double average, Rule rule) + { + _count = count; + _name = name; + _average = average; + _rule = rule; + } + + + public override bool Equals(object obj) + { + if (this == obj) return true; + Thing thing = obj as Thing; + if (thing == null) return false; + if (_count != thing._count) return false; + if (!Equals(_name, thing._name)) return false; + if (_average != thing._average) return false; + if (!Equals(_rule, thing._rule)) return false; + return true; + } + + public override int GetHashCode() + { + int result = _count; + result = 29*result + (_name != null ? _name.GetHashCode() : 0); + result = 29*result + _average.GetHashCode(); + result = 29*result + (_rule != null ? _rule.GetHashCode() : 0); + return result; + } + } +} Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/InstanceExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/InstanceExpressionTester.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/InstanceExpressionTester.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -25,9 +25,7 @@ [Test, ExpectedException(typeof(StructureMapException))] public void BlowUpIfNoPropertyIsFoundForType() { - InstanceExpression expression = new InstanceExpression(typeof(IWidget)); - expression.UsingConcreteType<AWidget>(); - expression.Child<Rule>(); + Registry.Instance<IWidget>().UsingConcreteType<AWidget>().Child<Rule>(); } } } Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2007-03-26 14:31:28 UTC (rev 30) @@ -185,8 +185,10 @@ <SubType>Code</SubType> </Compile> <Compile Include="Configuration\DSL\AddInstanceTester.cs" /> + <Compile Include="Configuration\DSL\ChildInstanceExpressionTester.cs" /> <Compile Include="Configuration\DSL\CreatePluginFamilyTester.cs" /> <Compile Include="Configuration\DSL\CreateProfileTester.cs" /> + <Compile Include="Configuration\DSL\DeepInstanceTester.cs" /> <Compile Include="Configuration\DSL\InstanceExpressionTester.cs" /> <Compile Include="Configuration\DSL\LiteralExpressionTester.cs" /> <Compile Include="Configuration\DSL\ProfileExpressionTester.cs" /> Modified: trunk/Source/StructureMap.Testing.Widget/IWidget.cs =================================================================== --- trunk/Source/StructureMap.Testing.Widget/IWidget.cs 2007-03-26 01:52:39 UTC (rev 29) +++ trunk/Source/StructureMap.Testing.Widget/IWidget.cs 2007-03-26 14:31:28 UTC (rev 30) @@ -8,7 +8,7 @@ } [Pluggable("Color", "Only for testing")] - public class ColorWidget : IWidget + public class ColorWidget : IWidget, ICloneable { private string _Color; @@ -31,6 +31,24 @@ } #endregion + + public override bool Equals(object obj) + { + if (this == obj) return true; + ColorWidget colorWidget = obj as ColorWidget; + if (colorWidget == null) return false; + return Equals(_Color, colorWidget._Color); + } + + public override int GetHashCode() + { + return _Color != null ? _Color.GetHashCode() : 0; + } + + public object Clone() + { + return this.MemberwiseClone(); + } } [Pluggable("AWidget")] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |