|
From: <jer...@us...> - 2007-12-14 11:16:42
|
Revision: 49
http://structuremap.svn.sourceforge.net/structuremap/?rev=49&view=rev
Author: jeremydmiller
Date: 2007-12-14 03:16:38 -0800 (Fri, 14 Dec 2007)
Log Message:
-----------
Configuration changes for the dynamic injection
Modified Paths:
--------------
trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs
trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs
trunk/Source/StructureMap/Configuration/DSL/Registry.cs
trunk/Source/StructureMap/Graph/Plugin.cs
trunk/Source/StructureMap/Graph/PluginCollection.cs
trunk/Source/StructureMap/Graph/PluginFamily.cs
trunk/Source/StructureMap/IInstanceFactory.cs
trunk/Source/StructureMap/IInstanceManager.cs
trunk/Source/StructureMap/InstanceBuilder.cs
trunk/Source/StructureMap/InstanceFactory.cs
trunk/Source/StructureMap/InstanceManager.cs
trunk/Source/StructureMap/Interceptors/InstanceFactoryInterceptor.cs
trunk/Source/StructureMap/MemoryInstanceMemento.cs
trunk/Source/StructureMap/ObjectFactory.cs
trunk/Source/StructureMap/StructureMap.csproj
trunk/Source/StructureMap/StructureMapException.resx
trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs
trunk/Source/StructureMap.Testing/Container/FillDependenciesTester.cs
trunk/Source/StructureMap.Testing/Container/FullStackFacadeTester.cs
trunk/Source/StructureMap.Testing/Container/InstanceFactoryTester.cs
trunk/Source/StructureMap.Testing/Container/MockingTester.cs
trunk/Source/StructureMap.Testing/ObjectMother.cs
trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
trunk/Source/StructureMap.sln
Added Paths:
-----------
trunk/Source/StructureMap/Configuration/DSL/ConstructorExpression.cs
trunk/Source/StructureMap/ConstructorMemento.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs
trunk/Source/StructureMap.Testing/ConstructorMementoTester.cs
trunk/Source/StructureMap.Testing/Container/DynamicInjectionTester.cs
Removed Paths:
-------------
trunk/Source/StructureMap/MockInstanceFactory.cs
trunk/Source/StructureMap/StubbedInstanceFactory.cs
trunk/Source/StructureMap.Testing/Container/StubbedInstanceFactoryTester.cs
Added: trunk/Source/StructureMap/Configuration/DSL/ConstructorExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/ConstructorExpression.cs (rev 0)
+++ trunk/Source/StructureMap/Configuration/DSL/ConstructorExpression.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -0,0 +1,49 @@
+using System;
+using StructureMap.Graph;
+
+namespace StructureMap.Configuration.DSL
+{
+ public class ConstructorExpression<PLUGINTYPE> : MementoBuilder<ConstructorExpression<PLUGINTYPE>>
+ {
+ private ConstructorMemento<PLUGINTYPE> _memento;
+
+ public ConstructorExpression(BuildObjectDelegate<PLUGINTYPE> builder)
+ : base(typeof (PLUGINTYPE))
+ {
+ _memento.Builder = builder;
+ }
+
+
+ protected override InstanceMemento memento
+ {
+ get { return _memento; }
+ }
+
+ protected override ConstructorExpression<PLUGINTYPE> thisInstance
+ {
+ get { return this; }
+ }
+
+ protected override void configureMemento(PluginFamily family)
+ {
+ }
+
+ protected override void validate()
+ {
+ }
+
+ protected override void buildMemento()
+ {
+ _memento = new ConstructorMemento<PLUGINTYPE>();
+ }
+
+ public override void ValidatePluggability(Type pluginType)
+ {
+ if (!pluginType.Equals(typeof(PLUGINTYPE)))
+ {
+ throw new StructureMapException(306,
+ typeof (PLUGINTYPE).FullName, pluginType.FullName);
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Configuration/DSL/CreatePluginFamilyExpression.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -65,20 +65,34 @@
return this;
}
+ public CreatePluginFamilyExpression AddInstance(IMementoBuilder builder)
+ {
+ builder.ValidatePluggability(_pluginType);
+
+ _children.Add(builder);
+ _alterations.Add(delegate (PluginFamily family)
+ {
+ InstanceMemento memento = builder.BuildMemento(family);
+ family.Source.AddExternalMemento(memento);
+ });
+
+ return this;
+ }
+
/// <summary>
/// Convenience method that sets the default concrete type of the PluginType. Type T
/// can only accept types that do not have any primitive constructor arguments.
/// StructureMap has to know how to construct all of the constructor argument types.
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="CONCRETETYPE"></typeparam>
/// <returns></returns>
- public CreatePluginFamilyExpression TheDefaultIsConcreteType<T>()
+ public CreatePluginFamilyExpression TheDefaultIsConcreteType<CONCRETETYPE>()
{
- ExpressionValidator.ValidatePluggabilityOf(typeof (T)).IntoPluginType(_pluginType);
+ ExpressionValidator.ValidatePluggabilityOf(typeof (CONCRETETYPE)).IntoPluginType(_pluginType);
_alterations.Add(delegate(PluginFamily family)
{
- Plugin plugin = family.Plugins.FindOrCreate(typeof (T));
+ Plugin plugin = family.Plugins.FindOrCreate(typeof (CONCRETETYPE));
family.DefaultInstanceKey = plugin.ConcreteKey;
});
@@ -112,5 +126,7 @@
delegate(PluginFamily family) { family.InterceptionChain.AddInterceptor(new SingletonInterceptor()); });
return this;
}
+
+
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Configuration/DSL/LiteralMemento.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -9,8 +9,14 @@
public LiteralMemento(object instance)
{
_instance = instance;
+ InstanceKey = Guid.NewGuid().ToString();
}
+ public LiteralMemento Named(string name)
+ {
+ InstanceKey = name;
+ return this;
+ }
public object Instance
{
Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -52,16 +52,32 @@
/// Direct StructureMap to build instances of type T, and look for concrete classes
/// marked with the [Pluggable] attribute that implement type T
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <returns></returns>
- public CreatePluginFamilyExpression BuildInstancesOf<T>()
+ public CreatePluginFamilyExpression BuildInstancesOf<PLUGINTYPE>()
{
- CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof (T));
+ CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof (PLUGINTYPE));
addExpression(expression);
return expression;
}
+ /// <summary>
+ /// Direct StructureMap to build instances of type T, and look for concrete classes
+ /// marked with the [Pluggable] attribute that implement type T.
+ ///
+ /// This is the equivalent of calling BuildInstancesOf<T>()
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <returns></returns>
+ public CreatePluginFamilyExpression ForRequestedType<PLUGINTYPE>()
+ {
+ CreatePluginFamilyExpression expression = new CreatePluginFamilyExpression(typeof(PLUGINTYPE));
+ addExpression(expression);
+
+ return expression;
+ }
+
public IInstanceManager BuildInstanceManager()
{
ConfigurePluginGraph(_graph);
@@ -72,57 +88,59 @@
/// <summary>
/// Starts an instance definition of type T
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <returns></returns>
- public InstanceExpression.InstanceTypeExpression AddInstanceOf<T>()
+ public InstanceExpression.InstanceTypeExpression AddInstanceOf<PLUGINTYPE>()
{
- InstanceExpression expression = new InstanceExpression(typeof (T));
+ InstanceExpression expression = new InstanceExpression(typeof (PLUGINTYPE));
addExpression(expression);
return expression.TypeExpression();
}
+
+
/// <summary>
/// Convenience method to start the definition of an instance of type T
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <returns></returns>
- public static InstanceExpression.InstanceTypeExpression Instance<T>()
+ public static InstanceExpression.InstanceTypeExpression Instance<PLUGINTYPE>()
{
- InstanceExpression expression = new InstanceExpression(typeof (T));
+ InstanceExpression expression = new InstanceExpression(typeof (PLUGINTYPE));
return expression.TypeExpression();
}
/// <summary>
/// Convenience method to register a prototype instance
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <param name="prototype"></param>
/// <returns></returns>
- public static PrototypeExpression<T> Prototype<T>(T prototype)
+ public static PrototypeExpression<PLUGINTYPE> Prototype<PLUGINTYPE>(PLUGINTYPE prototype)
{
- return new PrototypeExpression<T>(prototype);
+ return new PrototypeExpression<PLUGINTYPE>(prototype);
}
/// <summary>
/// Convenience method to register a preconfigured instance of type T
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <param name="instance"></param>
/// <returns></returns>
- public static LiteralExpression<T> Object<T>(T instance)
+ public static LiteralExpression<PLUGINTYPE> Object<PLUGINTYPE>(PLUGINTYPE instance)
{
- return new LiteralExpression<T>(instance);
+ return new LiteralExpression<PLUGINTYPE>(instance);
}
/// <summary>
/// Registers a preconfigured instance
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <param name="target"></param>
/// <returns></returns>
- public LiteralExpression<T> AddInstanceOf<T>(T target)
+ public LiteralExpression<PLUGINTYPE> AddInstanceOf<PLUGINTYPE>(PLUGINTYPE target)
{
- LiteralExpression<T> literal = new LiteralExpression<T>(target);
+ LiteralExpression<PLUGINTYPE> literal = new LiteralExpression<PLUGINTYPE>(target);
addExpression(literal);
return literal;
@@ -131,12 +149,12 @@
/// <summary>
/// Add a preconfigured instance as a Prototype
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <param name="prototype"></param>
/// <returns></returns>
- public PrototypeExpression<T> AddPrototypeInstanceOf<T>(T prototype)
+ public PrototypeExpression<PLUGINTYPE> AddPrototypeInstanceOf<PLUGINTYPE>(PLUGINTYPE prototype)
{
- PrototypeExpression<T> expression = new PrototypeExpression<T>(prototype);
+ PrototypeExpression<PLUGINTYPE> expression = new PrototypeExpression<PLUGINTYPE>(prototype);
addExpression(expression);
return expression;
@@ -145,12 +163,12 @@
/// <summary>
/// convenience method for a UserControl
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <param name="url"></param>
/// <returns></returns>
- public static UserControlExpression LoadUserControlFrom<T>(string url)
+ public static UserControlExpression LoadUserControlFrom<PLUGINTYPE>(string url)
{
- return new UserControlExpression(typeof (T), url);
+ return new UserControlExpression(typeof (PLUGINTYPE), url);
}
/// <summary>
@@ -184,15 +202,21 @@
/// <summary>
/// Registers a UserControl as an instance
/// </summary>
- /// <typeparam name="T"></typeparam>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
/// <param name="url"></param>
/// <returns></returns>
- public UserControlExpression LoadControlFromUrl<T>(string url)
+ public UserControlExpression LoadControlFromUrl<PLUGINTYPE>(string url)
{
- UserControlExpression expression = new UserControlExpression(typeof (T), url);
+ UserControlExpression expression = new UserControlExpression(typeof (PLUGINTYPE), url);
addExpression(expression);
return expression;
}
+
+ public static ConstructorExpression<PLUGINTYPE> ConstructedBy<PLUGINTYPE>
+ (BuildObjectDelegate<PLUGINTYPE> builder)
+ {
+ return new ConstructorExpression<PLUGINTYPE>(builder);
+ }
}
}
\ No newline at end of file
Added: trunk/Source/StructureMap/ConstructorMemento.cs
===================================================================
--- trunk/Source/StructureMap/ConstructorMemento.cs (rev 0)
+++ trunk/Source/StructureMap/ConstructorMemento.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -0,0 +1,40 @@
+using System;
+
+namespace StructureMap
+{
+ public delegate PLUGINTYPE BuildObjectDelegate<PLUGINTYPE>();
+
+ public class ConstructorMemento<PLUGINTYPE> : MemoryInstanceMemento
+ {
+ private BuildObjectDelegate<PLUGINTYPE> _builder;
+
+
+ public ConstructorMemento()
+ {
+ }
+
+ public ConstructorMemento(string instanceKey, BuildObjectDelegate<PLUGINTYPE> builder)
+ : base(instanceKey, instanceKey)
+ {
+ _builder = builder;
+ }
+
+ public ConstructorMemento(BuildObjectDelegate<PLUGINTYPE> builder)
+ : this(Guid.NewGuid().ToString(), builder)
+ {
+
+ }
+
+ public override object Build(IInstanceCreator creator)
+ {
+ return _builder();
+ }
+
+
+ public BuildObjectDelegate<PLUGINTYPE> Builder
+ {
+ get { return _builder; }
+ set { _builder = value; }
+ }
+ }
+}
Modified: trunk/Source/StructureMap/Graph/Plugin.cs
===================================================================
--- trunk/Source/StructureMap/Graph/Plugin.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Graph/Plugin.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -13,6 +13,18 @@
{
#region static
+ public static Plugin CreateAutofilledPlugin(Type concreteType)
+ {
+ string pluginKey = Guid.NewGuid().ToString();
+ Plugin plugin = Plugin.CreateExplicitPlugin(concreteType, pluginKey, string.Empty);
+ if (!plugin.CanBeAutoFilled)
+ {
+ throw new StructureMapException(231);
+ }
+
+ return plugin;
+ }
+
/// <summary>
/// Finds an array of Plugin objects for a given PluginType in an Assembly object
/// by searching for all exported types marked with [Pluggable] that can be cast
@@ -428,5 +440,14 @@
throw new StructureMapException(302, typeof (T).FullName, _pluggedType.FullName);
}
+
+ public void AddToSource(MementoSource source)
+ {
+ InstanceMemento memento = CreateImplicitMemento();
+ if (memento != null)
+ {
+ source.AddExternalMemento(memento);
+ }
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Graph/PluginCollection.cs
===================================================================
--- trunk/Source/StructureMap/Graph/PluginCollection.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Graph/PluginCollection.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -22,6 +22,17 @@
get { return _plugins.Values; }
}
+ public Plugin[] All
+ {
+ get
+ {
+ Plugin[] returnValue = new Plugin[_plugins.Count];
+ _plugins.Values.CopyTo(returnValue, 0);
+
+ return returnValue;
+ }
+ }
+
public void Add(TypePath path, string concreteKey)
{
Plugin plugin = new Plugin(path, concreteKey);
@@ -61,12 +72,9 @@
throw new StructureMapException(114, plugin.PluggedType.FullName, _family.PluginTypeName);
}
- InstanceMemento memento = plugin.CreateImplicitMemento();
- if (memento != null)
- {
- _family.Source.AddExternalMemento(memento);
- }
+ plugin.AddToSource(_family.Source);
+
_plugins.Add(plugin.ConcreteKey, plugin);
}
Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs
===================================================================
--- trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -16,19 +16,14 @@
public static PluginFamily CreateAutoFilledPluginFamily(Type pluginType)
{
- Plugin plugin = Plugin.CreateExplicitPlugin(pluginType, CONCRETE_KEY, string.Empty);
- if (!plugin.CanBeAutoFilled)
- {
- throw new StructureMapException(231);
- }
+ Plugin plugin = Plugin.CreateAutofilledPlugin(pluginType);
PluginFamily family = new PluginFamily(pluginType);
family.DefinitionSource = DefinitionSource.Implicit;
family.Plugins.Add(plugin);
+ family.DefaultInstanceKey = plugin.ConcreteKey;
- family.DefaultInstanceKey = CONCRETE_KEY;
-
return family;
}
Modified: trunk/Source/StructureMap/IInstanceFactory.cs
===================================================================
--- trunk/Source/StructureMap/IInstanceFactory.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/IInstanceFactory.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -69,5 +69,8 @@
/// </summary>
/// <returns></returns>
IList GetAllInstances();
+
+ void AddInstance(InstanceMemento memento);
+ InstanceMemento AddType<T>();
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/IInstanceManager.cs
===================================================================
--- trunk/Source/StructureMap/IInstanceManager.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/IInstanceManager.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -8,7 +8,7 @@
T CreateInstance<T>(string instanceKey);
T CreateInstance<T>();
T FillDependencies<T>();
- void Inject<T>(T instance);
+ void InjectStub<T>(T instance);
IList<T> GetAllInstances<T>();
void SetDefaultsToProfile(string profile);
Modified: trunk/Source/StructureMap/InstanceBuilder.cs
===================================================================
--- trunk/Source/StructureMap/InstanceBuilder.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/InstanceBuilder.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -1,3 +1,6 @@
+using System;
+using StructureMap.Graph;
+
namespace StructureMap
{
/// <summary>
@@ -26,5 +29,11 @@
{
get { return _manager; }
}
+
+ public bool IsType(Type type)
+ {
+ Type plugged = Type.GetType(PluggedType);
+ return plugged.Equals(type);
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/InstanceFactory.cs
===================================================================
--- trunk/Source/StructureMap/InstanceFactory.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/InstanceFactory.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -1,5 +1,6 @@
using System;
using System.Collections;
+using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
@@ -15,15 +16,14 @@
public class InstanceFactory : IInstanceFactory, IInstanceCreator
{
private Type _pluginType;
- private string _assemblyName;
- private HybridDictionary _instanceBuilders;
+ private Dictionary<string, InstanceBuilder> _instanceBuilders;
private MementoSource _source;
#region constructor functions
private InstanceFactory()
{
- _instanceBuilders = new HybridDictionary();
+ _instanceBuilders = new Dictionary<string, InstanceBuilder>();
_source = new MemoryMementoSource();
}
@@ -42,8 +42,8 @@
try
{
determineMementoSource(family);
- setPluginType(family.PluginType);
- processPlugins(family.Plugins);
+ _pluginType = family.PluginType;
+ processPlugins(family.Plugins.All);
determineDefaultKey(family, failOnException);
}
catch (Exception e)
@@ -52,6 +52,7 @@
}
}
+
private void determineMementoSource(PluginFamily family)
{
if (family.Source == null)
@@ -102,13 +103,6 @@
}
}
-
- private void setPluginType(Type PluginType)
- {
- _pluginType = PluginType;
- _assemblyName = Guid.NewGuid().ToString().Replace(".", "") + "InstanceBuilderAssembly";
- }
-
#endregion
/// <summary>
@@ -125,21 +119,19 @@
#region create instance builders
- private void processPlugins(PluginCollection plugins)
+ private void processPlugins(Plugin[] plugins)
{
- _instanceBuilders.Clear();
-
Assembly assembly = createInstanceBuilderAssembly(plugins);
-
foreach (Plugin plugin in plugins)
{
addPlugin(assembly, plugin);
}
}
- private Assembly createInstanceBuilderAssembly(PluginCollection plugins)
+ private Assembly createInstanceBuilderAssembly(Plugin[] plugins)
{
- InstanceBuilderAssembly builderAssembly = new InstanceBuilderAssembly(_assemblyName, PluginType);
+ string assemblyName = Guid.NewGuid().ToString().Replace(".", "") + "InstanceBuilderAssembly";
+ InstanceBuilderAssembly builderAssembly = new InstanceBuilderAssembly(assemblyName, PluginType);
foreach (Plugin plugin in plugins)
{
@@ -212,7 +204,7 @@
public object BuildInstance(InstanceMemento memento)
{
- if (!_instanceBuilders.Contains(memento.ConcreteKey))
+ if (!_instanceBuilders.ContainsKey(memento.ConcreteKey))
{
throw new StructureMapException(
201, memento.ConcreteKey, memento.InstanceKey, PluginType.FullName);
@@ -338,5 +330,30 @@
return list;
}
+
+ public void AddInstance(InstanceMemento memento)
+ {
+ _source.AddExternalMemento(memento);
+ }
+
+
+ public InstanceMemento AddType<T>()
+ {
+ Type pluggedType = typeof (T);
+ foreach (KeyValuePair<string, InstanceBuilder> pair in _instanceBuilders)
+ {
+ InstanceBuilder builder = pair.Value;
+ if (builder.IsType(pluggedType))
+ {
+ return new MemoryInstanceMemento(builder.ConcreteTypeKey, builder.ConcreteTypeKey);
+ }
+ }
+
+ Plugin plugin = Plugin.CreateImplicitPlugin(typeof (T));
+ processPlugins(new Plugin[]{plugin});
+ plugin.AddToSource(_source);
+
+ return plugin.CreateImplicitMemento();
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/InstanceManager.cs
===================================================================
--- trunk/Source/StructureMap/InstanceManager.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/InstanceManager.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -1,7 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
-using NMock;
+using StructureMap.Configuration.DSL;
using StructureMap.Exceptions;
using StructureMap.Graph;
using StructureMap.Interceptors;
@@ -14,7 +14,6 @@
public class InstanceManager : IInstanceManager, IEnumerable
{
private Dictionary<Type, IInstanceFactory> _factories;
- private Dictionary<Type, InstanceFactory> _filledTypeFactories;
private bool _failOnException = true;
private GenericsPluginGraph _genericsGraph;
private InstanceDefaultManager _defaultManager;
@@ -25,7 +24,6 @@
public InstanceManager()
{
_factories = new Dictionary<Type, IInstanceFactory>();
- _filledTypeFactories = new Dictionary<Type, InstanceFactory>();
_genericsGraph = new GenericsPluginGraph();
}
@@ -325,7 +323,12 @@
throw new StructureMapException(230);
}
- InstanceFactory factory = getFilledTypeFactory(type);
+ IInstanceFactory factory = getOrCreateFactory(type, delegate(Type t)
+ {
+ PluginFamily family =
+ PluginFamily.CreateAutoFilledPluginFamily(t);
+ return new InstanceFactory(family, true);
+ });
return factory.GetInstance();
}
@@ -334,118 +337,27 @@
return (T) FillDependencies(typeof (T));
}
- private InstanceFactory getFilledTypeFactory(Type type)
+ private delegate InstanceFactory CreateFactoryDelegate(Type type);
+
+ private IInstanceFactory getOrCreateFactory(Type type, CreateFactoryDelegate createFactory)
{
- if (!_filledTypeFactories.ContainsKey(type))
+ if (!_factories.ContainsKey(type))
{
lock (this)
{
- if (!_filledTypeFactories.ContainsKey(type))
+ if (!_factories.ContainsKey(type))
{
- PluginFamily family = PluginFamily.CreateAutoFilledPluginFamily(type);
- InstanceFactory factory = new InstanceFactory(family, true);
+ InstanceFactory factory = createFactory(type);
factory.SetInstanceManager(this);
- _filledTypeFactories.Add(type, factory);
+ _factories.Add(type, factory);
}
}
}
- return _filledTypeFactories[type];
+ return _factories[type];
}
- #region mocking
-
/// <summary>
- /// When called, returns an NMock.IMock instance for the TargetType. Until UnMocked, calling
- /// GetInstance(Type TargetType) will return the MockInstance member of the IMock
- /// </summary>
- /// <param name="TargetType"></param>
- /// <returns></returns>
- public IMock Mock(Type TargetType)
- {
- if (IsMocked(TargetType))
- {
- string msg = string.Format("The Type {0} is already mocked", TargetType.AssemblyQualifiedName);
- throw new InvalidOperationException(msg);
- }
-
- IInstanceFactory factory = this[TargetType];
- MockInstanceFactory mockFactory = new MockInstanceFactory(factory);
- IMock returnValue = mockFactory.GetMock();
-
- lock (this)
- {
- this[TargetType] = mockFactory;
- }
-
- return returnValue;
- }
-
- /// <summary>
- /// Is the specified TargetType currently setup as an IMock
- /// </summary>
- /// <param name="TargetType"></param>
- /// <returns></returns>
- public bool IsMocked(Type TargetType)
- {
- return isInstanceFamilyMocked(this[TargetType]);
- }
-
- private static bool isInstanceFamilyMocked(IInstanceFactory instanceFactory)
- {
- bool returnValue = false;
-
- InstanceFactoryInterceptor interceptor = instanceFactory as InstanceFactoryInterceptor;
- if (interceptor != null)
- {
- returnValue = interceptor.IsMockedOrStubbed;
- }
-
- return returnValue;
- }
-
- /// <summary>
- /// Release the NMock behavior of TargetType
- /// </summary>
- /// <param name="TargetType"></param>
- public void UnMock(Type TargetType)
- {
- if (IsMocked(TargetType))
- {
- InstanceFactoryInterceptor instanceFactory = (InstanceFactoryInterceptor) this[TargetType];
- IInstanceFactory innerFactory = instanceFactory.InnerInstanceFactory;
- lock (this)
- {
- this[TargetType] = innerFactory;
- }
- }
- }
-
- /// <summary>
- /// Calls UnMock() on all IInstanceFactory's
- /// </summary>
- public void UnMockAll()
- {
- ArrayList typeList = new ArrayList();
- lock (this)
- {
- foreach (IInstanceFactory factory in _factories.Values)
- {
- if (isInstanceFamilyMocked(factory))
- {
- typeList.Add(factory.PluginType);
- }
- }
-
-
- foreach (Type type in typeList)
- {
- UnMock(type);
- }
- }
- }
-
- /// <summary>
/// Sets up the InstanceManager to return the object in the "stub" argument anytime
/// any instance of the PluginType is requested
/// </summary>
@@ -459,21 +371,15 @@
stub.GetType().FullName);
}
- IInstanceFactory innerFactory = this[pluginType];
- StubbedInstanceFactory stubbedFactory = new StubbedInstanceFactory(innerFactory, stub);
- lock (this)
- {
- this[pluginType] = stubbedFactory;
- }
+ LiteralMemento memento = new LiteralMemento(stub);
+ this[pluginType].SetDefault(memento);
}
- public void Inject<T>(T instance)
+ public void InjectStub<T>(T instance)
{
InjectStub(typeof (T), instance);
}
- #endregion
-
public IEnumerator GetEnumerator()
{
return _factories.Values.GetEnumerator();
@@ -503,5 +409,31 @@
Profile defaultProfile = _defaultManager.CalculateDefaults(machineName, profile);
SetDefaults(defaultProfile);
}
+
+ public void AddInstance<T>(InstanceMemento memento)
+ {
+ IInstanceFactory factory = getOrCreateFactory(typeof (T), createFactory);
+ factory.AddInstance(memento);
+ }
+
+ public void AddInstance<PLUGINTYPE, CONCRETETYPE>()
+ {
+ IInstanceFactory factory = getOrCreateFactory(typeof(PLUGINTYPE), createFactory);
+ InstanceMemento memento = factory.AddType<CONCRETETYPE>();
+ factory.AddInstance(memento);
+ }
+
+ private InstanceFactory createFactory(Type pluggedType)
+ {
+ PluginFamily family = new PluginFamily(pluggedType);
+ return new InstanceFactory(family, true);
+ }
+
+ public void AddDefaultInstance<PLUGINTYPE, CONCRETETYPE>()
+ {
+ IInstanceFactory factory = getOrCreateFactory(typeof (PLUGINTYPE), createFactory);
+ InstanceMemento memento = factory.AddType<CONCRETETYPE>();
+ factory.SetDefault(memento);
+ }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Interceptors/InstanceFactoryInterceptor.cs
===================================================================
--- trunk/Source/StructureMap/Interceptors/InstanceFactoryInterceptor.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/Interceptors/InstanceFactoryInterceptor.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -15,10 +15,6 @@
private IInstanceFactory _innerInstanceFactory;
- public InstanceFactoryInterceptor()
- {
- }
-
public virtual IInstanceFactory InnerInstanceFactory
{
get { return _innerInstanceFactory; }
@@ -112,6 +108,16 @@
return InnerInstanceFactory.GetAllInstances();
}
+ public void AddInstance(InstanceMemento memento)
+ {
+ InnerInstanceFactory.AddInstance(memento);
+ }
+
+ public InstanceMemento AddType<T>()
+ {
+ return InnerInstanceFactory.AddType<T>();
+ }
+
/// <summary>
/// Declares whether or not the interceptor creates a stubbed or mocked version of the PluginType
/// </summary>
Modified: trunk/Source/StructureMap/MemoryInstanceMemento.cs
===================================================================
--- trunk/Source/StructureMap/MemoryInstanceMemento.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/MemoryInstanceMemento.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -1,8 +1,16 @@
using System.Collections;
using System.Collections.Specialized;
+using StructureMap.Graph;
namespace StructureMap
{
+ public class GenericMemento<T> : MemoryInstanceMemento
+ {
+ public GenericMemento(string instanceKey)
+ : base(Plugin.CreateImplicitPlugin(typeof(T)).ConcreteKey, instanceKey)
+ {}
+ }
+
/// <summary>
/// An in-memory implementation of InstanceMemento.
/// </summary>
@@ -59,6 +67,7 @@
{
}
+
/// <summary>
/// Constructs a MemoryInstanceMemento with properties
/// </summary>
Deleted: trunk/Source/StructureMap/MockInstanceFactory.cs
===================================================================
--- trunk/Source/StructureMap/MockInstanceFactory.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/MockInstanceFactory.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -1,72 +0,0 @@
-using NMock;
-using StructureMap.Interceptors;
-
-namespace StructureMap
-{
- /// <summary>
- /// Implementation of IInstanceFactory that uses NMock internally to create instances of the
- /// PluginType
- /// </summary>
- public class MockInstanceFactory : InstanceFactoryInterceptor
- {
- private IMock _mockType;
-
- /// <summary>
- /// Creates a MockInstanceFactory that wraps and intercepts calls to the innerFactory
- /// </summary>
- /// <param name="innerFactory"></param>
- public MockInstanceFactory(IInstanceFactory innerFactory)
- {
- InnerInstanceFactory = innerFactory;
- _mockType = new DynamicMock(innerFactory.PluginType);
- }
-
- /// <summary>
- /// Returns "new DyanamicProxy(this.PluginType) as IMock"
- /// </summary>
- /// <returns></returns>
- public IMock GetMock()
- {
- return _mockType;
- }
-
- /// <summary>
- /// See <cref>IInstanceFactory</cref>
- /// </summary>
- /// <param name="InstanceKey"></param>
- /// <returns></returns>
- public override object GetInstance(string InstanceKey)
- {
- return _mockType.MockInstance;
- }
-
- /// <summary>
- /// See <cref>IInstanceFactory</cref>
- /// </summary>
- /// <param name="Memento"></param>
- /// <returns></returns>
- public override object GetInstance(InstanceMemento Memento)
- {
- return _mockType.MockInstance;
- }
-
- /// <summary>
- /// See <cref>IInstanceFactory</cref>
- /// </summary>
- /// <returns></returns>
- public override object GetInstance()
- {
- return _mockType.MockInstance;
- }
-
- public override bool IsMockedOrStubbed
- {
- get { return true; }
- }
-
- public override object Clone()
- {
- return MemberwiseClone();
- }
- }
-}
\ No newline at end of file
Modified: trunk/Source/StructureMap/ObjectFactory.cs
===================================================================
--- trunk/Source/StructureMap/ObjectFactory.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/ObjectFactory.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Security.Permissions;
using System.Text;
-using NMock;
+using StructureMap.Configuration.DSL;
using StructureMap.Graph;
namespace StructureMap
@@ -109,14 +109,9 @@
{
lock (_lockObject)
{
- manager.UnMockAll();
Profile = string.Empty;
}
}
- catch (StructureMapException)
- {
- throw;
- }
catch (TypeInitializationException ex)
{
if (ex.InnerException is StructureMapException)
@@ -286,62 +281,99 @@
return (T) _manager.FillDependencies(typeof (T));
}
- #region Mocking
-
/// <summary>
- /// When called, returns an NMock.IMock instance for the TargetType. Until UnMocked, calling
- /// GetInstance(Type TargetType) will return the MockInstance member of the IMock
+ /// Sets up StructureMap to return the object in the "stub" argument anytime
+ /// any instance of the PluginType is requested
/// </summary>
- /// <param name="TargetType"></param>
- /// <returns></returns>
- public static IMock Mock(Type TargetType)
+ /// <param name="pluginType"></param>
+ /// <param name="stub"></param>
+ public static void InjectStub(Type pluginType, object stub)
{
- return manager.Mock(TargetType);
+ manager.InjectStub(pluginType, stub);
}
/// <summary>
- /// Sets up the internal InstanceManager to return the object in the "stub" argument anytime
+ /// Sets up StructureMap to return the object in the "stub" argument anytime
/// any instance of the PluginType is requested
/// </summary>
/// <param name="targetType"></param>
/// <param name="stub"></param>
- public static void InjectStub(Type targetType, object stub)
+ public static void InjectStub<PLUGINTYPE>(PLUGINTYPE stub)
{
- manager.InjectStub(targetType, stub);
+ manager.InjectStub(typeof (PLUGINTYPE), stub);
}
+
+ 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();
+ }
+
/// <summary>
- /// Is the specified TargetType currently setup as an IMock
+ /// Sets the default instance of PLUGINTYPE to the object in the instance argument
/// </summary>
- /// <param name="TargetType"></param>
- /// <returns></returns>
- public static bool IsMocked(Type TargetType)
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <param name="instance"></param>
+ public static void Inject<PLUGINTYPE>(PLUGINTYPE instance)
{
- return manager.IsMocked(TargetType);
+ LiteralMemento memento = new LiteralMemento(instance);
+ manager.AddInstance<PLUGINTYPE>(memento);
+ manager.SetDefault(typeof (PLUGINTYPE), memento);
}
/// <summary>
- /// Release the NMock behavior of TargetType
+ /// Injects a new instance of PLUGINTYPE by name.
/// </summary>
- /// <param name="TargetType"></param>
- public static void UnMock(Type TargetType)
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <param name="instance"></param>
+ /// <param name="instanceKey"></param>
+ public static void InjectByName<PLUGINTYPE>(PLUGINTYPE instance, string instanceKey)
{
- manager.UnMock(TargetType);
+ LiteralMemento memento = new LiteralMemento(instance).Named(instanceKey);
+ manager.AddInstance<PLUGINTYPE>(memento);
}
- #endregion
+ /// <summary>
+ /// Injects a new instance of CONCRETETYPE to PLUGINTYPE by name.
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <param name="instance"></param>
+ /// <param name="instanceKey"></param>
+ public static void InjectByName<PLUGINTYPE, CONCRETETYPE>(string instanceKey)
+ {
+ manager.AddInstance<PLUGINTYPE, CONCRETETYPE>();
+ GenericMemento<CONCRETETYPE> memento = new GenericMemento<CONCRETETYPE>(instanceKey);
+ manager.AddInstance<PLUGINTYPE>(memento);
+ }
- public static string WhatDoIHave()
+ /// <summary>
+ /// StructureMap will return an instance of CONCRETETYPE whenever
+ /// a PLUGINTYPE is requested
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <typeparam name="CONCRETETYPE"></typeparam>
+ public static void InjectDefaultType<PLUGINTYPE, CONCRETETYPE>() where CONCRETETYPE : PLUGINTYPE
{
- StringBuilder sb = new StringBuilder();
+ manager.AddDefaultInstance<PLUGINTYPE, CONCRETETYPE>();
+ }
- foreach (IInstanceFactory factory in manager)
- {
- sb.AppendFormat("PluginType {0}, Default: {1}\r\n", factory.PluginType.AssemblyQualifiedName,
- factory.DefaultInstanceKey);
- }
-
- return sb.ToString();
+ /// <summary>
+ /// Adds a new CONCRETETYPE to StructureMap so that an instance of CONCRETETYPE
+ /// will be returned from a call to ObjectFactory.GetAllInstances<PLUGINTYPE>()
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <typeparam name="CONCRETETYPE"></typeparam>
+ public static void AddType<PLUGINTYPE, CONCRETETYPE>() where CONCRETETYPE : PLUGINTYPE
+ {
+ manager.AddInstance<PLUGINTYPE, CONCRETETYPE>();
}
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/StructureMap.csproj
===================================================================
--- trunk/Source/StructureMap/StructureMap.csproj 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/StructureMap.csproj 2007-12-14 11:16:38 UTC (rev 49)
@@ -96,10 +96,6 @@
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
- <Reference Include="nmock">
- <Name>nmock</Name>
- <HintPath>..\..\bin\nmock.dll</HintPath>
- </Reference>
<Reference Include="System">
<Name>System</Name>
</Reference>
@@ -214,6 +210,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Configuration\DSL\ChildInstanceExpression.cs" />
+ <Compile Include="Configuration\DSL\ConstructorExpression.cs" />
<Compile Include="Configuration\DSL\CreatePluginFamilyExpression.cs" />
<Compile Include="Configuration\DSL\ExpressionValidator.cs" />
<Compile Include="Configuration\DSL\IExpression.cs" />
@@ -335,6 +332,7 @@
<Compile Include="Configuration\XmlConstants.cs">
<SubType>Code</SubType>
</Compile>
+ <Compile Include="ConstructorMemento.cs" />
<Compile Include="DeploymentTasks\DeploymentConfiguration.cs">
<SubType>Code</SubType>
</Compile>
@@ -498,9 +496,6 @@
<Compile Include="MemoryInstanceMemento.cs">
<SubType>Code</SubType>
</Compile>
- <Compile Include="MockInstanceFactory.cs">
- <SubType>Code</SubType>
- </Compile>
<Compile Include="ObjectFactory.cs">
<SubType>Code</SubType>
</Compile>
@@ -556,9 +551,6 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="StructureMapConfiguration.cs" />
- <Compile Include="StubbedInstanceFactory.cs">
- <SubType>Code</SubType>
- </Compile>
<Compile Include="Verification\IStartUp.cs" />
<Compile Include="Verification\PluginGraphConsoleWriter.cs">
<SubType>Code</SubType>
Modified: trunk/Source/StructureMap/StructureMapException.resx
===================================================================
--- trunk/Source/StructureMap/StructureMapException.resx 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/StructureMapException.resx 2007-12-14 11:16:38 UTC (rev 49)
@@ -249,4 +249,7 @@
<data name="105" xml:space="preserve">
<value>The <{0}> section could not be loaded from the application configuration file.</value>
</data>
+ <data name="306" xml:space="preserve">
+ <value>The configured BuilderDelegate of type {0} does not match the PluginFamily type {1}</value>
+ </data>
</root>
\ No newline at end of file
Deleted: trunk/Source/StructureMap/StubbedInstanceFactory.cs
===================================================================
--- trunk/Source/StructureMap/StubbedInstanceFactory.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap/StubbedInstanceFactory.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -1,59 +0,0 @@
-using StructureMap.Interceptors;
-
-namespace StructureMap
-{
- /// <summary>
- /// Decorator that intercepts a call to create a PluginType and returns a
- /// static mock or stub. Used when ObjectFactory.InjectStub(Type, object) is called.
- /// </summary>
- public class StubbedInstanceFactory : InstanceFactoryInterceptor
- {
- private readonly object _stub;
-
- public StubbedInstanceFactory(IInstanceFactory innerFactory, object stub) : base()
- {
- InnerInstanceFactory = innerFactory;
- _stub = stub;
- }
-
- /// <summary>
- /// Creates an object instance for the InstanceKey
- /// </summary>
- /// <param name="InstanceKey">The named instance</param>
- /// <returns></returns>
- public override object GetInstance(string InstanceKey)
- {
- return _stub;
- }
-
- /// <summary>
- /// Creates an object instance directly from the Memento
- /// </summary>
- /// <param name="Memento">A representation of an object instance</param>
- /// <returns></returns>
- public override object GetInstance(InstanceMemento Memento)
- {
- return _stub;
- }
-
- /// <summary>
- /// Creates a new object instance of the default instance memento
- /// </summary>
- /// <returns></returns>
- public override object GetInstance()
- {
- return _stub;
- }
-
-
- public override bool IsMockedOrStubbed
- {
- get { return true; }
- }
-
- public override object Clone()
- {
- return MemberwiseClone();
- }
- }
-}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/ConstructorExpressionTester.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -0,0 +1,100 @@
+using NUnit.Framework;
+using Rhino.Mocks;
+using StructureMap.Configuration.DSL;
+using StructureMap.Testing.Container;
+
+namespace StructureMap.Testing.Configuration.DSL
+{
+ [TestFixture]
+ public class ConstructorExpressionTester
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ StructureMapConfiguration.ResetAll();
+ ObjectFactory.Reset();
+ }
+
+ public interface Abstraction { }
+
+ public class Concretion : Abstraction { }
+
+ [Test]
+ public void ConstructSomething()
+ {
+ Concretion concretion = new Concretion();
+
+ Registry registry = new Registry();
+ registry.ForRequestedType<Abstraction>().TheDefaultIs(
+ Registry.ConstructedBy<Abstraction>(delegate { return concretion; })
+ );
+
+ IInstanceManager manager = registry.BuildInstanceManager();
+ Assert.AreSame(concretion, manager.CreateInstance<Abstraction>());
+ }
+
+ [Test]
+ public void ConstructSomethingNotByDefault()
+ {
+ Concretion concretion = new Concretion();
+
+ Registry registry = new Registry();
+ registry.ForRequestedType<Abstraction>().AddInstance(
+ Registry.ConstructedBy<Abstraction>(delegate { return concretion; })
+ );
+
+
+ IInstanceManager manager = registry.BuildInstanceManager();
+
+ Abstraction actual = manager.GetAllInstances<Abstraction>()[0];
+ Assert.AreSame(concretion, actual);
+
+ }
+
+ [Test]
+ public void ConstructSomethingByName()
+ {
+ Concretion concretion1 = new Concretion();
+ Concretion concretion2 = new Concretion();
+
+ Registry registry = new Registry();
+ registry.ForRequestedType<Abstraction>().AddInstance(
+ Registry.ConstructedBy<Abstraction>(delegate { return concretion1; }).WithName("One")
+ );
+
+ registry.ForRequestedType<Abstraction>().AddInstance(
+ Registry.ConstructedBy<Abstraction>(delegate { return concretion2; }).WithName("Two")
+ );
+
+ IInstanceManager manager = registry.BuildInstanceManager();
+
+ Assert.AreSame(concretion1, manager.CreateInstance<Abstraction>("One"));
+ Assert.AreSame(concretion2, manager.CreateInstance<Abstraction>("Two"));
+
+ }
+
+ [Test]
+ public void AddTwoConstructorsConsecutively()
+ {
+ Concretion concretion1 = new Concretion();
+ Concretion concretion2 = new Concretion();
+
+ Registry registry = new Registry();
+ registry.ForRequestedType<Abstraction>()
+ .AddInstance(
+ Registry.ConstructedBy<Abstraction>(delegate { return concretion1; }).WithName("One")
+ )
+ .AddInstance(
+ Registry.ConstructedBy<Abstraction>(delegate { return concretion2; }).WithName("Two")
+ );
+
+ IInstanceManager manager = registry.BuildInstanceManager();
+
+ Assert.AreSame(concretion1, manager.CreateInstance<Abstraction>("One"));
+ Assert.AreSame(concretion2, manager.CreateInstance<Abstraction>("Two"));
+
+ }
+ }
+
+
+}
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs 2007-07-27 13:43:55 UTC (rev 48)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryIntegratedTester.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -82,5 +82,6 @@
Assert.Contains("Brown", colors);
Assert.Contains("Black", colors);
}
+
}
}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/ConstructorMementoTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/ConstructorMementoTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/ConstructorMementoTester.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -0,0 +1,23 @@
+using NUnit.Framework;
+using Rhino.Mocks;
+
+namespace StructureMap.Testing
+{
+ [TestFixture]
+ public class ConstructorMementoTester
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ }
+
+ [Test]
+ public void Construct()
+ {
+ ConstructorMemento<string> memento = new ConstructorMemento<string>("A", delegate { return "Hello"; });
+ Assert.AreEqual("A", memento.InstanceKey);
+ string actual = (string) memento.Build(null);
+ Assert.AreEqual("Hello", actual);
+ }
+ }
+}
Added: trunk/Source/StructureMap.Testing/Container/DynamicInjectionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/DynamicInjectionTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Container/DynamicInjectionTester.cs 2007-12-14 11:16:38 UTC (rev 49)
@@ -0,0 +1,239 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using NUnit.Framework;
+using Rhino.Mocks;
+using StructureMap.Configuration.DSL;
+using StructureMap.Graph;
+using StructureMap.Interceptors;
+using StructureMap.Testing.Widget3;
+
+namespace StructureMap.Testing.Container
+{
+ [TestFixture]
+ public class DynamicInjectionTester
+ {
+ private IService _red = new ColorService("Red");
+ private IService _blue = new ColorService("Blue");
+ private IService _orange = new ColorService("Orange");
+
+ [SetUp]
+ public void SetUp()
+ {
+ ObjectFactory.ReInitialize();
+ StructureMapConfiguration.ResetAll();
+ }
+
+ [Test]
+ public void CanAddMementosDirectlyToAnInstanceFactory()
+ {
+ InstanceFactory factory = ObjectMother.Factory<IService>();
+
+ factory.AddInstance(new LiteralMemento(_red).Named("Red"));
+ factory.AddInstance(new LiteralMemento(_blue).Named("Blue"));
+
+ Assert.AreSame(_red, factory.GetInstance("Red"));
+ Assert.AreSame(_blue, factory.GetInstance("Blue"));
+ }
+
+ [Test]
+ public void NowOverwriteAPreviouslyAttachedMemento()
+ {
+ InstanceFactory factory = ObjectMother.Factory<IService>();
+
+ factory.AddInstance(new LiteralMemento(_red).Named("Red"));
+ factory.AddInstance(new LiteralMemento(_blue).Named("Blue"));
+
+ // Replace Blue
+ factory.AddInstance(new LiteralMemento(_orange).Named("Blue"));
+
+ Assert.AreSame(_orange, factory.GetInstance("Blue"));
+ }
+
+ [Test]
+ public void AddInstanceWithInstanceFactoryInterceptor()
+ {
+ InstanceFactoryInterceptor interceptor = new FakeInstanceFactoryInterceptor();
+
+ InstanceFactory factory = ObjectMother.Factory<IService>();
+ interceptor.InnerInstanceFactory = factory;
+
+ interceptor.AddInstance(new LiteralMemento(_red).Named("Red"));
+ interceptor.AddInstance(new LiteralMemento(_blue).Named("Blue"));
+
+ Assert.AreSame(_red, interceptor.GetInstance("Red"));
+ Assert.AreSame(_blue, interceptor.GetInstance("Blue"));
+ }
+
+ [Test]
+ public void AddInstanceToInstanceManagerWhenTheInstanceFactoryDoesNotExist()
+ {
+ InstanceManager manager = new InstanceManager();
+ manager.AddInstance<IService>(new LiteralMemento(_red).Named("Red"));
+ manager.AddInstance<IService>(new LiteralMemento(_blue).Named("Blue"));
+
+ Assert.AreSame(_red, manager.CreateInstance(typeof(IService), "Red"));
+ Assert.AreSame(_blue, manager.CreateInstance(typeof(IService), "Blue"));
+
+ }
+
+ [Test]
+ public void AddInstanceFromObjectFactory()
+ {
+ SomethingOne one = new SomethingOne();
+ ObjectFactory.Inject<ISomething>(one);
+
+ Assert.AreSame(one, ObjectFactory.GetInstance<ISomething>());
+ }
+
+ [Test]
+ public void OverwriteInstanceFromObjectFactory()
+ {
+ SomethingOne one = new SomethingOne();
+ SomethingOne two = new SomethingOne();
+ ObjectFactory.Inject<ISomething>(one);
+ ObjectFactory.Inject<ISomething>(two);
+
+ Assert.AreSame(two, ObjectFactory.GetInstance<ISomething>());
+ }
+
+ [Test]
+ public void AddNamedInstanceToobjectFactory()
+ {
+ SomethingOne one = new SomethingOne();
+ SomethingOne two = new SomethingOne();
+
+ ObjectFactory.InjectByName<ISomething>(one, "One");
+ ObjectFactory.InjectByName<ISomething>(two, "Two");
+
+
+ Assert.AreSame(one, ObjectFactory.GetNamedInstance<ISomething>("One"));
+ Assert.AreSame(two, ObjectFactory.GetNamedInstance<ISomething>("Two"));
+ }
+
+ [Test]
+ public void AddNamedInstanceByType()
+ {
+ ObjectFactory.InjectByName<ISomething, SomethingOne>("One");
+ ObjectFactory.InjectByName<ISomething, SomethingTwo>("Two");
+
+ Assert.IsInstanceOfType(typeof(SomethingOne), ObjectFactory.GetNamedInstance<ISomething>("One"));
+ Assert.IsInstanceOfType(typeof(SomethingTwo), ObjectFactory.GetNamedInstance<ISomething>("Two"));
+ }
+
+ [Test]
+ public void OverwriteInstanceAndDontBlowUp()
+ {
+ ObjectFactory.InjectByName<ISomething, SomethingOne>("One");
+ ObjectFactory.InjectByName<ISomething, SomethingTwo>("One");
+
+ Assert.IsInstanceOfType(typeof(SomethingTwo), ObjectFactory.GetNamedInstance<ISomething>("One"));
+ }
+
+ [Test]
+ public void JustAddATypeWithNoNameAndDefault()
+ {
+ ObjectFactory.InjectDefaultType<ISomething, SomethingOne>();
+ Assert.IsInstanceOfType(typeof(SomethingOne), ObjectFactory.GetInstance<ISomething>());
+ }
+
+ [Test]
+...
[truncated message content] |