From: <jer...@us...> - 2010-02-04 14:09:08
|
Revision: 339 http://structuremap.svn.sourceforge.net/structuremap/?rev=339&view=rev Author: jeremydmiller Date: 2010-02-04 13:40:53 +0000 (Thu, 04 Feb 2010) Log Message: ----------- Added the Func<> lazy evaluation strategy Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/ModelQueryTester.cs trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/Pipeline/AdapterClasses.cs trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap/Container.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -558,9 +558,17 @@ _pluginGraph.FindFamily(typeof (IContainer)).AddInstance(thisInstance); _pluginGraph.ProfileManager.SetDefault(typeof (IContainer), thisInstance); + var funcInstance = new FactoryTemplate(typeof (LazyInstance<>)); + _pluginGraph.FindFamily(typeof(Func<>)).AddInstance(funcInstance); + _pluginGraph.ProfileManager.SetDefault(typeof(Func<>), funcInstance); + + + pluginGraph.Log.AssertFailures(); _pipelineGraph = new PipelineGraph(pluginGraph); + + } [Obsolete("delegate to something cleaner in BuildSession")] Added: trunk/Source/StructureMap/Pipeline/AdapterClasses.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/AdapterClasses.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/AdapterClasses.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -0,0 +1,79 @@ +using System; + +namespace StructureMap.Pipeline +{ + public class FactoryTemplate : Instance + { + private readonly Type _openInstanceType; + + public FactoryTemplate(Type openInstanceType) + { + _openInstanceType = openInstanceType; + } + + protected override string getDescription() + { + return string.Empty; + } + + protected override object build(Type pluginType, BuildSession session) + { + throw new NotImplementedException(); + } + + public override Instance CloseType(Type[] types) + { + var instanceType = _openInstanceType.MakeGenericType(types); + return (Instance) Activator.CreateInstance(instanceType); + } + } + + + public class LazyInstance<T> : Instance + { + protected override string getDescription() + { + return "Lazy construction of " + typeof (T).FullName; + } + + protected override object build(Type pluginType, BuildSession session) + { + var container = session.GetInstance<IContainer>(); + Func<T> func = () => container.GetInstance<T>(); + + return func; + } + } + + public class FactoryInstance<T> : Instance + { + protected override string getDescription() + { + return "Lazy factory of " + typeof (T).FullName; + } + + protected override object build(Type pluginType, BuildSession session) + { + var container = session.GetInstance<IContainer>(); + Func<string, T> func = name => container.GetInstance<T>(name); + + return func; + } + } + + public class FactoryInstance<T, T1> : Instance + { + protected override string getDescription() + { + return "Lazy construction of {0} using {1}".ToFormat(typeof(T1).FullName, typeof(T).FullName); + } + + protected override object build(Type pluginType, BuildSession session) + { + var container = session.GetInstance<IContainer>(); + Func<T, T1> func = key => container.With(key).GetInstance<T1>(); + + return func; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap/PipelineGraph.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -37,6 +37,8 @@ var factory = new InstanceFactory(family); _factories.Add(family.PluginType, factory); }); + + } private PipelineGraph(ProfileManager profileManager, GenericsPluginGraph genericsGraph, GraphLog log) Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap/StructureMap.csproj 2010-02-04 13:40:53 UTC (rev 339) @@ -371,6 +371,7 @@ <Compile Include="Graph\PluggableAttributeScanner.cs" /> <Compile Include="Graph\PluginCache.cs" /> <Compile Include="IContext.cs" /> + <Compile Include="Pipeline\AdapterClasses.cs" /> <Compile Include="Pipeline\HybridLifecycle.cs" /> <Compile Include="Query\EmptyConfiguration.cs" /> <Compile Include="Query\GenericFamilyConfiguration.cs" /> Modified: trunk/Source/StructureMap.Testing/ModelQueryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap.Testing/ModelQueryTester.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -112,15 +112,15 @@ [Test] public void Iterate_over_pluginTypes() { - // 3 registered plus the 4th is the IContainer itself - _model.PluginTypes.Count().ShouldEqual(4); + // 3 registered plus the 4th is the IContainer itself + Func + _model.PluginTypes.Count().ShouldEqual(5); } [Test] public void Iterate_over_pluginTypes_w_container() { - // IContainer is always added to the Container - _container.Model.PluginTypes.Count().ShouldEqual(4); + // IContainer is always added to the Container + Func<> + _container.Model.PluginTypes.Count().ShouldEqual(5); } [Test] Added: trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/LazyFuncTester.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -0,0 +1,78 @@ +using System; +using NUnit.Framework; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class LazyFuncTester + { + [SetUp] + public void SetUp() + { + } + + [Test] + public void FactoryTemplateTester() + { + var container = new Container(x => + { + x.For(typeof (Func<>)).Use(new FactoryTemplate(typeof (LazyInstance<>))); + }); + + //container.GetInstance<Func<ConcreteClass>>()().ShouldNotBeNull(); + } + + [Test] + public void build_a_func_for_a_concrete_class() + { + var container = new Container(); + var func = container.GetInstance<Func<ConcreteClass>>(); + + func().ShouldNotBeNull(); + } + + [Test] + public void build_a_func_that_returns_a_transient() + { + var container = new Container(x => + { + x.For<IWidget>().Use<ColorWidget>().Ctor<string>("color").Is("green"); + }); + + var func = container.GetInstance<Func<IWidget>>(); + var w1 = func(); + var w2 = func(); + var w3 = func(); + + w1.ShouldBeOfType<ColorWidget>().Color.ShouldEqual("green"); + + w1.ShouldNotBeTheSameAs(w2); + w1.ShouldNotBeTheSameAs(w3); + w2.ShouldNotBeTheSameAs(w3); + } + + [Test] + public void build_a_func_that_returns_a_singleton() + { + var container = new Container(x => + { + x.ForSingletonOf<IWidget>().Use<ColorWidget>().Ctor<string>("color").Is("green"); + }); + + var func = container.GetInstance<Func<IWidget>>(); + var w1 = func(); + var w2 = func(); + var w3 = func(); + + w1.ShouldBeOfType<ColorWidget>().Color.ShouldEqual("green"); + + w1.ShouldBeTheSameAs(w2); + w1.ShouldBeTheSameAs(w3); + w2.ShouldBeTheSameAs(w3); + } + + public class ConcreteClass{} + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap.Testing/Query/ModelIntegrationTester.cs 2010-02-04 13:40:53 UTC (rev 339) @@ -46,8 +46,8 @@ [Test] public void can_iterate_through_families_including_both_generics_and_normal() { - // +1 for "IContainer" itself - container.Model.PluginTypes.Count().ShouldEqual(8); + // +1 for "IContainer" itself + Func + container.Model.PluginTypes.Count().ShouldEqual(9); container.Model.PluginTypes.Each(x => Debug.WriteLine(x.PluginType.FullName)); } @@ -86,7 +86,7 @@ [Test] public void get_all_instances_from_the_top() { - container.Model.AllInstances.Count().ShouldEqual(11); + container.Model.AllInstances.Count().ShouldEqual(12); } [Test] Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2010-02-04 03:56:44 UTC (rev 338) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2010-02-04 13:40:53 UTC (rev 339) @@ -364,6 +364,7 @@ <Compile Include="Pipeline\GenericsHelperExpressionTester.cs" /> <Compile Include="Pipeline\HybridBuildLifecycleTester.cs" /> <Compile Include="Pipeline\InstanceTester.cs" /> + <Compile Include="Pipeline\LazyFuncTester.cs" /> <Compile Include="Pipeline\MainObjectCacheTester.cs" /> <Compile Include="Pipeline\ObjectInstanceTester.cs" /> <Compile Include="Pipeline\MissingInstanceTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |