|
From: <jer...@us...> - 2008-05-27 16:04:37
|
Revision: 101
http://structuremap.svn.sourceforge.net/structuremap/?rev=101&view=rev
Author: jeremydmiller
Date: 2008-05-27 08:36:33 -0700 (Tue, 27 May 2008)
Log Message:
-----------
adding convenience methods for registering instances
Modified Paths:
--------------
trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs
trunk/Source/StructureMap/Configuration/DSL/Registry.cs
trunk/Source/StructureMap/Delegates.cs
trunk/Source/StructureMap/InstanceManager.cs
trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs
trunk/Source/StructureMap/Interceptors/InterceptorLibrary.cs
trunk/Source/StructureMap/StructureMap.csproj
trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs
trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
Added Paths:
-----------
trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs
trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs
trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs
trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs
Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/Configuration/DSL/Expressions/CreatePluginFamilyExpression.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -109,7 +109,7 @@
_children.Add(
delegate(PluginGraph graph)
{
- InterceptionFunction function = delegate(object target)
+ Func<object, object> function = delegate(object target)
{
handler((PLUGINTYPE) target);
return target;
@@ -127,7 +127,7 @@
_children.Add(
delegate(PluginGraph graph)
{
- InterceptionFunction function = delegate(object target) { return handler((PLUGINTYPE) target); };
+ Func<object, object> function = delegate(object target) { return handler((PLUGINTYPE) target); };
PluginTypeInterceptor interceptor = new PluginTypeInterceptor(typeof (PLUGINTYPE), function);
graph.InterceptorLibrary.AddInterceptor(interceptor);
@@ -178,5 +178,29 @@
return this;
}
+
+ public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIs(PLUGINTYPE @object)
+ {
+ return TheDefaultIs(new LiteralInstance(@object));
+ }
+
+ public CreatePluginFamilyExpression<PLUGINTYPE> TheDefaultIs(Func<PLUGINTYPE> func)
+ {
+ ConstructorInstance instance = new ConstructorInstance(delegate() { return func(); });
+ return TheDefaultIs(instance);
+ }
+
+ public CreatePluginFamilyExpression<PLUGINTYPE> AddInstance(PLUGINTYPE @object)
+ {
+ LiteralInstance instance = new LiteralInstance(@object);
+ return AddInstance(instance);
+ }
+
+ public CreatePluginFamilyExpression<PLUGINTYPE> AddInstance(Func<PLUGINTYPE> func)
+ {
+ ConstructorInstance instance = new ConstructorInstance(delegate(){ return func();});
+ return AddInstance(instance);
+ }
+
}
}
\ No newline at end of file
Added: trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs (rev 0)
+++ trunk/Source/StructureMap/Configuration/DSL/Expressions/GenericFamilyExpression.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -0,0 +1,102 @@
+using System;
+using StructureMap.Attributes;
+using StructureMap.Graph;
+using StructureMap.Interceptors;
+using StructureMap.Pipeline;
+
+namespace StructureMap.Configuration.DSL.Expressions
+{
+ public class GenericFamilyExpression
+ {
+ private readonly Type _pluginType;
+ private readonly Registry _registry;
+
+ public GenericFamilyExpression(Type pluginType, Registry registry)
+ {
+ _pluginType = pluginType;
+ _registry = registry;
+ }
+
+ private GenericFamilyExpression alterAndContinue(Action<PluginFamily> action)
+ {
+ _registry.addExpression(delegate(PluginGraph graph)
+ {
+ PluginFamily family = graph.FindFamily(_pluginType);
+ action(family);
+ });
+
+ return this;
+ }
+
+ public GenericFamilyExpression TheDefaultIsConcreteType(Type concreteType)
+ {
+ ConfiguredInstance instance = new ConfiguredInstance(concreteType);
+ return TheDefaultIs(instance);
+ }
+
+ public GenericFamilyExpression TheDefaultIs(Instance instance)
+ {
+ return alterAndContinue(delegate(PluginFamily family)
+ {
+ family.AddInstance(instance);
+ family.DefaultInstanceKey = instance.Name;
+ });
+ }
+
+ public GenericFamilyExpression TheDefaultIs(Func<object> func)
+ {
+ ConstructorInstance instance = new ConstructorInstance(func);
+ return TheDefaultIs(instance);
+ }
+
+ public GenericFamilyExpression AddInstance(Instance instance)
+ {
+ return alterAndContinue(delegate(PluginFamily family) { family.AddInstance(instance); });
+ }
+
+ public GenericFamilyExpression CacheBy(InstanceScope scope)
+ {
+ return alterAndContinue(delegate(PluginFamily family) { family.SetScopeTo(scope); });
+ }
+
+ public GenericFamilyExpression OnCreation(Action<object> action)
+ {
+ Func<object, object> func = delegate(object raw)
+ {
+ action(raw);
+ return raw;
+ };
+ return EnrichWith(func);
+ }
+
+ public GenericFamilyExpression EnrichWith(Func<object, object> func)
+ {
+ _registry.addExpression(delegate(PluginGraph graph)
+ {
+ PluginTypeInterceptor interceptor = new PluginTypeInterceptor(_pluginType, func);
+ graph.InterceptorLibrary.AddInterceptor(interceptor);
+ });
+
+ return this;
+ }
+
+
+ public GenericFamilyExpression InterceptConstructionWith(IBuildInterceptor interceptor)
+ {
+ return alterAndContinue(delegate(PluginFamily family)
+ {
+ family.AddInterceptor(interceptor);
+ });
+ }
+
+ public GenericFamilyExpression AddConcreteType(Type concreteType)
+ {
+ return AddInstance(new ConfiguredInstance(concreteType));
+ }
+
+ public GenericFamilyExpression AddConcreteType(Type concreteType, string instanceName)
+ {
+ return AddInstance(new ConfiguredInstance(concreteType).WithName(instanceName));
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -7,7 +7,7 @@
namespace StructureMap.Configuration.DSL
{
- public class Registry : IDisposable
+ public class Registry : RegistryExpressions, IDisposable
{
private readonly List<Action<PluginGraph>> _actions = new List<Action<PluginGraph>>();
private readonly PluginGraph _graph;
@@ -67,6 +67,12 @@
return new CreatePluginFamilyExpression<PLUGINTYPE>(this);
}
+
+ public GenericFamilyExpression ForRequestedType(Type pluginType)
+ {
+ return new GenericFamilyExpression(pluginType, this);
+ }
+
/// <summary>
/// Direct StructureMap to build instances of type T, and look for concrete classes
/// marked with the [Pluggable] attribute that implement type T.
@@ -114,41 +120,6 @@
/// <summary>
- /// Convenience method to start the definition of an instance of type T
- /// </summary>
- /// <typeparam name="PLUGGEDTYPE"></typeparam>
- /// <returns></returns>
- public static ConfiguredInstance Instance<PLUGGEDTYPE>()
- {
- ConfiguredInstance instance = new ConfiguredInstance();
- instance.PluggedType = typeof (PLUGGEDTYPE);
-
- return instance;
- }
-
- /// <summary>
- /// Convenience method to register a prototype instance
- /// </summary>
- /// <typeparam name="PLUGINTYPE"></typeparam>
- /// <param name="prototype"></param>
- /// <returns></returns>
- public static PrototypeInstance Prototype<PLUGINTYPE>(PLUGINTYPE prototype)
- {
- return new PrototypeInstance((ICloneable) prototype);
- }
-
- /// <summary>
- /// Convenience method to register a preconfigured instance of type T
- /// </summary>
- /// <typeparam name="PLUGINTYPE"></typeparam>
- /// <param name="instance"></param>
- /// <returns></returns>
- public static LiteralInstance Object<PLUGINTYPE>(PLUGINTYPE instance)
- {
- return new LiteralInstance(instance);
- }
-
- /// <summary>
/// Registers a preconfigured instance
/// </summary>
/// <typeparam name="PLUGINTYPE"></typeparam>
@@ -214,33 +185,6 @@
return (type.GetConstructor(new Type[0]) != null);
}
- /// <summary>
- /// Registers a UserControl as an instance
- /// </summary>
- /// <typeparam name="PLUGINTYPE"></typeparam>
- /// <param name="url"></param>
- /// <returns></returns>
- public UserControlInstance LoadControlFromUrl<PLUGINTYPE>(string url)
- {
- UserControlInstance instance = new UserControlInstance(url);
-
- PluginFamily family = _graph.FindFamily(typeof (PLUGINTYPE));
- family.AddInstance(instance);
-
- return instance;
- }
-
- public static ConstructorInstance ConstructedBy<PLUGINTYPE>
- (Func<PLUGINTYPE> builder)
- {
- return new ConstructorInstance(delegate() { return builder(); });
- }
-
- public static ReferencedInstance Instance(string referencedKey)
- {
- return new ReferencedInstance(referencedKey);
- }
-
public void RegisterInterceptor(TypeInterceptor interceptor)
{
addExpression(
@@ -276,35 +220,4 @@
_actions.Add(delegate(PluginGraph graph) { graph.FindFamily(typeof (PLUGINTYPE)).AddInstance(instance); });
}
}
-
-
- public class MatchedTypeInterceptor : TypeInterceptor
- {
- private readonly Predicate<Type> _match;
- private InterceptionFunction _interception;
-
- internal MatchedTypeInterceptor(Predicate<Type> match)
- {
- _match = match;
- }
-
- #region TypeInterceptor Members
-
- public bool MatchesType(Type type)
- {
- return _match(type);
- }
-
- public object Process(object target)
- {
- return _interception(target);
- }
-
- #endregion
-
- public void InterceptWith(InterceptionFunction interception)
- {
- _interception = interception;
- }
- }
}
\ No newline at end of file
Added: trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs (rev 0)
+++ trunk/Source/StructureMap/Configuration/DSL/RegistryExpressions.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -0,0 +1,65 @@
+using System;
+using StructureMap.Pipeline;
+
+namespace StructureMap.Configuration.DSL
+{
+ public class RegistryExpressions
+ {
+ /// <summary>
+ /// Convenience method to start the definition of an instance of type T
+ /// </summary>
+ /// <typeparam name="PLUGGEDTYPE"></typeparam>
+ /// <returns></returns>
+ public static ConfiguredInstance Instance<PLUGGEDTYPE>()
+ {
+ ConfiguredInstance instance = new ConfiguredInstance();
+ instance.PluggedType = typeof (PLUGGEDTYPE);
+
+ return instance;
+ }
+
+ /// <summary>
+ /// Convenience method to register a prototype instance
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <param name="prototype"></param>
+ /// <returns></returns>
+ public static PrototypeInstance Prototype<PLUGINTYPE>(PLUGINTYPE prototype)
+ {
+ return new PrototypeInstance((ICloneable) prototype);
+ }
+
+ /// <summary>
+ /// Convenience method to register a preconfigured instance of type T
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <param name="instance"></param>
+ /// <returns></returns>
+ public static LiteralInstance Object<PLUGINTYPE>(PLUGINTYPE instance)
+ {
+ return new LiteralInstance(instance);
+ }
+
+ /// <summary>
+ /// Registers a UserControl as an instance
+ /// </summary>
+ /// <typeparam name="PLUGINTYPE"></typeparam>
+ /// <param name="url"></param>
+ /// <returns></returns>
+ public UserControlInstance LoadControlFromUrl(string url)
+ {
+ return new UserControlInstance(url);
+ }
+
+ public static ConstructorInstance ConstructedBy<PLUGINTYPE>
+ (Func<PLUGINTYPE> builder)
+ {
+ return new ConstructorInstance(delegate() { return builder(); });
+ }
+
+ public static ReferencedInstance Instance(string referencedKey)
+ {
+ return new ReferencedInstance(referencedKey);
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Delegates.cs
===================================================================
--- trunk/Source/StructureMap/Delegates.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/Delegates.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -8,5 +8,6 @@
public delegate void Action<T>(T t);
public delegate void Action<T, T1>(T t, T1 t1);
public delegate T Func<T>();
+ public delegate T1 Func<T, T1>(T t);
}
Modified: trunk/Source/StructureMap/InstanceManager.cs
===================================================================
--- trunk/Source/StructureMap/InstanceManager.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/InstanceManager.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
+using StructureMap.Configuration.DSL;
using StructureMap.Diagnostics;
using StructureMap.Graph;
using StructureMap.Interceptors;
@@ -15,9 +16,17 @@
/// </summary>
public class InstanceManager : TypeRules, IInstanceManager
{
- private readonly InterceptorLibrary _interceptorLibrary;
- private readonly PipelineGraph _pipelineGraph;
+ private InterceptorLibrary _interceptorLibrary;
+ private PipelineGraph _pipelineGraph;
+ public InstanceManager(Action<Registry> action)
+ {
+ Registry registry = new Registry();
+ action(registry);
+
+ construct(registry.Build());
+ }
+
public InstanceManager() : this(new PluginGraph())
{
}
@@ -30,6 +39,11 @@
/// <param name="failOnException">Flags the InstanceManager to fail or trap exceptions</param>
public InstanceManager(PluginGraph pluginGraph)
{
+ construct(pluginGraph);
+ }
+
+ private void construct(PluginGraph pluginGraph)
+ {
_interceptorLibrary = pluginGraph.InterceptorLibrary;
if (!pluginGraph.IsSealed)
Modified: trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs
===================================================================
--- trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/Interceptors/FilteredInstanceInterceptor.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -10,10 +10,10 @@
public class PluginTypeInterceptor : TypeInterceptor
{
- private readonly InterceptionFunction _function;
+ private readonly Func<object, object> _function;
private readonly Type _pluginType;
- public PluginTypeInterceptor(Type pluginType, InterceptionFunction function)
+ public PluginTypeInterceptor(Type pluginType, Func<object, object> function)
{
_pluginType = pluginType;
_function = function;
Modified: trunk/Source/StructureMap/Interceptors/InterceptorLibrary.cs
===================================================================
--- trunk/Source/StructureMap/Interceptors/InterceptorLibrary.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/Interceptors/InterceptorLibrary.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -3,8 +3,6 @@
namespace StructureMap.Interceptors
{
- public delegate object InterceptionFunction(object target);
-
public class InterceptorLibrary
{
public static readonly InterceptorLibrary Empty = new InterceptorLibrary();
Added: trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs
===================================================================
--- trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs (rev 0)
+++ trunk/Source/StructureMap/Interceptors/MatchedTypeInterceptor.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace StructureMap.Interceptors
+{
+ public class MatchedTypeInterceptor : TypeInterceptor
+ {
+ private readonly Predicate<Type> _match;
+ private Func<object, object> _interception;
+
+ internal MatchedTypeInterceptor(Predicate<Type> match)
+ {
+ _match = match;
+ }
+
+ #region TypeInterceptor Members
+
+ public bool MatchesType(Type type)
+ {
+ return _match(type);
+ }
+
+ public object Process(object target)
+ {
+ return _interception(target);
+ }
+
+ #endregion
+
+ public void InterceptWith(Func<object, object> interception)
+ {
+ _interception = interception;
+ }
+ }
+}
Modified: trunk/Source/StructureMap/StructureMap.csproj
===================================================================
--- trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 15:36:33 UTC (rev 101)
@@ -116,6 +116,8 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="BuildSession.cs" />
+ <Compile Include="Configuration\DSL\Expressions\GenericFamilyExpression.cs" />
+ <Compile Include="Configuration\DSL\RegistryExpressions.cs" />
<Compile Include="Configuration\ProfileBuilder.cs" />
<Compile Include="Delegates.cs" />
<Compile Include="Diagnostics\BuildError.cs" />
@@ -140,6 +142,7 @@
<Compile Include="InstanceBuilderList.cs" />
<Compile Include="InstanceCache.cs" />
<Compile Include="InstanceFamily.cs" />
+ <Compile Include="Interceptors\MatchedTypeInterceptor.cs" />
<Compile Include="PipelineGraph.cs" />
<Compile Include="Pipeline\BuildPolicy.cs" />
<Compile Include="Pipeline\CacheInterceptor.cs" />
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -213,6 +213,54 @@
Assert.IsInstanceOfType(typeof (DefaultGateway), gateway);
}
+
+ [Test]
+ public void Set_the_default_to_a_built_object()
+ {
+ AWidget aWidget = new AWidget();
+
+ InstanceManager manager = new InstanceManager(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IWidget>().TheDefaultIs(aWidget);
+ });
+
+ Assert.AreSame(aWidget, manager.CreateInstance<IWidget>());
+ }
+
+ [Test]
+ public void Set_the_default_by_a_lambda()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IWidget>().TheDefaultIs(delegate() { return new AWidget(); });
+ });
+
+ Assert.IsInstanceOfType(typeof(AWidget), manager.CreateInstance<IWidget>());
+ }
+
+ [Test]
+ public void Add_an_instance_by_literal()
+ {
+ AWidget aWidget = new AWidget();
+
+ InstanceManager manager = new InstanceManager(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IWidget>().AddInstance(aWidget);
+ });
+
+ Assert.IsInstanceOfType(typeof(AWidget), manager.GetAllInstances<IWidget>()[0]);
+ }
+
+ [Test]
+ public void Add_an_instance_by_lambda()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry registry)
+ {
+ registry.ForRequestedType<IWidget>().AddInstance(delegate() { return new AWidget(); });
+ });
+
+ Assert.IsInstanceOfType(typeof(AWidget), manager.GetAllInstances<IWidget>()[0]);
+ }
}
public class StubbedInstanceFactoryInterceptor : IBuildInterceptor
Added: trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -0,0 +1,189 @@
+using System;
+using NUnit.Framework;
+using StructureMap.Attributes;
+using StructureMap.Configuration.DSL;
+using StructureMap.Graph;
+using StructureMap.Interceptors;
+using StructureMap.Pipeline;
+
+namespace StructureMap.Testing.Configuration.DSL
+{
+ [TestFixture]
+ public class GenericFamilyExpressionTester : RegistryExpressions
+ {
+ public interface ITarget
+ {
+ }
+
+ public class Target1 : ITarget
+ {
+ }
+
+ public class Target2 : ITarget
+ {
+ }
+
+ public class Target3 : ITarget
+ {
+ }
+
+ public class WrappedTarget : ITarget
+ {
+ private readonly ITarget _inner;
+
+ public WrappedTarget(ITarget target)
+ {
+ _inner = target;
+ }
+
+ public ITarget Inner
+ {
+ get { return _inner; }
+ }
+ }
+
+ [Test]
+ public void Add_concrete_type()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof(ITarget)).AddConcreteType(typeof(Target1));
+ });
+
+
+ Assert.IsInstanceOfType(typeof(Target1), manager.GetAllInstances<ITarget>()[0]);
+ }
+
+ [Test]
+ public void Add_concrete_type_with_name()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof(ITarget)).AddConcreteType(typeof(Target1), "1");
+ r.ForRequestedType(typeof(ITarget)).AddConcreteType(typeof(Target2), "2");
+ r.ForRequestedType(typeof(ITarget)).AddConcreteType(typeof(Target3), "3");
+ });
+
+
+ Assert.IsInstanceOfType(typeof(Target1), manager.CreateInstance<ITarget>("1"));
+ Assert.IsInstanceOfType(typeof(Target2), manager.CreateInstance<ITarget>("2"));
+ Assert.IsInstanceOfType(typeof(Target3), manager.CreateInstance<ITarget>("3"));
+ }
+
+ [Test]
+ public void Add_default_by_concrete_type()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof (ITarget)).TheDefaultIsConcreteType(typeof (Target3));
+ });
+
+ Assert.IsInstanceOfType(typeof(Target3), manager.CreateInstance<ITarget>());
+ }
+
+ [Test]
+ public void Add_default_instance()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof(ITarget)).TheDefaultIs(Instance<Target2>());
+ });
+
+ Assert.IsInstanceOfType(typeof(Target2), manager.CreateInstance<ITarget>());
+ }
+
+ [Test]
+ public void Add_instance_by_lambda()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof(ITarget)).TheDefaultIs(delegate() { return new Target1(); });
+ });
+
+ Assert.IsInstanceOfType(typeof(Target1), manager.CreateInstance<ITarget>());
+ }
+
+ [Test]
+ public void Add_instance_directly()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof (ITarget)).AddInstance(Instance<Target2>());
+ });
+
+
+ Assert.IsInstanceOfType(typeof(Target2), manager.GetAllInstances<ITarget>()[0]);
+ }
+
+ [Test]
+ public void Enrichment()
+ {
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof(ITarget))
+ .TheDefaultIsConcreteType(typeof(Target1))
+ .EnrichWith(delegate(object raw){ return new WrappedTarget((ITarget) raw);});
+ });
+
+ WrappedTarget target = (WrappedTarget) manager.CreateInstance<ITarget>();
+ Assert.IsInstanceOfType(typeof(Target1), target.Inner);
+ }
+
+ [Test]
+ public void Intercept_construction_with()
+ {
+ Registry registry = new Registry();
+ TestingBuildPolicy policy = new TestingBuildPolicy();
+ registry.ForRequestedType(typeof (ITarget)).InterceptConstructionWith(policy);
+ PluginGraph graph = registry.Build();
+
+ Assert.AreSame(policy, graph.FindFamily(typeof(ITarget)).Policy);
+ }
+
+ public class TestingBuildPolicy : IBuildInterceptor
+ {
+ public IBuildPolicy InnerPolicy
+ {
+ get { throw new NotImplementedException(); }
+ set { }
+ }
+
+ public object Build(IBuildSession buildSession, Type pluginType, Instance instance)
+ {
+ throw new NotImplementedException();
+ }
+
+ public IBuildPolicy Clone()
+ {
+ throw new NotImplementedException();
+ }
+ }
+
+ [Test]
+ public void On_creation()
+ {
+ ITarget created = null;
+
+ InstanceManager manager = new InstanceManager(delegate(Registry r)
+ {
+ r.ForRequestedType(typeof (ITarget))
+ .TheDefaultIsConcreteType(typeof (Target3))
+ .OnCreation(delegate(object raw) { created = (ITarget) raw; });
+ });
+
+ manager.CreateInstance<ITarget>();
+
+ Assert.IsInstanceOfType(typeof(Target3), created);
+ }
+
+ [Test]
+ public void Set_caching()
+ {
+ Registry registry = new Registry();
+ registry.ForRequestedType(typeof (ITarget)).CacheBy(InstanceScope.ThreadLocal);
+ PluginGraph graph = registry.Build();
+
+ Assert.IsInstanceOfType(typeof(ThreadLocalStoragePolicy), graph.FindFamily(typeof(ITarget)).Policy);
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap.Testing/Configuration/DSL/RegistryTester.cs 2008-05-27 15:36:33 UTC (rev 101)
@@ -20,26 +20,6 @@
#endregion
-
- [Test]
- public void LoadControl()
- {
- PluginGraph graph = new PluginGraph();
- Registry registry = new Registry(graph);
-
- string theUrl = "some url";
- string theKey = "the memento";
- registry.LoadControlFromUrl<IGateway>(theUrl).WithName(theKey);
-
- registry.Dispose();
-
- PluginFamily family = graph.FindFamily(typeof (IGateway));
- UserControlInstance instance = (UserControlInstance) family.GetInstance(theKey);
- Assert.IsNotNull(instance);
-
- Assert.AreEqual(theUrl, instance.Url);
- Assert.AreEqual(theKey, instance.Name);
- }
}
public class TestRegistry : Registry
Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
===================================================================
--- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-05-27 13:25:28 UTC (rev 100)
+++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-05-27 15:36:33 UTC (rev 101)
@@ -166,6 +166,7 @@
<Compile Include="Configuration\DSL\CreatePluginFamilyTester.cs" />
<Compile Include="Configuration\DSL\CreateProfileTester.cs" />
<Compile Include="Configuration\DSL\DeepInstanceTester.cs" />
+ <Compile Include="Configuration\DSL\GenericFamilyExpressionTester.cs" />
<Compile Include="Configuration\DSL\InjectArrayTester.cs" />
<Compile Include="Configuration\DSL\InstanceExpressionTester.cs" />
<Compile Include="Configuration\DSL\InterceptAllInstancesOfPluginTypeTester.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|