From: <jer...@us...> - 2008-08-19 15:27:29
|
Revision: 141 http://structuremap.svn.sourceforge.net/structuremap/?rev=141&view=rev Author: jeremydmiller Date: 2008-08-19 15:27:23 +0000 (Tue, 19 Aug 2008) Log Message: ----------- SmartInstance!!! Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs trunk/Source/StructureMap/Graph/Constructor.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapConfiguration.cs trunk/Source/StructureMap.Testing/BuildSessionTester.cs trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs trunk/Source/StructureMap/Pipeline/PropertyExpression.cs trunk/Source/StructureMap/Pipeline/SmartInstance.cs trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -190,7 +190,7 @@ [Obsolete("Kill!")] public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIs(Func<PLUGINTYPE> func) { - ConstructorInstance instance = new ConstructorInstance(() => func()); + ConstructorInstance<PLUGINTYPE> instance = new ConstructorInstance<PLUGINTYPE>(func); return TheDefaultIs(instance); } @@ -204,7 +204,7 @@ [Obsolete("Kill!")] public CreatePluginFamilyExpression<PLUGINTYPE> AddInstance(Func<PLUGINTYPE> func) { - ConstructorInstance instance = new ConstructorInstance(() => func()); + ConstructorInstance<PLUGINTYPE> instance = new ConstructorInstance<PLUGINTYPE>(func); return AddInstance(instance); } Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -45,7 +45,7 @@ public GenericFamilyExpression TheDefaultIs(Func<object> func) { - ConstructorInstance instance = new ConstructorInstance(func); + ConstructorInstance<object> instance = new ConstructorInstance<object>(func); return TheDefaultIs(instance); } Added: trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/InstanceExpression.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -0,0 +1,46 @@ +using System; +using StructureMap.Pipeline; + +namespace StructureMap.Configuration.DSL.Expressions +{ + public interface IsExpression<T> + { + InstanceExpression<T> Is { get; } + } + + public class InstanceExpression<T> : IsExpression<T> + { + private readonly Action<Instance> _action; + + internal InstanceExpression(Action<Instance> action) + { + _action = action; + } + + public void Is(Instance instance) + { + _action(instance); + } + + private T returnInstance<T>(T instance) where T : Instance + { + Is(instance); + return instance; + } + + public SmartInstance<PLUGGEDTYPE> OfConcreteType<PLUGGEDTYPE>() where PLUGGEDTYPE : T + { + return returnInstance(new SmartInstance<PLUGGEDTYPE>()); + } + + public LiteralInstance Object(T theObject) + { + return returnInstance(new LiteralInstance(theObject)); + } + + InstanceExpression<T> IsExpression<T>.Is + { + get { return this; } + } + } +} Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/ProfileExpression.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -82,7 +82,7 @@ public ProfileExpression Use(Func<T> func) { - ConstructorInstance instance = new ConstructorInstance(delegate { return func(); }); + ConstructorInstance<T> instance = new ConstructorInstance<T>(func); return Use(instance); } Modified: trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -50,10 +50,10 @@ return new UserControlInstance(url); } - public static ConstructorInstance ConstructedBy<PLUGINTYPE> - (Func<PLUGINTYPE> builder) + public static ConstructorInstance<T> ConstructedBy<T> + (Func<T> builder) { - return new ConstructorInstance(() => builder()); + return new ConstructorInstance<T>(() => builder()); } public static ReferencedInstance Instance(string referencedKey) Modified: trunk/Source/StructureMap/Graph/Constructor.cs =================================================================== --- trunk/Source/StructureMap/Graph/Constructor.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Graph/Constructor.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -75,11 +75,11 @@ return true; } - public string FindFirstConstructorArgumentOfType<T>() + public string FindFirstConstructorArgumentOfType(Type type) { foreach (ParameterInfo info in _ctor.GetParameters()) { - if (info.ParameterType.Equals(typeof (T))) + if (info.ParameterType.Equals(type)) { return info.Name; } Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -82,13 +82,18 @@ public string FindArgumentNameForType<T>() { + return FindArgumentNameForType(typeof (T)); + } + + public string FindArgumentNameForType(Type type) + { string returnValue = - _constructor.FindFirstConstructorArgumentOfType<T>() ?? - _setters.FindFirstConstructorArgumentOfType<T>(); + _constructor.FindFirstConstructorArgumentOfType(type) ?? + _setters.FindFirstConstructorArgumentOfType(type); if (returnValue == null) { - throw new StructureMapException(302, typeof (T).FullName, _pluggedType.FullName); + throw new StructureMapException(302, type.FullName, _pluggedType.FullName); } return returnValue; Modified: trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Graph/SetterPropertyCollection.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -115,11 +115,11 @@ return returnValue; } - public string FindFirstConstructorArgumentOfType<T>() + public string FindFirstConstructorArgumentOfType(Type type) { foreach (SetterProperty setterProperty in this) { - if (setterProperty.Property.PropertyType.Equals(typeof (T))) + if (setterProperty.Property.PropertyType.Equals(type)) { return setterProperty.Name; } Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.Expressions.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -1,5 +1,4 @@ using System; -using System.Configuration; using StructureMap.Configuration.DSL; using StructureMap.Graph; @@ -72,9 +71,9 @@ /// </summary> /// <param name="propertyName"></param> /// <returns></returns> - public PropertyExpression WithProperty(string propertyName) + public PropertyExpression<ConfiguredInstance> WithProperty(string propertyName) { - return new PropertyExpression(this, propertyName); + return new PropertyExpression<ConfiguredInstance>(this, propertyName); } #region Nested type: ChildArrayExpression @@ -193,45 +192,6 @@ #region Nested type: PropertyExpression - /// <summary> - /// Defines the value of a primitive argument to a constructur argument - /// </summary> - public class PropertyExpression - { - private readonly ConfiguredInstance _instance; - private readonly string _propertyName; - - public PropertyExpression(ConfiguredInstance instance, string propertyName) - { - _instance = instance; - _propertyName = propertyName; - } - - /// <summary> - /// Sets the value of the constructor argument - /// </summary> - /// <param name="propertyValue"></param> - /// <returns></returns> - public ConfiguredInstance EqualTo(object propertyValue) - { - _instance.setProperty(_propertyName, propertyValue.ToString()); - return _instance; - } - - /// <summary> - /// Sets the value of the constructor argument to the key/value in the - /// AppSettings - /// </summary> - /// <param name="appSettingKey"></param> - /// <returns></returns> - public ConfiguredInstance EqualToAppSetting(string appSettingKey) - { - string propertyValue = ConfigurationManager.AppSettings[appSettingKey]; - _instance.setProperty(_propertyName, propertyValue); - return _instance; - } - } - #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -4,22 +4,21 @@ { - public class ConstructorInstance : ExpressedInstance<ConstructorInstance> + public class ConstructorInstance<T> : ExpressedInstance<ConstructorInstance<T>> { - private Func<object> _builder; + private readonly Func<BuildSession, T> _builder; - public ConstructorInstance(Func<object> builder) + public ConstructorInstance(Func<BuildSession, T> builder) { _builder = builder; } - public Func<object> Builder + public ConstructorInstance(Func<T> func) { - get { return _builder; } - set { _builder = value; } + _builder = s => func(); } - protected override ConstructorInstance thisInstance + protected override ConstructorInstance<T> thisInstance { get { return this; } } @@ -28,7 +27,7 @@ { try { - return _builder(); + return _builder(session); } catch (Exception ex) { Added: trunk/Source/StructureMap/Pipeline/PropertyExpression.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PropertyExpression.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/PropertyExpression.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -0,0 +1,43 @@ +using System.Configuration; + +namespace StructureMap.Pipeline +{ + /// <summary> + /// Defines the value of a primitive argument to a constructur argument + /// </summary> + public class PropertyExpression<T> where T : IConfiguredInstance + { + private readonly IConfiguredInstance _instance; + private readonly string _propertyName; + + public PropertyExpression(IConfiguredInstance instance, string propertyName) + { + _instance = instance; + _propertyName = propertyName; + } + + /// <summary> + /// Sets the value of the constructor argument + /// </summary> + /// <param name="propertyValue"></param> + /// <returns></returns> + public T EqualTo(object propertyValue) + { + _instance.SetProperty(_propertyName, propertyValue.ToString()); + return (T) _instance; + } + + /// <summary> + /// Sets the value of the constructor argument to the key/value in the + /// AppSettings + /// </summary> + /// <param name="appSettingKey"></param> + /// <returns></returns> + public T EqualToAppSetting(string appSettingKey) + { + string propertyValue = ConfigurationManager.AppSettings[appSettingKey]; + _instance.SetProperty(_propertyName, propertyValue); + return (T) _instance; + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/SmartInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SmartInstance.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/SmartInstance.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using StructureMap.Configuration.DSL.Expressions; +using StructureMap.Graph; + +namespace StructureMap.Pipeline +{ + public class SmartInstance<T> : ConfiguredInstanceBase<SmartInstance<T>> + { + private readonly List<Action<T>> _actions = new List<Action<T>>(); + + public SmartInstance() : base(typeof(T)) + { + } + + protected override SmartInstance<T> thisInstance + { + get { return this; } + } + + protected override string getDescription() + { + return "Smart Instance for " + getConcreteType().FullName; + } + + public PropertyExpression<SmartInstance<T>> WithCtorArg(string argumentName) + { + return new PropertyExpression<SmartInstance<T>>(this, argumentName); + } + + protected override object build(Type pluginType, BuildSession session) + { + T builtTarget = (T) base.build(pluginType, session); + foreach (Action<T> action in _actions) + { + action(builtTarget); + } + + return builtTarget; + } + + public SmartInstance<T> SetProperty(Action<T> action) + { + _actions.Add(action); + return this; + } + + public PropertyExpression<SmartInstance<T>> WithProperty(Expression<Func<T, object>> expression) + { + string propertyName = ReflectionHelper.GetProperty(expression).Name; + return WithProperty(propertyName); + } + + public PropertyExpression<SmartInstance<T>> WithProperty(string propertyName) + { + return new PropertyExpression<SmartInstance<T>>(this, propertyName); + } + + public DependencyExpression<T, CTORTYPE> CtorDependency<CTORTYPE>() + { + string constructorArg = getArgumentNameForType<CTORTYPE>(); + return CtorDependency<CTORTYPE>(constructorArg); + } + + private string getArgumentNameForType<CTORTYPE>() + { + Plugin plugin = PluginCache.GetPlugin(getConcreteType()); + return plugin.FindArgumentNameForType<CTORTYPE>(); + } + + public DependencyExpression<T, CTORTYPE> CtorDependency<CTORTYPE>(string constructorArg) + { + return new DependencyExpression<T, CTORTYPE>(this, constructorArg); + } + + public DependencyExpression<T, SETTERTYPE> SetterDependency<SETTERTYPE>(Expression<Func<T, SETTERTYPE>> expression) + { + string propertyName = ReflectionHelper.GetProperty(expression).Name; + return new DependencyExpression<T, SETTERTYPE>(this, propertyName); + } + + public ArrayDefinitionExpression<T, CHILD> TheArrayOf<CHILD>() + { + if (typeof(CHILD).IsArray) + { + throw new ApplicationException("Please specify the element type in the call to TheArrayOf"); + } + + Plugin plugin = PluginCache.GetPlugin(typeof (T)); + string propertyName = plugin.FindArgumentNameForType(typeof (CHILD).MakeArrayType()); + + return new ArrayDefinitionExpression<T, CHILD>(this, propertyName); + } + + public class ArrayDefinitionExpression<T, ARRAY> + { + private SmartInstance<T> _instance; + private string _propertyName; + + internal ArrayDefinitionExpression(SmartInstance<T> instance, string propertyName) + { + _instance = instance; + _propertyName = propertyName; + } + + public SmartInstance<T> Contains(Action<InstanceExpression<ARRAY>> action) + { + List<Instance> list = new List<Instance>(); + + InstanceExpression<ARRAY> child = new InstanceExpression<ARRAY>(i => list.Add(i)); + action(child); + + _instance.setChildArray(_propertyName, list.ToArray()); + + return _instance; + } + } + + public class DependencyExpression<T, CHILD> + { + private readonly SmartInstance<T> _instance; + private readonly string _propertyName; + + internal DependencyExpression(SmartInstance<T> instance, string propertyName) + { + _instance = instance; + _propertyName = propertyName; + } + + public SmartInstance<T> Is(Func<InstanceExpression<CHILD>, Instance> func) + { + var expression = new InstanceExpression<CHILD>(i => _instance.setChild(_propertyName, i)); + Instance instance = func(expression); + + + return _instance; + } + } + } + + +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-08-19 15:27:23 UTC (rev 141) @@ -390,6 +390,7 @@ </None> <None Include="ConfigurationClasses.cd" /> <Compile Include="Configuration\DictionaryReader.cs" /> + <Compile Include="Configuration\DSL\Expressions\InstanceExpression.cs" /> <Compile Include="Configuration\ITypeReader.cs" /> <Compile Include="Configuration\PrimitiveArrayReader.cs" /> <Compile Include="Configuration\TypeReaderFactory.cs" /> @@ -408,7 +409,9 @@ <Compile Include="Pipeline\BuildStack.cs" /> <Compile Include="Pipeline\ConfiguredInstanceBase.cs" /> <Compile Include="Pipeline\IStructuredInstance.cs" /> + <Compile Include="Pipeline\PropertyExpression.cs" /> <Compile Include="Pipeline\SerializedInstance.cs" /> + <Compile Include="Pipeline\SmartInstance.cs" /> <Compile Include="ReflectionHelper.cs" /> <Compile Include="Util\Cache.cs" /> </ItemGroup> Modified: trunk/Source/StructureMap/StructureMapConfiguration.cs =================================================================== --- trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -164,16 +164,19 @@ /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> + [Obsolete] public static Registry.ConfiguredInstanceExpression<T> AddInstanceOf<T>() { return _registry.AddInstanceOf<T>(); } + [Obsolete] public static void AddInstanceOf<T>(Func<T> func) { - _registry.AddInstanceOf<T>(new ConstructorInstance(() => func)); + _registry.AddInstanceOf<T>(new ConstructorInstance<T>(func)); } + [Obsolete] public static void AddInstanceOf<T>(Instance instance) { _registry.ForRequestedType<T>().AddInstance(instance); @@ -187,6 +190,7 @@ /// <typeparam name="T"></typeparam> /// <param name="target"></param> /// <returns></returns> + [Obsolete] public static LiteralInstance AddInstanceOf<T>(T target) { return _registry.AddInstanceOf(target); Modified: trunk/Source/StructureMap.Testing/BuildSessionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/BuildSessionTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -56,7 +56,7 @@ BuildSession session = new BuildSession(new PluginGraph()); BuildSession session2 = new BuildSession(new PluginGraph()); - ConstructorInstance instance = new ConstructorInstance(delegate + ConstructorInstance<ColorRule> instance = new ConstructorInstance<ColorRule>(() => { count++; return new ColorRule("Red"); @@ -94,7 +94,7 @@ int count = 0; BuildSession session = new BuildSession(new PluginGraph()); - ConstructorInstance instance = new ConstructorInstance(delegate + ConstructorInstance<ColorRule> instance = new ConstructorInstance<ColorRule>(() => { count++; return new ColorRule("Red"); @@ -117,7 +117,7 @@ { int count = 0; - ConstructorInstance instance = new ConstructorInstance(delegate + ConstructorInstance<ColorRule> instance = new ConstructorInstance<ColorRule>( () => { count++; return new ColorRule("Red"); Modified: trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/Diagnostics/ValidationBuildSessionTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -30,7 +30,7 @@ return session.BuildErrors[0]; } - private ConstructorInstance errorInstance() + private ConstructorInstance<IWidget> errorInstance() { return ConstructedBy<IWidget>(delegate { throw new NotSupportedException("You can't make me!"); }); } Modified: trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -106,10 +106,10 @@ public void Singleton_build_policy() { SingletonPolicy policy = new SingletonPolicy(); - ConstructorInstance instance1 = - new ConstructorInstance(delegate { return new ColorService("Red"); }).WithName("Red"); - ConstructorInstance instance2 = - new ConstructorInstance(delegate { return new ColorService("Green"); }).WithName("Green"); + ConstructorInstance<ColorService> instance1 = + new ConstructorInstance<ColorService>(() => new ColorService("Red")).WithName("Red"); + ConstructorInstance<ColorService> instance2 = + new ConstructorInstance<ColorService>(() => new ColorService("Green")).WithName("Green"); ColorService red1 = (ColorService) policy.Build(new StubBuildSession(), null, instance1); ColorService green1 = (ColorService) policy.Build(new StubBuildSession(), null, instance2); Modified: trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -11,7 +11,10 @@ [Test] public void Sad_path_inner_function_throws_exception_207_with_key_and_plugin_type() { - ConstructorInstance instance = new ConstructorInstance(delegate { throw new NotImplementedException(); }); + ConstructorInstance<object> instance = new ConstructorInstance<object>(() => + { + throw new NotImplementedException(); + }); try { Modified: trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -25,7 +25,7 @@ private void addDefaultToPluginFamily<T>(string name) { - ConstructorInstance instance = new ConstructorInstance(null).WithName(name); + LiteralInstance instance = new LiteralInstance(0).WithName(name); PluginFamily family = _pluginGraph.FindFamily(typeof (T)); family.AddInstance(instance); family.DefaultInstanceKey = instance.Name; @@ -35,7 +35,7 @@ { _manager.SetDefault(profile, typeof (T), new ReferencedInstance(name)); PluginFamily family = _pluginGraph.FindFamily(typeof (T)); - family.AddInstance(new ConstructorInstance(null).WithName(name)); + family.AddInstance(new LiteralInstance(0).WithName(name)); } private void addDefaultToMachine<T>(string name) Added: trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/SmartInstanceTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -0,0 +1,158 @@ +using System; +using NUnit.Framework; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class SmartInstanceTester + { + private IStructuredInstance structuredInstance; + private IConfiguredInstance configuredInstance; + + private SmartInstance<T> instanceOf<T>() + { + var instance = new SmartInstance<T>(); + structuredInstance = instance; + configuredInstance = instance; + + return instance; + } + + public T build<T>(Action<SmartInstance<T>> action) + { + SmartInstance<T> instance = instanceOf<T>(); + action(instance); + + var container = new Container(r => r.ForRequestedType<T>().TheDefaultIs(instance)); + return container.GetInstance<T>(); + } + + [Test] + public void specify_a_constructor_dependency() + { + var widget = new ColorWidget("Red"); + build<ClassWithWidget>(i => i.CtorDependency<IWidget>("widget").Is(x => x.Object(widget))).Widget. + ShouldBeTheSameAs(widget); + } + + [Test] + public void specify_a_constructor_dependency_by_type() + { + var widget = new ColorWidget("Red"); + build<ClassWithWidget>(i => i.CtorDependency<IWidget>().Is(x => x.Object(widget))).Widget.ShouldBeTheSameAs( + widget); + } + + [Test] + public void specify_a_property_dependency() + { + var widget = new ColorWidget("Red"); + build<ClassWithWidgetProperty>(i => i.SetterDependency(x => x.Widget).Is(x => x.Object(widget))).Widget. + ShouldBeTheSameAs(widget); + } + + [Test] + public void specify_a_simple_property() + { + build<SimplePropertyTarget>(i => i.SetProperty(x => x.Name = "Jeremy")).Name.ShouldEqual("Jeremy"); + build<SimplePropertyTarget>(i => i.SetProperty(x => x.Age = 16)).Age.ShouldEqual(16); + } + + [Test] + public void specify_a_simple_property_name_with_equal_to() + { + build<SimplePropertyTarget>(i => i.WithProperty("Name").EqualTo("Scott")).Name.ShouldEqual("Scott"); + } + + [Test] + public void specify_a_simple_property_with_equal_to() + { + build<SimplePropertyTarget>(i => i.WithProperty(x => x.Name).EqualTo("Bret")).Name.ShouldEqual("Bret"); + } + + [Test] + public void successfully_specify_the_constructor_argument_of_a_string() + { + build<ColorRule>(i => i.WithCtorArg("color").EqualTo("Red")).Color.ShouldEqual("Red"); + } + + [Test] + public void specify_an_array_as_a_constructor() + { + IWidget widget1 = new AWidget(); + IWidget widget2 = new AWidget(); + IWidget widget3 = new AWidget(); + + build<ClassWithWidgetArrayCtor>(i => i.TheArrayOf<IWidget>().Contains(x => + { + x.Object(widget1); + x.Object(widget2); + x.Object(widget3); + })).Widgets.ShouldEqual(new IWidget[] {widget1, widget2, widget3}); + } + + + [Test] + public void specify_an_array_as_a_property() + { + IWidget widget1 = new AWidget(); + IWidget widget2 = new AWidget(); + IWidget widget3 = new AWidget(); + + build<ClassWithWidgetArraySetter>(i => i.TheArrayOf<IWidget>().Contains(x => + { + x.Object(widget1); + x.Object(widget2); + x.Object(widget3); + })).Widgets.ShouldEqual(new IWidget[] { widget1, widget2, widget3 }); + } + } + + public class ClassWithWidgetArrayCtor + { + private readonly IWidget[] _widgets; + + public ClassWithWidgetArrayCtor(IWidget[] widgets) + { + _widgets = widgets; + } + + public IWidget[] Widgets + { + get { return _widgets; } + } + } + + public class ClassWithWidgetArraySetter + { + public IWidget[] Widgets { get; set; } + } + + public class SimplePropertyTarget + { + public string Name { get; set; } + public int Age { get; set; } + } + + public class ClassWithWidget + { + private readonly IWidget _widget; + + public ClassWithWidget(IWidget widget) + { + _widget = widget; + } + + public IWidget Widget + { + get { return _widget; } + } + } + + public class ClassWithWidgetProperty + { + public IWidget Widget { get; set; } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs 2008-08-19 15:27:23 UTC (rev 141) @@ -15,7 +15,7 @@ public void SetUp() { _policy = new ThreadLocalStoragePolicy(); - _instance = new ConstructorInstance(delegate { return new ColorRule("Red"); }).WithName("Red"); + _instance = new ConstructorInstance<ColorRule>(() => new ColorRule("Red")).WithName("Red"); } #endregion @@ -24,7 +24,7 @@ private ColorRule _rule1; private ColorRule _rule2; private ColorRule _rule3; - private ConstructorInstance _instance; + private ConstructorInstance<ColorRule> _instance; private void findRule1() Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-08-19 01:24:37 UTC (rev 140) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-08-19 15:27:23 UTC (rev 141) @@ -370,6 +370,7 @@ <Compile Include="Pipeline\PrototypeInstanceTester.cs" /> <Compile Include="Pipeline\ReferencedInstanceTester.cs" /> <Compile Include="Pipeline\SerializedInstanceTester.cs" /> + <Compile Include="Pipeline\SmartInstanceTester.cs" /> <Compile Include="Pipeline\StubBuildSession.cs" /> <Compile Include="Pipeline\ThreadLocalStoragePolicyTester.cs" /> <Compile Include="Pipeline\TypeRulesTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |