From: <jer...@us...> - 2008-06-01 23:59:12
|
Revision: 114 http://structuremap.svn.sourceforge.net/structuremap/?rev=114&view=rev Author: jeremydmiller Date: 2008-06-01 16:59:11 -0700 (Sun, 01 Jun 2008) Log Message: ----------- Default convention scanning Modified Paths: -------------- trunk/Source/StructureMap/Configuration/DSL/Expressions/ScanAssembliesExpression.cs trunk/Source/StructureMap/Graph/AssemblyScanner.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs Modified: trunk/Source/StructureMap/Configuration/DSL/Expressions/ScanAssembliesExpression.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DSL/Expressions/ScanAssembliesExpression.cs 2008-06-01 14:33:12 UTC (rev 113) +++ trunk/Source/StructureMap/Configuration/DSL/Expressions/ScanAssembliesExpression.cs 2008-06-01 23:59:11 UTC (rev 114) @@ -85,5 +85,16 @@ return this; } + + public ScanAssembliesExpression With(ITypeScanner scanner) + { + _registry.addExpression(delegate(PluginGraph graph) + { + graph.Assemblies.AddScanner(scanner); + }); + + + return this; + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/AssemblyScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2008-06-01 14:33:12 UTC (rev 113) +++ trunk/Source/StructureMap/Graph/AssemblyScanner.cs 2008-06-01 23:59:11 UTC (rev 114) @@ -6,12 +6,12 @@ namespace StructureMap.Graph { - // TODO: redo in 3.5 w/ Lambdas public class AssemblyScanner { private readonly List<Assembly> _assemblies = new List<Assembly>(); private readonly GraphLog _log; + private readonly List<ITypeScanner> _scanners = new List<ITypeScanner>(); public AssemblyScanner(GraphLog log) { @@ -27,35 +27,49 @@ { // Don't do this for SystemScan scanTypes(delegate(Type type) - { - if (Registry.IsPublicRegistry(type)) - { - Registry registry = (Registry) Activator.CreateInstance(type); - registry.ConfigurePluginGraph(pluginGraph); - } - }); + { + if (!Registry.IsPublicRegistry(type)) return; + Registry registry = (Registry) Activator.CreateInstance(type); + registry.ConfigurePluginGraph(pluginGraph); + }); + findFamiliesAndPlugins(pluginGraph); + runScanners(pluginGraph); } + private void runScanners(PluginGraph graph) + { + Registry registry = new Registry(); + scanTypes(delegate(Type type) + { + foreach (ITypeScanner scanner in _scanners) + { + scanner.Process(type, registry); + } + }); + + registry.ConfigurePluginGraph(graph); + } + private void findFamiliesAndPlugins(PluginGraph pluginGraph) { scanTypes(delegate(Type type) - { - if (PluginFamilyAttribute.MarkedAsPluginFamily(type)) - { - pluginGraph.CreateFamily(type); - } - }); + { + if (PluginFamilyAttribute.MarkedAsPluginFamily(type)) + { + pluginGraph.CreateFamily(type); + } + }); scanTypes(delegate(Type type) - { - foreach (PluginFamily family in pluginGraph.PluginFamilies) - { - family.AnalyzeTypeForPlugin(type); - } - }); + { + foreach (PluginFamily family in pluginGraph.PluginFamilies) + { + family.AnalyzeTypeForPlugin(type); + } + }); } @@ -121,5 +135,10 @@ return false; } + + public void AddScanner(ITypeScanner scanner) + { + _scanners.Add(scanner); + } } } \ No newline at end of file Added: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs (rev 0) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2008-06-01 23:59:11 UTC (rev 114) @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; +using StructureMap.Configuration.DSL; + +namespace StructureMap.Graph +{ + public interface ITypeScanner + { + void Process(Type type, Registry registry); + } + + public class DefaultConventionScanner : TypeRules, ITypeScanner + { + public void Process(Type type, Registry registry) + { + if (!IsConcrete(type)) return; + + Type pluginType = FindPluginType(type); + if (pluginType != null) + { + registry.ForRequestedType(pluginType).TheDefaultIsConcreteType(type); + } + } + + public static Type FindPluginType(Type concreteType) + { + string interfaceName = "I" + concreteType.Name; + Type[] interfaces = concreteType.GetInterfaces(); + return Array.Find(interfaces, delegate(Type t) + { + return t.Name == interfaceName; + }); + } + } +} Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-06-01 14:33:12 UTC (rev 113) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-06-01 23:59:11 UTC (rev 114) @@ -374,6 +374,7 @@ <Link>Properties\structuremap.snk</Link> </None> <Compile Include="ErrorMessages.cs" /> + <Compile Include="Graph\ITypeScanner.cs" /> <Compile Include="Pipeline\ConfiguredInstance.Building.cs" /> <Compile Include="Pipeline\IStructuredInstance.cs" /> <Compile Include="Util\Cache.cs" /> Added: trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Graph/DefaultConventionScanningTester.cs 2008-06-01 23:59:11 UTC (rev 114) @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NUnit.Framework; +using StructureMap.Configuration.DSL; +using StructureMap.Graph; +using StructureMap.Pipeline; + +namespace StructureMap.Testing.Graph +{ + [TestFixture] + public class DefaultConventionScanningTester + { + [Test] + public void FindPluginType() + { + Assert.AreEqual(typeof(IConvention), DefaultConventionScanner.FindPluginType(typeof(Convention))); + Assert.IsNull(DefaultConventionScanner.FindPluginType(this.GetType())); + } + + + [Test] + public void Process_to_PluginGraph() + { + Registry registry = new Registry(); + DefaultConventionScanner scanner = new DefaultConventionScanner(); + scanner.Process(typeof(Convention), registry); + + PluginGraph graph = registry.Build(); + + Assert.IsFalse(graph.PluginFamilies.Contains(typeof(IServer))); + Assert.IsTrue(graph.PluginFamilies.Contains(typeof(IConvention))); + + PluginFamily family = graph.FindFamily(typeof (IConvention)); + Assert.AreEqual(1, family.InstanceCount); + } + + [Test] + public void Process_to_Container() + { + Container container = new Container(delegate(Registry registry) + { + registry.ScanAssemblies().IncludeTheCallingAssembly() + .With(new DefaultConventionScanner()); + }); + + Assert.IsInstanceOfType(typeof(Convention), container.GetInstance<IConvention>()); + } + } + + public interface IConvention{} + public interface IServer{} + public class Convention : IConvention, IServer + { + public void Go() + { + throw new NotImplementedException(); + } + } + + public class Something : IConvention{} +} Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-06-01 14:33:12 UTC (rev 113) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-06-01 23:59:11 UTC (rev 114) @@ -191,6 +191,7 @@ <Compile Include="Graph\ContainerConstructorAttributeTester.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Graph\DefaultConventionScanningTester.cs" /> <Compile Include="Graph\DynamicInjectionTester.cs" /> <Compile Include="Graph\EmittingTester.cs"> <SubType>Code</SubType> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |