|
From: <jer...@us...> - 2009-07-04 17:12:21
|
Revision: 252
http://structuremap.svn.sourceforge.net/structuremap/?rev=252&view=rev
Author: jeremydmiller
Date: 2009-07-04 17:12:20 +0000 (Sat, 04 Jul 2009)
Log Message:
-----------
the redirect shortcut
Modified Paths:
--------------
trunk/Source/StructureMap/Configuration/DSL/Registry.cs
trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
Added Paths:
-----------
trunk/Source/StructureMap.Testing/Pipeline/RedirectTester.cs
Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs
===================================================================
--- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-06-26 14:34:44 UTC (rev 251)
+++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-07-04 17:12:20 UTC (rev 252)
@@ -410,5 +410,26 @@
{
return ForRequestedType(pluginType);
}
+
+
+ /// <summary>
+ /// Shortcut to make StructureMap return the default object of U casted to T
+ /// whenever T is requested. I.e.:
+ /// For<T>().TheDefault.Is.ConstructedBy(c => c.GetInstance<U>() as T);
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ /// <typeparam name="U"></typeparam>
+ /// <returns></returns>
+ public ConstructorInstance<T> Redirect<T, U>() where T : class where U : class
+ {
+ return For<T>().TheDefault.Is.ConstructedBy(c =>
+ {
+ var raw = c.GetInstance<U>();
+ var t = raw as T;
+ if (t == null) throw new ApplicationException(raw.GetType().AssemblyQualifiedName + " could not be cast to " + typeof(T).AssemblyQualifiedName);
+
+ return t;
+ });
+ }
}
}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Pipeline/RedirectTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Pipeline/RedirectTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Pipeline/RedirectTester.cs 2009-07-04 17:12:20 UTC (rev 252)
@@ -0,0 +1,53 @@
+using System;
+using NUnit.Framework;
+
+namespace StructureMap.Testing.Pipeline
+{
+ [TestFixture]
+ public class RedirectTester
+ {
+ [Test]
+ public void successfully_redirect_from_one_type_to_another()
+ {
+ var container = new Container(x =>
+ {
+ x.For<ITarget>().Use<ClassThatImplementsBoth>();
+ x.Redirect<IOtherTarget, ITarget>();
+ });
+
+ container.GetInstance<IOtherTarget>().ShouldBeOfType<ClassThatImplementsBoth>();
+ }
+
+ [Test, ExpectedException(typeof(StructureMapException))]
+ public void fail_with_cast_failure_when_the_types_are_not_convertible()
+ {
+ var container = new Container(x =>
+ {
+ x.For<ITarget>().Use<ClassThatOnlyImplementsITarget>();
+ x.Redirect<IOtherTarget, ITarget>();
+ });
+
+ container.GetInstance<IOtherTarget>().ShouldBeOfType<ClassThatImplementsBoth>();
+ }
+ }
+
+ public interface ITarget
+ {
+
+ }
+
+ public interface IOtherTarget
+ {
+
+ }
+
+ public class ClassThatImplementsBoth : ITarget, IOtherTarget
+ {
+
+ }
+
+ public class ClassThatOnlyImplementsITarget : ITarget
+ {
+
+ }
+}
\ No newline at end of file
Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj
===================================================================
--- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-06-26 14:34:44 UTC (rev 251)
+++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2009-07-04 17:12:20 UTC (rev 252)
@@ -361,6 +361,7 @@
<Compile Include="Pipeline\ProfileManagerTester.cs" />
<Compile Include="Pipeline\ProfileTester.cs" />
<Compile Include="Pipeline\PrototypeInstanceTester.cs" />
+ <Compile Include="Pipeline\RedirectTester.cs" />
<Compile Include="Pipeline\ReferencedInstanceTester.cs" />
<Compile Include="Pipeline\SerializedInstanceTester.cs" />
<Compile Include="Pipeline\SmartInstanceTester.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|