From: <jer...@us...> - 2009-05-18 21:40:20
|
Revision: 241 http://structuremap.svn.sourceforge.net/structuremap/?rev=241&view=rev Author: jeremydmiller Date: 2009-05-18 21:40:09 +0000 (Mon, 18 May 2009) Log Message: ----------- little helper thing for open generic type scanning Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Registry.cs trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/ObjectFactory.cs trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs trunk/Source/StructureMap.Testing/Graph/TestExplicitArguments.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Registry.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap/Configuration/DSL/Registry.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -371,5 +371,24 @@ } + /// <summary> + /// Syntactic Sugar for saying ForRequestedType().TheDefault.IsThis( @object ) + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <param name="object"></param> + public void Register<PLUGINTYPE>(PLUGINTYPE @object) + { + ForRequestedType<PLUGINTYPE>().TheDefault.IsThis(@object); + } + + /// <summary> + /// Syntactic Sugar for saying ForRequestedType().TheDefault.IsThis( instance ) + /// </summary> + /// <typeparam name="PLUGINTYPE"></typeparam> + /// <param name="instance"></param> + public void Register<PLUGINTYPE>(Instance instance) + { + ForRequestedType<PLUGINTYPE>().TheDefault.IsThis(instance); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap/Container.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -434,6 +434,15 @@ return new ExplicitArgsExpression(this).With(argName); } + public ExplicitArgsExpression With(Action<ExplicitArgsExpression> action) + { + var expression = new ExplicitArgsExpression(this); + action(expression); + + return expression; + } + + /// <summary> /// Use with caution! Does a full environment test of the configuration of this container. Will try to create every configured /// instance and afterward calls any methods marked with the [ValidationMethod] attribute Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -160,6 +160,14 @@ // ... Other methods #endregion + /// <summary> + /// Scans for PluginType's and Concrete Types that close the given open generic type + /// </summary> + /// <example> + /// + /// </example> + /// <param name="openGenericType"></param> + void ConnectImplementationsToTypesClosing(Type openGenericType); } public class AssemblyScanner : IAssemblyScanner @@ -377,6 +385,11 @@ Exclude(type => type == typeof (T)); } + public void ConnectImplementationsToTypesClosing(Type openGenericType) + { + With(new GenericConnectionScanner(openGenericType)); + } + public void AssembliesFromPath(string path) { AssembliesFromPath(path, a => true); Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -1,4 +1,5 @@ using System; +using StructureMap; namespace StructureMap.Graph { @@ -31,4 +32,28 @@ return Array.Find(interfaces, t => t.Name == interfaceName); } } + + public class GenericConnectionScanner : ITypeScanner + { + private readonly Type _openType; + + public GenericConnectionScanner(Type openType) + { + _openType = openType; + + if (!_openType.IsGeneric()) + { + throw new ApplicationException("This scanning convention can only be used with open generic types"); + } + } + + public void Process(Type type, PluginGraph graph) + { + Type interfaceType = type.FindInterfaceThatCloses(_openType); + if (interfaceType != null) + { + graph.AddType(interfaceType, type); + } + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/ObjectFactory.cs =================================================================== --- trunk/Source/StructureMap/ObjectFactory.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap/ObjectFactory.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -467,5 +467,12 @@ return container.ForObject(subject); } + public static IContainer Container + { + get + { + return container; + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -5,7 +5,51 @@ namespace StructureMap.Testing.Graph { + [TestFixture] + public class GenericConnectionScannerTester + { + private Container container; + + [SetUp] + public void SetUp() + { + container = new Container(x => + { + x.Scan(o => + { + o.TheCallingAssembly(); + o.ConnectImplementationsToTypesClosing(typeof (IFinder<>)); + }); + }); + } + + [Test] + public void can_find_the_closed_finders() + { + container.GetInstance<IFinder<string>>().ShouldBeOfType<StringFinder>(); + container.GetInstance<IFinder<int>>().ShouldBeOfType<IntFinder>(); + container.GetInstance<IFinder<double>>().ShouldBeOfType<DoubleFinder>(); + } + + [Test, ExpectedException(typeof(ApplicationException))] + public void fails_on_closed_type() + { + new GenericConnectionScanner(typeof (double)); + } + } + + public interface IFinder<T> + { + + } + + public class StringFinder : IFinder<string>{} + public class IntFinder : IFinder<int>{} + public class DoubleFinder : IFinder<double>{} + + + [TestFixture] public class DefaultConventionScanningTester { [Test] Modified: trunk/Source/StructureMap.Testing/Graph/TestExplicitArguments.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/TestExplicitArguments.cs 2009-04-20 01:47:17 UTC (rev 240) +++ trunk/Source/StructureMap.Testing/Graph/TestExplicitArguments.cs 2009-05-18 21:40:09 UTC (rev 241) @@ -440,6 +440,17 @@ } [Test] + public void can_build_a_concrete_class_with_constructor_args_that_is_not_previously_registered_2() + { + var container = new Container(); + + container.With(x => + { + x.With("name").EqualTo("Jeremy"); + }).GetInstance<ConcreteThatNeedsString>().Name.ShouldEqual("Jeremy"); + } + + [Test] public void can_build_a_concrete_type_from_explicit_args_passed_into_a_named_instance() { var container = new Container(x => This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |