|
From: <jer...@us...> - 2008-04-11 23:32:30
|
Revision: 79
http://structuremap.svn.sourceforge.net/structuremap/?rev=79&view=rev
Author: jeremydmiller
Date: 2008-04-11 16:32:28 -0700 (Fri, 11 Apr 2008)
Log Message:
-----------
The Great Refactoring of 2008. Generics are temporarily broken however
Added Paths:
-----------
trunk/Source/StructureMap/InstanceBuilderList.cs
trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs
trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs
trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs
trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs
trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs
trunk/Source/StructureMap.Testing/Pipeline/StubInstanceCreator.cs
Added: trunk/Source/StructureMap/InstanceBuilderList.cs
===================================================================
--- trunk/Source/StructureMap/InstanceBuilderList.cs (rev 0)
+++ trunk/Source/StructureMap/InstanceBuilderList.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,90 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using StructureMap.Emitting;
+using StructureMap.Graph;
+
+namespace StructureMap
+{
+ public class InstanceBuilderList
+ {
+ private readonly Dictionary<Type, InstanceBuilder> _builders = new Dictionary<Type, InstanceBuilder>();
+ private Type _pluginType;
+
+
+ public InstanceBuilderList(Type pluginType, Plugin[] plugins)
+ {
+ _pluginType = pluginType;
+ processPlugins(plugins);
+ }
+
+
+ public InstanceBuilder FindByType(Type pluggedType)
+ {
+ if (_builders.ContainsKey(pluggedType))
+ {
+ return _builders[pluggedType];
+ }
+
+ // Add a missing PluggedType if we can
+ if (Plugin.CanBeCast(_pluginType, pluggedType))
+ {
+ Plugin plugin = Plugin.CreateImplicitPlugin(pluggedType);
+ processPlugins(new Plugin[]{plugin});
+
+ return _builders[pluggedType];
+ }
+
+ return null;
+ }
+
+ public InstanceBuilder FindByConcreteKey(string concreteKey)
+ {
+ foreach (KeyValuePair<Type, InstanceBuilder> pair in _builders)
+ {
+ if (pair.Value.ConcreteTypeKey == concreteKey)
+ {
+ return pair.Value;
+ }
+ }
+
+ return null;
+ }
+
+ public void Add(Type pluggedType, InstanceBuilder builder)
+ {
+ _builders.Add(pluggedType, builder);
+ }
+
+ private void processPlugins(IEnumerable<Plugin> plugins)
+ {
+ Assembly assembly = createInstanceBuilderAssembly(plugins);
+ foreach (Plugin plugin in plugins)
+ {
+ addPlugin(assembly, plugin);
+ }
+ }
+
+ private Assembly createInstanceBuilderAssembly(IEnumerable<Plugin> plugins)
+ {
+ string assemblyName = Guid.NewGuid().ToString().Replace(".", "") + "InstanceBuilderAssembly";
+ InstanceBuilderAssembly builderAssembly = new InstanceBuilderAssembly(assemblyName, _pluginType);
+
+ foreach (Plugin plugin in plugins)
+ {
+ builderAssembly.AddPlugin(plugin);
+ }
+
+ return builderAssembly.Compile();
+ }
+
+
+ private void addPlugin(Assembly assembly, Plugin plugin)
+ {
+ string instanceBuilderClassName = plugin.GetInstanceBuilderClassName();
+ InstanceBuilder builder = (InstanceBuilder)assembly.CreateInstance(instanceBuilderClassName);
+
+ _builders.Add(plugin.PluggedType, builder);
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs
===================================================================
--- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs (rev 0)
+++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,33 @@
+using System;
+
+namespace StructureMap.Pipeline
+{
+ public delegate object BuildObjectDelegate();
+
+ public class ConstructorInstance : ExpressedInstance<ConstructorInstance>
+ {
+ private BuildObjectDelegate _builder;
+
+
+ public ConstructorInstance(BuildObjectDelegate builder)
+ {
+ _builder = builder;
+ }
+
+ public BuildObjectDelegate Builder
+ {
+ get { return _builder; }
+ set { _builder = value; }
+ }
+
+ protected override ConstructorInstance thisInstance
+ {
+ get { return this; }
+ }
+
+ protected override object build(Type pluginType, IInstanceCreator creator)
+ {
+ return _builder();
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs
===================================================================
--- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs (rev 0)
+++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,18 @@
+namespace StructureMap.Pipeline
+{
+ public interface IConfiguredInstance
+ {
+ Instance[] GetChildrenArray(string propertyName);
+ string GetProperty(string propertyName);
+ object GetChild(string propertyName, string typeName, IInstanceCreator instanceCreator);
+ InstanceBuilder FindBuilder(InstanceBuilderList builders);
+
+ string ConcreteKey
+ {
+ get;
+ set;
+ }
+
+ string Name { get;}
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs
===================================================================
--- trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs (rev 0)
+++ trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,54 @@
+using System;
+using StructureMap.Graph;
+
+namespace StructureMap.Pipeline
+{
+ public class InstanceMementoPropertyReader : IPluginArgumentVisitor
+ {
+ private readonly ConfiguredInstance _instance;
+ private readonly InstanceMemento _memento;
+ private readonly PluginGraph _pluginGraph;
+ private readonly Type _pluginType;
+
+ public InstanceMementoPropertyReader(ConfiguredInstance instance, InstanceMemento memento, PluginGraph pluginGraph, Type pluginType)
+ {
+ _instance = instance;
+ _memento = memento;
+ _pluginGraph = pluginGraph;
+ _pluginType = pluginType;
+ }
+
+ public void Primitive(string name)
+ {
+ _instance.SetProperty(name, _memento.GetProperty(name));
+ }
+
+ public void Child(string name, Type childType)
+ {
+ InstanceMemento child = _memento.GetChildMemento(name);
+
+ Instance childInstance = child == null ? new DefaultInstance() : child.ReadInstance(_pluginGraph, childType);
+ _instance.SetChild(name, childInstance);
+ }
+
+ public void ChildArray(string name, Type childType)
+ {
+ InstanceMemento[] mementoes = _memento.GetChildrenArray(name);
+
+ // TODO -- want to default to mementoes == null is all
+ if (mementoes == null)
+ {
+ mementoes = new InstanceMemento[0];
+ }
+
+ Instance[] children = new Instance[mementoes.Length];
+ for (int i = 0; i < mementoes.Length; i++)
+ {
+ InstanceMemento memento = mementoes[i];
+ children[i] = memento.ReadInstance(_pluginGraph, childType);
+ }
+
+ _instance.SetChildArray(name, children);
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/InstanceMementoInstanceCreationTester.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,262 @@
+using System.Drawing;
+using NUnit.Framework;
+using StructureMap.Configuration;
+using StructureMap.Configuration.Mementos;
+using StructureMap.Graph;
+using StructureMap.Pipeline;
+using StructureMap.Testing.Widget3;
+
+namespace StructureMap.Testing
+{
+ [TestFixture]
+ public class InstanceMementoInstanceCreationTester
+ {
+ private PluginGraph _graph;
+
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ _graph = new PluginGraph();
+ PluginFamily family = _graph.PluginFamilies.Add(typeof (IService));
+ family.Plugins.Add(typeof(ColorService), "Color");
+
+ _graph.PluginFamilies.Add(typeof (Rule));
+ }
+
+ #endregion
+
+ public class Rule
+ {
+ }
+
+ public class ComplexRule : Rule
+ {
+ private readonly Color _color;
+ private bool _Bool;
+ private byte _Byte;
+ private double _Double;
+ private int _Int;
+ private long _Long;
+ private string _String;
+
+
+ [DefaultConstructor]
+ public ComplexRule(string String, Color color, int Int, long Long, byte Byte, double Double, bool Bool,
+ IAutomobile car, IAutomobile[] cars)
+ {
+ _String = String;
+ _color = color;
+ _Int = Int;
+ _Long = Long;
+ _Byte = Byte;
+ _Double = Double;
+ _Bool = Bool;
+ }
+
+ /// <summary>
+ /// Plugin should find the constructor above, not the "greedy" one below.
+ /// </summary>
+ /// <param name="String"></param>
+ /// <param name="String2"></param>
+ /// <param name="Int"></param>
+ /// <param name="Long"></param>
+ /// <param name="Byte"></param>
+ /// <param name="Double"></param>
+ /// <param name="Bool"></param>
+ /// <param name="extra"></param>
+ public ComplexRule(string String, string String2, int Int, long Long, byte Byte, double Double, bool Bool,
+ string extra)
+ {
+ }
+
+ public string String
+ {
+ get { return _String; }
+ }
+
+
+ public int Int
+ {
+ get { return _Int; }
+ }
+
+ public byte Byte
+ {
+ get { return _Byte; }
+ }
+
+ public long Long
+ {
+ get { return _Long; }
+ }
+
+ public double Double
+ {
+ get { return _Double; }
+ }
+
+ public bool Bool
+ {
+ get { return _Bool; }
+ }
+
+ public static MemoryInstanceMemento GetMemento()
+ {
+ MemoryInstanceMemento memento = new MemoryInstanceMemento("", "Sample");
+ memento.SetProperty("String", "Red");
+ memento.SetProperty("color", "Green");
+ memento.SetProperty("Int", "1");
+ memento.SetProperty("Long", "2");
+ memento.SetProperty("Byte", "3");
+ memento.SetProperty("Double", "4");
+ memento.SetProperty("Bool", "true");
+
+ return memento;
+ }
+ }
+
+
+ public interface IAutomobile
+ {
+ }
+
+ public class GrandPrix : IAutomobile
+ {
+ private readonly string _color;
+ private readonly int _horsePower;
+
+ public GrandPrix(int horsePower, string color)
+ {
+ _horsePower = horsePower;
+ _color = color;
+ }
+ }
+
+ public class Mustang : IAutomobile
+ {
+ public Mustang()
+ {
+ }
+ }
+
+ [Test]
+ public void Create_a_default_instance()
+ {
+ MemoryInstanceMemento memento = MemoryInstanceMemento.CreateDefaultInstanceMemento();
+ Instance instance = memento.ReadInstance(null, null);
+
+ Assert.IsInstanceOfType(typeof (DefaultInstance), instance);
+ }
+
+ [Test]
+ public void Create_a_referenced_instance()
+ {
+ MemoryInstanceMemento memento = MemoryInstanceMemento.CreateReferencedInstanceMemento("blue");
+ ReferencedInstance instance = (ReferencedInstance) memento.ReadInstance(null, null);
+
+ Assert.AreEqual("blue", instance.ReferenceKey);
+ }
+
+
+ [Test]
+ public void Get_the_instance_name()
+ {
+ MemoryInstanceMemento memento = new MemoryInstanceMemento("Color", "Red");
+ memento.SetProperty("Color", "Red");
+ memento.InstanceKey = "Red";
+
+ Assert.AreEqual("Red", memento.ReadInstance(_graph, typeof(IService)).Name);
+ }
+
+ [Test]
+ public void ReadChildArrayProperty()
+ {
+ PluginGraph graph = new PluginGraph();
+ Plugin plugin = Plugin.CreateImplicitPlugin(typeof (ComplexRule));
+
+ graph.PluginFamilies.Add(typeof (Rule)).Plugins.Add(plugin);
+
+ MemoryInstanceMemento memento = ComplexRule.GetMemento();
+ memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);
+ memento.AddChildArray("cars", new InstanceMemento[]
+ {
+ MemoryInstanceMemento.CreateReferencedInstanceMemento("Ford"),
+ MemoryInstanceMemento.CreateReferencedInstanceMemento("Chevy"),
+ MemoryInstanceMemento.CreateReferencedInstanceMemento("Dodge"),
+ });
+
+ ConfiguredInstance instance = (ConfiguredInstance)memento.ReadInstance(graph, typeof(Rule));
+ Instance[] instances = instance.GetChildArray("cars");
+ Assert.AreEqual(3, instances.Length);
+
+ assertIsReference(instances[0], "Ford");
+ assertIsReference(instances[1], "Chevy");
+ assertIsReference(instances[2], "Dodge");
+
+ }
+
+ private void assertIsReference(Instance instance, string referenceKey)
+ {
+ ReferencedInstance referencedInstance = (ReferencedInstance) instance;
+ Assert.AreEqual(referenceKey, referencedInstance.ReferenceKey);
+ }
+
+ [Test]
+ public void ReadChildProperty_child_property_is_defined_build_child()
+ {
+ PluginGraph graph = new PluginGraph();
+ Plugin plugin = Plugin.CreateImplicitPlugin(typeof (ComplexRule));
+
+ graph.PluginFamilies.Add(typeof (Rule)).Plugins.Add(plugin);
+
+ MemoryInstanceMemento memento = ComplexRule.GetMemento();
+ memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);
+ MemoryInstanceMemento carMemento = MemoryInstanceMemento.CreateReferencedInstanceMemento("GrandPrix");
+ memento.AddChild("car", carMemento);
+
+ ConfiguredInstance instance = (ConfiguredInstance) memento.ReadInstance(graph, typeof (Rule));
+ ReferencedInstance child = (ReferencedInstance) instance.GetChild("car");
+
+ Assert.AreEqual("GrandPrix", child.ReferenceKey);
+ }
+
+ [Test]
+ public void ReadChildProperty_child_property_is_not_defined_so_use_default()
+ {
+ PluginGraph graph = new PluginGraph();
+ Plugin plugin = Plugin.CreateImplicitPlugin(typeof (ComplexRule));
+
+ graph.PluginFamilies.Add(typeof (Rule)).Plugins.Add(plugin);
+
+ MemoryInstanceMemento memento = ComplexRule.GetMemento();
+ memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);
+
+ ConfiguredInstance instance = (ConfiguredInstance) memento.ReadInstance(graph, typeof (Rule));
+ Assert.IsInstanceOfType(typeof (DefaultInstance), instance.GetChild("car"));
+ }
+
+ [Test]
+ public void ReadPrimitivePropertiesHappyPath()
+ {
+ PluginGraph graph = new PluginGraph();
+ Plugin plugin = Plugin.CreateImplicitPlugin(typeof (ComplexRule));
+
+ graph.PluginFamilies.Add(typeof (Rule)).Plugins.Add(plugin);
+
+ MemoryInstanceMemento memento = ComplexRule.GetMemento();
+ memento.SetProperty(XmlConstants.PLUGGED_TYPE, typeof (ComplexRule).AssemblyQualifiedName);
+
+ IConfiguredInstance instance = (IConfiguredInstance) memento.ReadInstance(graph, typeof (Rule));
+
+ Assert.AreEqual(memento.GetProperty("String"), instance.GetProperty("String"));
+ Assert.AreEqual(memento.GetProperty("color"), instance.GetProperty("color"));
+ Assert.AreEqual(memento.GetProperty("Int"), instance.GetProperty("Int"));
+ Assert.AreEqual(memento.GetProperty("Long"), instance.GetProperty("Long"));
+ Assert.AreEqual(memento.GetProperty("Byte"), instance.GetProperty("Byte"));
+ Assert.AreEqual(memento.GetProperty("Double"), instance.GetProperty("Double"));
+ Assert.AreEqual(memento.GetProperty("Bool"), instance.GetProperty("Bool"));
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Pipeline/ConfiguredInstanceTester.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,48 @@
+using NUnit.Framework;
+using Rhino.Mocks;
+using StructureMap.Pipeline;
+
+namespace StructureMap.Testing.Pipeline
+{
+ [TestFixture]
+ public class ConfiguredInstanceTester
+ {
+ private ConfiguredInstance instance;
+
+ [SetUp]
+ public void SetUp()
+ {
+ instance = new ConfiguredInstance();
+ }
+
+ [Test]
+ public void GetProperty_happy_path()
+ {
+ instance.SetProperty("Color", "Red")
+ .SetProperty("Age", "34");
+
+ IConfiguredInstance configuredInstance = instance;
+
+ Assert.AreEqual("Red", configuredInstance.GetProperty("Color"));
+ Assert.AreEqual("34", configuredInstance.GetProperty("Age"));
+
+ instance.SetProperty("Color", "Blue");
+ Assert.AreEqual("Blue", configuredInstance.GetProperty("Color"));
+ }
+
+ [Test]
+ public void Property_cannot_be_found_so_throw_205()
+ {
+ try
+ {
+ IConfiguredInstance configuredInstance = instance;
+ configuredInstance.GetProperty("anything");
+ Assert.Fail("Did not throw exception");
+ }
+ catch (StructureMapException ex)
+ {
+ Assert.AreEqual(205, ex.ErrorCode);
+ }
+ }
+ }
+}
Added: trunk/Source/StructureMap.Testing/Pipeline/StubInstanceCreator.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Pipeline/StubInstanceCreator.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Pipeline/StubInstanceCreator.cs 2008-04-11 23:32:28 UTC (rev 79)
@@ -0,0 +1,57 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using StructureMap.Interceptors;
+using StructureMap.Pipeline;
+
+namespace StructureMap.Testing.Pipeline
+{
+ public class StubInstanceCreator : StructureMap.Pipeline.IInstanceCreator
+ {
+ public object CreateInstance(Type type, string referenceKey)
+ {
+ throw new NotImplementedException();
+ }
+
+ public Array CreateInstanceArray(string pluginTypeName, Instance[] instances)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object CreateInstance(string typeName, IConfiguredInstance instance)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object CreateInstance(string typeName)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object CreateInstance(Type pluginType)
+ {
+ throw new NotImplementedException();
+ }
+
+ public object CreateInstance(Type pluginType, IConfiguredInstance instance)
+ {
+ throw new NotImplementedException();
+ }
+
+ public InstanceBuilder FindInstanceBuilder(Type pluginType, string concreteKey)
+ {
+ throw new NotImplementedException();
+ }
+
+ public InstanceBuilder FindInstanceBuilder(Type pluginType, Type pluggedType)
+ {
+ throw new NotImplementedException();
+ }
+
+
+ public object ApplyInterception(Type pluginType, object actualValue)
+ {
+ return actualValue;
+ }
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|