|
From: <jer...@us...> - 2008-01-17 17:46:09
|
Revision: 59
http://structuremap.svn.sourceforge.net/structuremap/?rev=59&view=rev
Author: jeremydmiller
Date: 2008-01-17 09:46:06 -0800 (Thu, 17 Jan 2008)
Log Message:
-----------
the explicit argument functionality
Modified Paths:
--------------
trunk/Source/StructureMap/AssemblyInfo.cs
trunk/Source/StructureMap/IInstanceCreator.cs
trunk/Source/StructureMap/IInstanceManager.cs
trunk/Source/StructureMap/InstanceFactory.cs
trunk/Source/StructureMap/InstanceManager.cs
trunk/Source/StructureMap/InstanceMemento.cs
trunk/Source/StructureMap/ObjectFactory.cs
trunk/Source/StructureMap/StructureMap.csproj
trunk/Source/StructureMap/StructureMapConfiguration.cs
trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs
trunk/Source/StructureMap.Testing/Container/FullStackFacadeTester.cs
trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
Added Paths:
-----------
trunk/Source/StructureMap/Configuration/Mementos/ExplicitArgumentMemento.cs
trunk/Source/StructureMap/Configuration/Mementos/ExplicitArguments.cs
trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs
Modified: trunk/Source/StructureMap/AssemblyInfo.cs
===================================================================
--- trunk/Source/StructureMap/AssemblyInfo.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/AssemblyInfo.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -1,4 +1,5 @@
using System.Reflection;
+using System.Runtime.CompilerServices;
//
// General Information about an assembly is controlled through the following
@@ -7,4 +8,5 @@
//
[assembly : AssemblyTitle("StructureMap")]
-[assembly : AssemblyDescription("Main Library")]
\ No newline at end of file
+[assembly : AssemblyDescription("Main Library")]
+[assembly : InternalsVisibleTo("StructureMap.AutoMocking")]
\ No newline at end of file
Added: trunk/Source/StructureMap/Configuration/Mementos/ExplicitArgumentMemento.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/Mementos/ExplicitArgumentMemento.cs (rev 0)
+++ trunk/Source/StructureMap/Configuration/Mementos/ExplicitArgumentMemento.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -0,0 +1,71 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using StructureMap.Graph;
+
+namespace StructureMap.Configuration.Mementos
+{
+ public class ExplicitArgumentMemento : InstanceMemento
+ {
+ private readonly ExplicitArguments _args;
+ private InstanceMemento _inner;
+
+ public ExplicitArgumentMemento(ExplicitArguments args, InstanceMemento inner)
+ {
+ _args = args;
+ _inner = inner;
+ }
+
+
+ protected override object buildInstance(IInstanceCreator creator)
+ {
+ if (_inner == null)
+ {
+ _inner = creator.DefaultMemento;
+ }
+
+ return base.buildInstance(creator);
+ }
+
+ protected override string innerConcreteKey
+ {
+ get { return _inner.ConcreteKey; }
+ }
+
+ protected override string innerInstanceKey
+ {
+ get { return _inner.InstanceKey; }
+ }
+
+ public override bool IsReference
+ {
+ get { return false; }
+ }
+
+ public override string ReferenceKey
+ {
+ get { return _inner.ReferenceKey; }
+ }
+
+ protected override string getPropertyValue(string Key)
+ {
+ return _args.GetArg(Key) ?? _inner.GetProperty(Key);
+ }
+
+ protected override InstanceMemento getChild(string Key)
+ {
+ return _inner.GetChildMemento(Key);
+ }
+
+ public override object GetChild(string key, string typeName, InstanceManager manager)
+ {
+ Type type = Type.GetType(typeName, true);
+ return _args.Get(type) ?? base.GetChild(key, typeName, manager);
+ }
+
+ public override InstanceMemento[] GetChildrenArray(string Key)
+ {
+ return _inner.GetChildrenArray(Key);
+ }
+ }
+}
Added: trunk/Source/StructureMap/Configuration/Mementos/ExplicitArguments.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/Mementos/ExplicitArguments.cs (rev 0)
+++ trunk/Source/StructureMap/Configuration/Mementos/ExplicitArguments.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace StructureMap.Configuration.Mementos
+{
+ public class ExplicitArguments
+ {
+ private readonly Dictionary<Type, object> _children = new Dictionary<Type, object>();
+ private readonly Dictionary<string, string> _args = new Dictionary<string, string>();
+
+ public T Get<T>() where T : class
+ {
+ return (T) Get(typeof(T));
+ }
+
+ public object Get(Type type)
+ {
+ return _children.ContainsKey(type) ? _children[type] : null;
+ }
+
+ public void Set<T>(T arg)
+ {
+ _children.Add(typeof(T), arg);
+ }
+
+ public void SetArg(string key, object argValue)
+ {
+ _args.Add(key, argValue.ToString());
+ }
+
+ public string GetArg(string key)
+ {
+ return _args.ContainsKey(key) ? _args[key] : null;
+ }
+ }
+}
Modified: trunk/Source/StructureMap/IInstanceCreator.cs
===================================================================
--- trunk/Source/StructureMap/IInstanceCreator.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/IInstanceCreator.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -3,5 +3,6 @@
public interface IInstanceCreator
{
object BuildInstance(InstanceMemento memento);
+ InstanceMemento DefaultMemento{ get;}
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/IInstanceManager.cs
===================================================================
--- trunk/Source/StructureMap/IInstanceManager.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/IInstanceManager.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -1,4 +1,7 @@
+using System;
+using System.Collections;
using System.Collections.Generic;
+using StructureMap.Configuration.Mementos;
using StructureMap.Graph;
namespace StructureMap
@@ -9,10 +12,73 @@
T CreateInstance<T>(string instanceKey);
T CreateInstance<T>();
T FillDependencies<T>();
+ object FillDependencies(Type type);
void InjectStub<T>(T instance);
IList<T> GetAllInstances<T>();
void SetDefaultsToProfile(string profile);
T CreateInstance<T>(InstanceMemento memento);
+
+ /// <summary>
+ /// Sets up the InstanceManager to return the object in the "stub" argument anytime
+ /// any instance of the PluginType is requested
+ /// </summary>
+ /// <param name="pluginType"></param>
+ /// <param name="stub"></param>
+ void InjectStub(Type pluginType, object stub);
+
+ IList GetAllInstances(Type type);
+ void AddInstance<T>(InstanceMemento memento);
+ void AddInstance<PLUGINTYPE, CONCRETETYPE>();
+ void AddDefaultInstance<PLUGINTYPE, CONCRETETYPE>();
+ string WhatDoIHave();
+
+ /// <summary>
+ /// Sets the default instance for the PluginType
+ /// </summary>
+ /// <param name="pluginType"></param>
+ /// <param name="instanceMemento"></param>
+ void SetDefault(Type pluginType, InstanceMemento instanceMemento);
+
+ /// <summary>
+ /// Sets the default instance for the PluginType
+ /// </summary>
+ /// <param name="pluginType"></param>
+ /// <param name="instanceKey"></param>
+ void SetDefault(Type pluginType, string instanceKey);
+
+ /// <summary>
+ /// Sets the default instance for the PluginType
+ /// </summary>
+ /// <param name="pluginTypeName"></param>
+ /// <param name="instanceKey"></param>
+ void SetDefault(string pluginTypeName, string instanceKey);
+
+ /// <summary>
+ /// Creates a new object instance of the requested type
+ /// </summary>
+ /// <param name="pluginType"></param>
+ /// <returns></returns>
+ object CreateInstance(Type pluginType);
+
+
+ /// <summary>
+ /// Creates a new instance of the requested type using the InstanceMemento. Mostly used from other
+ /// classes to link children members
+ /// </summary>
+ /// <param name="pluginType"></param>
+ /// <param name="instanceMemento"></param>
+ /// <returns></returns>
+ object CreateInstance(Type pluginType, InstanceMemento instanceMemento);
+
+ /// <summary>
+ /// Creates the named instance of the PluginType
+ /// </summary>
+ /// <param name="pluginType"></param>
+ /// <param name="instanceKey"></param>
+ /// <returns></returns>
+ object CreateInstance(Type pluginType, string instanceKey);
+
+ PLUGINTYPE CreateInstance<PLUGINTYPE>(ExplicitArguments args);
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/InstanceFactory.cs
===================================================================
--- trunk/Source/StructureMap/InstanceFactory.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/InstanceFactory.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -385,5 +385,11 @@
201, memento.ConcreteKey, memento.InstanceKey, PluginType.FullName);
}
}
+
+
+ InstanceMemento IInstanceCreator.DefaultMemento
+ {
+ get { return _source.DefaultMemento; }
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/InstanceManager.cs
===================================================================
--- trunk/Source/StructureMap/InstanceManager.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/InstanceManager.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Text;
using StructureMap.Configuration.Mementos;
using StructureMap.Exceptions;
using StructureMap.Graph;
@@ -147,6 +148,13 @@
return (T) CreateInstance(typeof (T), memento);
}
+ public PLUGINTYPE CreateInstance<PLUGINTYPE>(ExplicitArguments args)
+ {
+ ExplicitArgumentMemento memento = new ExplicitArgumentMemento(args, null);
+ return CreateInstance<PLUGINTYPE>(memento);
+
+ }
+
public T CreateInstance<T>()
{
return (T) CreateInstance(typeof (T));
@@ -266,6 +274,8 @@
return instanceFactory.GetInstance(instanceKey);
}
+
+
/// <summary>
/// Creates a new object instance of the requested type
/// </summary>
@@ -461,5 +471,18 @@
protected delegate InstanceFactory CreateFactoryDelegate(Type type);
#endregion
+
+ public string WhatDoIHave()
+ {
+ StringBuilder sb = new StringBuilder();
+
+ foreach (IInstanceFactory factory in this)
+ {
+ sb.AppendFormat("PluginType {0}, Default: {1}\r\n", factory.PluginType.AssemblyQualifiedName,
+ factory.DefaultInstanceKey);
+ }
+
+ return sb.ToString();
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/InstanceMemento.cs
===================================================================
--- trunk/Source/StructureMap/InstanceMemento.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/InstanceMemento.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -180,6 +180,7 @@
/// <returns></returns>
protected abstract InstanceMemento getChild(string Key);
+
/// <summary>
/// Using InstanceManager and the TypeName, creates an object instance using the
/// child InstanceMemento specified by Key
@@ -188,7 +189,7 @@
/// <param name="typeName"></param>
/// <param name="manager"></param>
/// <returns></returns>
- public object GetChild(string key, string typeName, InstanceManager manager)
+ public virtual object GetChild(string key, string typeName, InstanceManager manager)
{
InstanceMemento memento = GetChildMemento(key);
object returnValue = null;
Modified: trunk/Source/StructureMap/ObjectFactory.cs
===================================================================
--- trunk/Source/StructureMap/ObjectFactory.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/ObjectFactory.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -16,8 +16,8 @@
[EnvironmentPermission(SecurityAction.Assert, Read="COMPUTERNAME")]
public class ObjectFactory
{
- private static object _lockObject = new object();
- private static InstanceManager _manager;
+ private static readonly object _lockObject = new object();
+ private static IInstanceManager _manager;
private static string _profile = string.Empty;
@@ -79,15 +79,7 @@
public static string WhatDoIHave()
{
- StringBuilder sb = new StringBuilder();
-
- foreach (IInstanceFactory factory in manager)
- {
- sb.AppendFormat("PluginType {0}, Default: {1}\r\n", factory.PluginType.AssemblyQualifiedName,
- factory.DefaultInstanceKey);
- }
-
- return sb.ToString();
+ return _manager.WhatDoIHave();
}
/// <summary>
@@ -151,7 +143,7 @@
#region InstanceManager and setting defaults
- private static InstanceManager manager
+ private static IInstanceManager manager
{
get
{
@@ -200,7 +192,16 @@
}
}
+ /// <summary>
+ /// Strictly used for testing scenarios
+ /// </summary>
+ /// <param name="manager"></param>
+ internal static void ReplaceManager(IInstanceManager manager)
+ {
+ _manager = manager;
+ }
+
/// <summary>
/// Fires when the ObjectFactory is refreshed
/// </summary>
@@ -374,6 +375,57 @@
return specificInstances;
}
+ public static ExplicitArgsExpression With<T>(T arg)
+ {
+ return new ExplicitArgsExpression(manager).With<T>(arg);
+ }
+
+ public static IExplicitProperty With(string argName)
+ {
+ return new ExplicitArgsExpression(manager).With(argName);
+ }
+
+ public interface IExplicitProperty
+ {
+ ExplicitArgsExpression EqualTo(object value);
+ }
+
+ public class ExplicitArgsExpression : IExplicitProperty
+ {
+ private readonly IInstanceManager _manager;
+ private readonly ExplicitArguments _args = new ExplicitArguments();
+ private string _lastArgName;
+
+ internal ExplicitArgsExpression(IInstanceManager manager)
+ {
+ _manager = manager;
+ }
+
+ 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);
+ }
+
+ ExplicitArgsExpression IExplicitProperty.EqualTo(object value)
+ {
+ _args.SetArg(_lastArgName, value);
+ return this;
+ }
+ }
+
#endregion
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/StructureMap.csproj
===================================================================
--- trunk/Source/StructureMap/StructureMap.csproj 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/StructureMap.csproj 2008-01-17 17:46:06 UTC (rev 59)
@@ -219,6 +219,8 @@
<Compile Include="Configuration\DSL\Expressions\InstanceDefaultExpression.cs" />
<Compile Include="Configuration\DSL\Expressions\InstanceExpression.cs" />
<Compile Include="Configuration\DSL\Expressions\LiteralExpression.cs" />
+ <Compile Include="Configuration\Mementos\ExplicitArgumentMemento.cs" />
+ <Compile Include="Configuration\Mementos\ExplicitArguments.cs" />
<Compile Include="Configuration\Mementos\LiteralMemento.cs" />
<Compile Include="Configuration\DSL\Expressions\MementoBuilder.cs" />
<Compile Include="Configuration\DSL\Expressions\ProfileExpression.cs" />
Modified: trunk/Source/StructureMap/StructureMapConfiguration.cs
===================================================================
--- trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -173,6 +173,16 @@
}
/// <summary>
+ /// Direct StructureMap to create instances of Type T
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE">The Type to build</typeparam>
+ /// <returns></returns>
+ public static CreatePluginFamilyExpression<PLUGINTYPE> ForRequestedType<PLUGINTYPE>()
+ {
+ return _registry.BuildInstancesOf<PLUGINTYPE>();
+ }
+
+ /// <summary>
/// Adds a new configured instance of Type T
/// </summary>
/// <typeparam name="T"></typeparam>
Modified: trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs
===================================================================
--- trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap.AutoMocking/RhinoAutoMocker.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -13,7 +13,7 @@
public class RhinoAutoMocker<TARGETCLASS> : MockRepository where TARGETCLASS : class
{
- private AutoMockedInstanceManager _manager;
+ private readonly AutoMockedInstanceManager _manager;
public RhinoAutoMocker()
{
@@ -21,6 +21,11 @@
_manager = new AutoMockedInstanceManager(locator);
}
+ public void MockObjectFactory()
+ {
+ ObjectFactory.ReplaceManager(_manager);
+ }
+
public TARGETCLASS Create()
{
return _manager.FillDependencies<TARGETCLASS>();
Added: trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Container/ExplicitArgumentTester.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -0,0 +1,232 @@
+using NUnit.Framework;
+using StructureMap.Configuration.DSL;
+using StructureMap.Configuration.Mementos;
+using StructureMap.Graph;
+
+namespace StructureMap.Testing.Container
+{
+ [TestFixture]
+ public class ExplicitArgumentTester
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ StructureMapConfiguration.ResetAll();
+ StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ StructureMapConfiguration.ResetAll();
+ ObjectFactory.Reset();
+
+ }
+
+ #endregion
+
+ public void GetTypedArgumentsFromAnExplicitArgumentsMementoIfThereIsAnExplicitArgument()
+ {
+ 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
+ {
+ }
+
+ public class RedTarget : IExplicitTarget
+ {
+ }
+
+ public class GreenTarget : IExplicitTarget
+ {
+ }
+
+ public class ExplicitTarget : IExplicitTarget
+ {
+ private readonly string _name;
+ private readonly IProvider _provider;
+
+ public ExplicitTarget(string name, IProvider provider)
+ {
+ _name = name;
+ _provider = provider;
+ }
+
+
+ public string Name
+ {
+ get { return _name; }
+ }
+
+ public IProvider Provider
+ {
+ get { return _provider; }
+ }
+ }
+
+ public interface IProvider
+ {
+ }
+
+ public class RedProvider : IProvider
+ {
+ }
+
+ public class BlueProvider : IProvider
+ {
+ }
+
+ [Test]
+ public void NowDoItWithObjectFactoryItself()
+ {
+ StructureMapConfiguration.ForRequestedType<ExplicitTarget>().TheDefaultIs(
+ Registry.Instance<ExplicitTarget>()
+ .UsingConcreteType<ExplicitTarget>()
+ .Child<IProvider>().IsConcreteType<RedProvider>()
+ .WithProperty("name").EqualTo("Jeremy")
+ );
+
+ ObjectFactory.Reset();
+
+ // Get the ExplicitTarget without setting an explicit arg for IProvider
+ ExplicitTarget firstTarget = ObjectFactory.GetInstance<ExplicitTarget>();
+ Assert.IsInstanceOfType(typeof (RedProvider), firstTarget.Provider);
+
+ // Now, set the explicit arg for IProvider
+ BlueProvider theBlueProvider = new BlueProvider();
+ ExplicitTarget secondTarget = ObjectFactory.With<IProvider>(theBlueProvider).GetInstance<ExplicitTarget>();
+ Assert.AreSame(theBlueProvider, secondTarget.Provider);
+ }
+
+ [Test]
+ public void OverrideAPrimitiveWithObjectFactory()
+ {
+ StructureMapConfiguration.ForRequestedType<ExplicitTarget>().TheDefaultIs(
+ Registry.Instance<ExplicitTarget>()
+ .UsingConcreteType<ExplicitTarget>()
+ .Child<IProvider>().IsConcreteType<RedProvider>()
+ .WithProperty("name").EqualTo("Jeremy")
+ );
+
+ ObjectFactory.Reset();
+
+ // Get the ExplicitTarget without setting an explicit arg for IProvider
+ ExplicitTarget firstTarget = ObjectFactory.GetInstance<ExplicitTarget>();
+ Assert.AreEqual("Jeremy", firstTarget.Name);
+
+ // Now, set the explicit arg for IProvider
+ ExplicitTarget secondTarget = ObjectFactory.With("name").EqualTo("Julia").GetInstance<ExplicitTarget>();
+ Assert.AreEqual("Julia", secondTarget.Name);
+ }
+
+ [Test]
+ public void OverridePrimitiveArgs()
+ {
+ 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);
+
+ // Once without an explicit arg set
+ Assert.AreEqual("Jeremy", manager.CreateInstance<ExplicitTarget>(memento).Name);
+
+ // Now, set the explicit arg
+ args.SetArg("name", "Max");
+ Assert.AreEqual("Max", manager.CreateInstance<ExplicitTarget>(memento).Name);
+ }
+
+ [Test]
+ public void PassExplicitArgsIntoInstanceManager()
+ {
+ Registry registry = new Registry();
+
+ registry.ForRequestedType<ExplicitTarget>().TheDefaultIs(
+ Registry.Instance<ExplicitTarget>()
+ .UsingConcreteType<ExplicitTarget>()
+ .Child<IProvider>().IsConcreteType<RedProvider>()
+ .WithProperty("name").EqualTo("Jeremy")
+ );
+
+ IInstanceManager manager = registry.BuildInstanceManager();
+
+ ExplicitArguments args = new ExplicitArguments();
+
+ // Get the ExplicitTarget without setting an explicit arg for IProvider
+ ExplicitTarget firstTarget = manager.CreateInstance<ExplicitTarget>(args);
+ Assert.IsInstanceOfType(typeof (RedProvider), firstTarget.Provider);
+
+ // Now, set the explicit arg for IProvider
+ args.Set<IProvider>(new BlueProvider());
+ ExplicitTarget secondTarget = manager.CreateInstance<ExplicitTarget>(args);
+ Assert.IsInstanceOfType(typeof (BlueProvider), secondTarget.Provider);
+ }
+
+ [Test]
+ public void RegisterAndFindServicesOnTheExplicitArgument()
+ {
+ ExplicitArguments args = new ExplicitArguments();
+ Assert.IsNull(args.Get<IProvider>());
+
+ RedProvider red = new RedProvider();
+ args.Set<IProvider>(red);
+
+ Assert.AreSame(red, args.Get<IProvider>());
+
+ args.Set<IExplicitTarget>(new RedTarget());
+ Assert.IsInstanceOfType(typeof (RedTarget), args.Get<IExplicitTarget>());
+ }
+
+ [Test]
+ public void RegisterAndRetrieveArgs()
+ {
+ ExplicitArguments args = new ExplicitArguments();
+ Assert.IsNull(args.GetArg("name"));
+
+ args.SetArg("name", "Jeremy");
+ Assert.AreEqual("Jeremy", args.GetArg("name"));
+
+ args.SetArg("age", 34);
+ Assert.AreEqual("34", args.GetArg("age"));
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Container/FullStackFacadeTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/FullStackFacadeTester.cs 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap.Testing/Container/FullStackFacadeTester.cs 2008-01-17 17:46:06 UTC (rev 59)
@@ -67,6 +67,13 @@
Assert.IsNotNull(target.Rule);
}
+ [Test]
+ public void FillDependenc1ies2()
+ {
+ FilledTarget target = ObjectFactory.FillDependencies<FilledTarget>();
+ Assert.IsNotNull(target.Gateway);
+ Assert.IsNotNull(target.Rule);
+ }
[Test]
public void GetChildWithDefinedGrandChild()
Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
===================================================================
--- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-01-16 14:23:24 UTC (rev 58)
+++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-01-17 17:46:06 UTC (rev 59)
@@ -296,6 +296,7 @@
<Compile Include="Container\ExceptionHandling\StructureMapExceptionTester.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="Container\ExplicitArgumentTester.cs" />
<Compile Include="Container\FillDependenciesTester.cs">
<SubType>Code</SubType>
</Compile>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|