From: <jer...@us...> - 2008-05-27 13:25:34
|
Revision: 100 http://structuremap.svn.sourceforge.net/structuremap/?rev=100&view=rev Author: jeremydmiller Date: 2008-05-27 06:25:28 -0700 (Tue, 27 May 2008) Log Message: ----------- Explicit argument passing cleanup, returning an array of all possibles when not totally defined Modified Paths: -------------- trunk/Source/HTML/HTML.csproj trunk/Source/StructureMap/BuildSession.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/SetterProperty.cs trunk/Source/StructureMap/Graph/TypeRules.cs trunk/Source/StructureMap/IInstanceManager.cs trunk/Source/StructureMap/InstanceManager.cs trunk/Source/StructureMap/ObjectFactory.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/BuildSessionTester.cs trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs trunk/Source/StructureMap.Testing/Graph/PluginTester.cs trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs Added Paths: ----------- trunk/Source/HTML/ExplicitArguments.htm trunk/Source/StructureMap/ExplicitArgsExpression.cs Property Changed: ---------------- trunk/Source/HTML/ Property changes on: trunk/Source/HTML ___________________________________________________________________ Name: svn:ignore + bin obj Added: trunk/Source/HTML/ExplicitArguments.htm =================================================================== --- trunk/Source/HTML/ExplicitArguments.htm (rev 0) +++ trunk/Source/HTML/ExplicitArguments.htm 2008-05-27 13:25:28 UTC (rev 100) @@ -0,0 +1,19 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> +<html> + <head> + <title></title> + </head> + <body> + <h1> + Explicit Arguments</h1> + <p> + A new feature in StructureMap 2.5 is the ability to pass in arguments to ObjectFactory.</p> + <p> + </p> + <h4> + Primitive Value</h4> + <p> + </p> + + </body> +</html> \ No newline at end of file Modified: trunk/Source/HTML/HTML.csproj =================================================================== --- trunk/Source/HTML/HTML.csproj 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/HTML/HTML.csproj 2008-05-27 13:25:28 UTC (rev 100) @@ -36,4 +36,7 @@ <Target Name="AfterBuild"> </Target> --> + <ItemGroup> + <Content Include="ExplicitArguments.htm" /> + </ItemGroup> </Project> \ No newline at end of file Modified: trunk/Source/StructureMap/BuildSession.cs =================================================================== --- trunk/Source/StructureMap/BuildSession.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/BuildSession.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using StructureMap.Graph; using StructureMap.Interceptors; @@ -58,20 +59,28 @@ public Array CreateInstanceArray(Type pluginType, Instance[] instances) { - // TODO -- default to returning all + Array array; + if (instances == null) { - throw new StructureMapException(205, pluginType, "UNKNOWN"); + IList list = forType(pluginType).GetAllInstances(this); + array = Array.CreateInstance(pluginType, list.Count); + for (int i = 0; i < list.Count; i++) + { + array.SetValue(list[i], i); + } } - - // TODO: 3.5, move this to an extension method of Array? - Array array = Array.CreateInstance(pluginType, instances.Length); - for (int i = 0; i < instances.Length; i++) + else { - Instance instance = instances[i]; + // TODO: 3.5, move this to an extension method of Array? + array = Array.CreateInstance(pluginType, instances.Length); + for (int i = 0; i < instances.Length; i++) + { + Instance instance = instances[i]; - object arrayValue = forType(pluginType).Build(this, instance); - array.SetValue(arrayValue, i); + object arrayValue = forType(pluginType).Build(this, instance); + array.SetValue(arrayValue, i); + } } return array; Added: trunk/Source/StructureMap/ExplicitArgsExpression.cs =================================================================== --- trunk/Source/StructureMap/ExplicitArgsExpression.cs (rev 0) +++ trunk/Source/StructureMap/ExplicitArgsExpression.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -0,0 +1,49 @@ +using StructureMap.Pipeline; + +namespace StructureMap +{ + public interface IExplicitProperty + { + ExplicitArgsExpression EqualTo(object value); + } + + public class ExplicitArgsExpression : IExplicitProperty + { + private readonly ExplicitArguments _args = new ExplicitArguments(); + private readonly IInstanceManager _manager; + private string _lastArgName; + + internal ExplicitArgsExpression(IInstanceManager manager) + { + _manager = manager; + } + + #region IExplicitProperty Members + + ExplicitArgsExpression IExplicitProperty.EqualTo(object value) + { + _args.SetArg(_lastArgName, value); + return this; + } + + #endregion + + public ExplicitArgsExpression With<T>(T arg) + { + _args.Set<T>(arg); + return this; + } + + public IExplicitProperty With(string argName) + { + _lastArgName = argName; + return this; + } + + + public T GetInstance<T>() + { + return _manager.CreateInstance<T>(_args); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -66,7 +66,7 @@ { foreach (ParameterInfo parameter in _ctor.GetParameters()) { - if (!IsChild(parameter.ParameterType)) + if (!IsAutoFillable(parameter.ParameterType)) { return false; } Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -140,7 +140,8 @@ _parent.Log.Try(delegate() { diagnosticInstance.Preprocess(this); - }).AndReportErrorAs(104, diagnosticInstance.CreateToken(), _pluginType); + }) + .AndReportErrorAs(104, diagnosticInstance.CreateToken(), _pluginType); if (!diagnosticInstance.CanBePartOfPluginFamily(this)) Modified: trunk/Source/StructureMap/Graph/SetterProperty.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterProperty.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/Graph/SetterProperty.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -27,7 +27,7 @@ public bool CanBeAutoFilled { - get { return IsChild(_property.PropertyType); } + get { return IsAutoFillable(_property.PropertyType); } } public void Visit(IArgumentVisitor visitor) Modified: trunk/Source/StructureMap/Graph/TypeRules.cs =================================================================== --- trunk/Source/StructureMap/Graph/TypeRules.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/Graph/TypeRules.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -89,5 +89,11 @@ { return !type.IsInterface && !type.IsAbstract; } + + + protected bool IsAutoFillable(Type type) + { + return IsChild(type) || IsChildArray(type); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/IInstanceManager.cs =================================================================== --- trunk/Source/StructureMap/IInstanceManager.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/IInstanceManager.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -75,6 +75,8 @@ object CreateInstance(Type pluginType, string instanceKey); PLUGINTYPE CreateInstance<PLUGINTYPE>(ExplicitArguments args); - + + ExplicitArgsExpression With<T>(T arg); + IExplicitProperty With(string argName); } } \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceManager.cs =================================================================== --- trunk/Source/StructureMap/InstanceManager.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/InstanceManager.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -248,6 +248,16 @@ return writer.GetText(); } + public ExplicitArgsExpression With<T>(T arg) + { + return new ExplicitArgsExpression(this).With<T>(arg); + } + + public IExplicitProperty With(string argName) + { + return new ExplicitArgsExpression(this).With(argName); + } + #endregion private IBuildSession withNewSession() @@ -260,5 +270,7 @@ { return _pipelineGraph.ForType(type); } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/ObjectFactory.cs =================================================================== --- trunk/Source/StructureMap/ObjectFactory.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/ObjectFactory.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -238,7 +238,6 @@ #endregion - #region GetInstance /// <summary> /// Returns the default instance of the requested System.Type @@ -347,67 +346,16 @@ public static ExplicitArgsExpression With<T>(T arg) { - return new ExplicitArgsExpression(manager).With<T>(arg); + return manager.With(arg); } public static IExplicitProperty With(string argName) { - return new ExplicitArgsExpression(manager).With(argName); + return manager.With(argName); } - #region Nested type: ExplicitArgsExpression - public class ExplicitArgsExpression : IExplicitProperty - { - private readonly ExplicitArguments _args = new ExplicitArguments(); - private readonly IInstanceManager _manager; - private string _lastArgName; + } - internal ExplicitArgsExpression(IInstanceManager manager) - { - _manager = manager; - } - #region IExplicitProperty Members - - ExplicitArgsExpression IExplicitProperty.EqualTo(object value) - { - _args.SetArg(_lastArgName, value); - return this; - } - - #endregion - - public ExplicitArgsExpression With<T>(T arg) - { - _args.Set<T>(arg); - return this; - } - - public IExplicitProperty With(string argName) - { - _lastArgName = argName; - return this; - } - - - public T GetInstance<T>() - { - return _manager.CreateInstance<T>(_args); - } - } - - #endregion - - #region Nested type: IExplicitProperty - - public interface IExplicitProperty - { - ExplicitArgsExpression EqualTo(object value); - } - - #endregion - - #endregion - } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -62,8 +62,12 @@ Instance[] IConfiguredInstance.GetChildrenArray(string propertyName) { - // TODO: Validate and throw exception if missing - return _arrays[propertyName]; + if (_arrays.ContainsKey(propertyName)) + { + return _arrays[propertyName]; + } + + return null; } string IConfiguredInstance.GetProperty(string propertyName) Modified: trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/Pipeline/ExplicitArguments.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -5,9 +5,18 @@ { public class ExplicitArguments { - private readonly Dictionary<string, string> _args = new Dictionary<string, string>(); + private readonly Dictionary<string, object> _args; private readonly Dictionary<Type, object> _children = new Dictionary<Type, object>(); + public ExplicitArguments(Dictionary<string, object> args) + { + _args = args; + } + + public ExplicitArguments() : this(new Dictionary<string, object>()) + { + } + public T Get<T>() where T : class { return (T) Get(typeof (T)); @@ -25,19 +34,19 @@ public void SetArg(string key, object argValue) { - _args.Add(key, argValue.ToString()); + _args.Add(key, argValue); } - public string GetArg(string key) + public object GetArg(string key) { return _args.ContainsKey(key) ? _args[key] : null; } public void Configure(ConfiguredInstance instance) { - foreach (KeyValuePair<string, string> arg in _args) + foreach (KeyValuePair<string, object> arg in _args) { - instance.SetProperty(arg.Key, arg.Value); + instance.SetProperty(arg.Key, arg.Value.ToString()); instance.SetChild(arg.Key, new LiteralInstance(arg.Value)); } } Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 13:25:28 UTC (rev 100) @@ -131,6 +131,7 @@ <Compile Include="Diagnostics\WhatDoIHaveWriter.cs" /> <Compile Include="Emitting\ArgumentEmitter.cs" /> <Compile Include="Exceptions\StructureMapConfigurationException.cs" /> + <Compile Include="ExplicitArgsExpression.cs" /> <Compile Include="Graph\Constructor.cs" /> <Compile Include="Graph\IArgumentVisitor.cs" /> <Compile Include="Graph\IPluginFamily.cs" /> Modified: trunk/Source/StructureMap.Testing/BuildSessionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -134,5 +134,34 @@ Assert.AreNotSame(result1, result3); Assert.AreSame(result3, result4); } + + [Test] + public void If_no_child_array_is_explicitly_defined_return_all_instances() + { + Registry registry = new Registry(); + registry.AddInstanceOf<IWidget>(new ColorWidget("Red")); + registry.AddInstanceOf<IWidget>(new ColorWidget("Blue")); + registry.AddInstanceOf<IWidget>(new ColorWidget("Green")); + + IInstanceManager manager = registry.BuildInstanceManager(); + + WidgetHolder holder = manager.CreateInstance<WidgetHolder>(); + Assert.AreEqual(3, holder.Widgets.Length); + } + + public class WidgetHolder + { + private readonly IWidget[] _widgets; + + public WidgetHolder(IWidget[] widgets) + { + _widgets = widgets; + } + + public IWidget[] Widgets + { + get { return _widgets; } + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -1,3 +1,4 @@ +using System.Collections.Generic; using NUnit.Framework; using StructureMap.Configuration.DSL; using StructureMap.Graph; @@ -2,2 +3,3 @@ using StructureMap.Pipeline; +using StructureMap.Testing.Pipeline; @@ -26,38 +28,7 @@ #endregion - public void GetTypedArgumentsFromAnExplicitArgumentsMementoIfThereIsAnExplicitArgument() - { - Assert.Fail("Redo"); - //PluginGraph pluginGraph = new PluginGraph(); - //using (Registry registry = new Registry(pluginGraph)) - //{ - // registry.ForRequestedType<ExplicitTarget>().TheDefaultIs( - // Registry.Instance<ExplicitTarget>() - // .UsingConcreteType<ExplicitTarget>() - // .Child<IProvider>().IsConcreteType<RedProvider>() - // .WithProperty("name").EqualTo("Jeremy") - // ); - //} - - //InstanceMemento inner = pluginGraph.PluginFamilies[typeof (ExplicitTarget)].Source.GetAllMementos()[0]; - //ExplicitArguments args = new ExplicitArguments(); - //ExplicitArgumentMemento memento = new ExplicitArgumentMemento(args, inner); - - //InstanceManager manager = new InstanceManager(pluginGraph); - - //// Get the ExplicitTarget without setting an explicit arg for IProvider - //ExplicitTarget firstTarget = manager.CreateInstance<ExplicitTarget>(memento); - //Assert.IsInstanceOfType(typeof (RedProvider), firstTarget.Provider); - - //// Now, set the explicit arg for IProvider - //args.Set<IProvider>(new BlueProvider()); - //ExplicitTarget secondTarget = manager.CreateInstance<ExplicitTarget>(memento); - //Assert.IsInstanceOfType(typeof (BlueProvider), secondTarget.Provider); - } - - public interface IExplicitTarget { } @@ -234,8 +205,62 @@ Assert.AreEqual("Jeremy", args.GetArg("name")); args.SetArg("age", 34); - Assert.AreEqual("34", args.GetArg("age")); + Assert.AreEqual(34, args.GetArg("age")); } + + [Test] + public void Fill_in_argument_by_name() + { + InstanceManager manager = new InstanceManager(); + manager.AddDefaultInstance<IView, View>(); + + Node theNode = new Node(); + Trade theTrade = new Trade(); + + Command command = manager + .With("node").EqualTo(theNode) + .With<Trade>(theTrade) + .GetInstance<Command>(); + + Assert.IsInstanceOfType(typeof(View), command.View); + Assert.AreSame(theNode, command.Node); + Assert.AreSame(theTrade, command.Trade); + } + + [Test] + public void Pass_in_arguments_as_dictionary() + { + InstanceManager manager = new InstanceManager(); + manager.AddDefaultInstance<IView, View>(); + + Node theNode = new Node(); + Trade theTrade = new Trade(); + + ExplicitArguments args = new ExplicitArguments(); + args.Set<Node>(theNode); + args.SetArg("trade", theTrade); + + Command command = manager.CreateInstance<Command>(args); + + Assert.IsInstanceOfType(typeof(View), command.View); + Assert.AreSame(theNode, command.Node); + Assert.AreSame(theTrade, command.Trade); + } + + [Test] + public void ExplicitArguments_can_return_child_by_name() + { + ExplicitArguments args = new ExplicitArguments(); + Node theNode = new Node(); + args.SetArg("node", theNode); + + IConfiguredInstance instance = new ExplicitInstance<Command>(args, null); + + Assert.AreSame(theNode, instance.GetChild("node", typeof(Node), new StubBuildSession())); + } + + + } public class Lump @@ -257,4 +282,42 @@ get { return _lump; } } } + + + public class Trade{} + public class Node{} + public interface IView{} + public class View : IView {} + + public class Command + { + private readonly Trade _trade; + private readonly Node _node; + private readonly IView _view; + + public Command(Trade trade, Node node, IView view) + { + _trade = trade; + _node = node; + _view = view; + } + + public Trade Trade + { + get { return _trade; } + } + + public Node Node + { + get { return _node; } + } + + public IView View + { + get { return _view; } + } + } + + + } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/PluginTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap.Testing/Graph/PluginTester.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -76,6 +76,41 @@ } [Test] + public void CanBeAutoFilled_with_child_array_in_ctor() + { + Constructor ctor = new Constructor(typeof(CanBeAutoFilledWithArray)); + Assert.IsTrue(ctor.CanBeAutoFilled()); + } + + public class CanBeAutoFilledWithArray + { + public CanBeAutoFilledWithArray(IWidget[] widgets) + { + + } + + [SetterProperty] + public IWidget[] More + { + get + { + return null; + } + set + { + + } + } + } + + [Test] + public void CanBeAutoFilled_with_child_array_in_setter() + { + SetterPropertyCollection setters = new SetterPropertyCollection(new Plugin(typeof(CanBeAutoFilledWithArray))); + Assert.IsTrue(setters.CanBeAutoFilled()); + } + + [Test] public void CanBeAutoFilledIsTrue() { Plugin plugin = new Plugin(typeof (Mustang)); Modified: trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2008-05-26 17:39:08 UTC (rev 99) +++ trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2008-05-27 13:25:28 UTC (rev 100) @@ -5,6 +5,7 @@ using StructureMap.Configuration; using StructureMap.Graph; using StructureMap.Testing.GenericWidgets; +using StructureMap.Testing.TestData; namespace StructureMap.Testing { @@ -16,6 +17,7 @@ [SetUp] public void SetUp() { + DataMother.RestoreStructureMapConfig(); ObjectFactory.ReInitialize(); StructureMapConfiguration.ResetAll(); } @@ -36,6 +38,37 @@ } [Test] + public void StructureMap_functions_without_StructureMapconfig_file_in_the_default_mode() + { + StructureMapConfiguration.ResetAll(); + DataMother.RemoveStructureMapConfig(); + + PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); + + } + + [Test] + public void Ignore_the_StructureMap_config_file_even_if_it_exists() + { + StructureMapConfiguration.ResetAll(); + StructureMapConfiguration.IgnoreStructureMapConfig = true; + + PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); + + Assert.AreEqual(0, graph.FamilyCount); + } + + [Test] + public void Use_the_StructureMap_config_file_if_it_exists() + { + StructureMapConfiguration.ResetAll(); + DataMother.RestoreStructureMapConfig(); + + PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); + Assert.IsTrue(graph.FamilyCount > 0); + } + + [Test] public void BuildPluginGraph() { PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |