From: <jer...@us...> - 2008-12-20 19:44:09
|
Revision: 207 http://structuremap.svn.sourceforge.net/structuremap/?rev=207&view=rev Author: jeremydmiller Date: 2008-12-20 19:44:05 +0000 (Sat, 20 Dec 2008) Log Message: ----------- allowing users to specify object scope in the Container.Configure() method Modified Paths: -------------- trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj Added Paths: ----------- trunk/Source/StructureMap/PluginTypeConfiguration.cs trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeInConfigureTester.cs Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2008-12-20 18:50:25 UTC (rev 206) +++ trunk/Source/StructureMap/InstanceFactory.cs 2008-12-20 19:44:05 UTC (rev 207) @@ -16,7 +16,7 @@ new Cache<string, Instance>(delegate { return null; }); private readonly Type _pluginType; - private readonly IBuildPolicy _policy = new BuildPolicy(); + private IBuildPolicy _policy = new BuildPolicy(); #region constructor functions @@ -136,6 +136,12 @@ public void ImportFrom(PluginFamily family) { + if (!_policy.GetType().Equals(family.Policy.GetType())) + { + // TODO: Might need to clear out the existing policy when it's ejected + _policy = family.Policy; + } + family.EachInstance(instance => _instances.Fill(instance.Name, instance)); } Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2008-12-20 18:50:25 UTC (rev 206) +++ trunk/Source/StructureMap/PipelineGraph.cs 2008-12-20 19:44:05 UTC (rev 207) @@ -9,31 +9,6 @@ public delegate InstanceFactory MissingFactoryFunction(Type pluginType, ProfileManager profileManager); - /// <summary> - /// Metadata describing the registration of a PluginType - /// </summary> - public class PluginTypeConfiguration - { - public Type PluginType { get; set; } - - /// <summary> - /// The "instance" that will be used when Container.GetInstance(PluginType) is called. - /// See <see cref="StructureMap.Pipeline.IInstance">IInstance</see> for more information - /// </summary> - public IInstance Default { get; set; } - - /// <summary> - /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods - /// </summary> - public IBuildPolicy Policy { get; set; } - - /// <summary> - /// All of the <see cref="StructureMap.Pipeline.IInstance">IInstance</see>'s registered - /// for this PluginType - /// </summary> - public IEnumerable<IInstance> Instances { get; set; } - } - public class PipelineGraph { private readonly Dictionary<Type, IInstanceFactory> _factories Added: trunk/Source/StructureMap/PluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/PluginTypeConfiguration.cs (rev 0) +++ trunk/Source/StructureMap/PluginTypeConfiguration.cs 2008-12-20 19:44:05 UTC (rev 207) @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using StructureMap.Pipeline; + +namespace StructureMap +{ + /// <summary> + /// Metadata describing the registration of a PluginType + /// </summary> + public class PluginTypeConfiguration + { + public Type PluginType { get; set; } + + /// <summary> + /// The "instance" that will be used when Container.GetInstance(PluginType) is called. + /// See <see cref="StructureMap.Pipeline.IInstance">IInstance</see> for more information + /// </summary> + public IInstance Default { get; set; } + + /// <summary> + /// The build "policy" for this PluginType. Used by the WhatDoIHave() diagnostics methods + /// </summary> + public IBuildPolicy Policy { get; set; } + + /// <summary> + /// All of the <see cref="StructureMap.Pipeline.IInstance">IInstance</see>'s registered + /// for this PluginType + /// </summary> + public IEnumerable<IInstance> Instances { get; set; } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-12-20 18:50:25 UTC (rev 206) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-12-20 19:44:05 UTC (rev 207) @@ -418,6 +418,7 @@ <Compile Include="Pipeline\PropertyExpression.cs" /> <Compile Include="Pipeline\SerializedInstance.cs" /> <Compile Include="Pipeline\SmartInstance.cs" /> + <Compile Include="PluginTypeConfiguration.cs" /> <Compile Include="ReflectionHelper.cs" /> <Compile Include="Util\Cache.cs" /> </ItemGroup> Added: trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeInConfigureTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeInConfigureTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/SpecifyScopeInConfigureTester.cs 2008-12-20 19:44:05 UTC (rev 207) @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using StructureMap.Attributes; +using StructureMap.Testing.Widget3; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class SpecifyScopeInConfigureTester + { + [Test] + public void specify_the_scope_in_a_Configure_if_it_is_not_already_set() + { + var container = new Container(x => { }); + container.Configure(x => + { + x.ForRequestedType<IGateway>().CacheBy(InstanceScope.Singleton) + .TheDefaultIsConcreteType<DefaultGateway>(); + + }); + + var gateway1 = container.GetInstance<IGateway>(); + var gateway2 = container.GetInstance<IGateway>(); + var gateway3 = container.GetInstance<IGateway>(); + + gateway1.ShouldBeTheSameAs(gateway2); + gateway1.ShouldBeTheSameAs(gateway3); + } + } +} Modified: trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs 2008-12-20 18:50:25 UTC (rev 206) +++ trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs 2008-12-20 19:44:05 UTC (rev 207) @@ -1,4 +1,5 @@ using NUnit.Framework; +using StructureMap.Attributes; using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Pipeline; @@ -74,6 +75,39 @@ } [Test] + public void import_from_another_family_will_override_the_build_policy_if_the_initial_policy_is_the_default() + { + var factory = new InstanceFactory(typeof(IWidget)); + + var family = new PluginFamily(typeof(IWidget)); + family.SetScopeTo(InstanceScope.Singleton); + + factory.ImportFrom(family); + + factory.Policy.ShouldBeOfType<SingletonPolicy>(); + } + + [Test] + public void do_not_replace_the_build_policy_if_it_is_the_same_type_as_the_imported_family() + { + PluginFamily originalFamily = new PluginFamily(typeof(IWidget)); + originalFamily.SetScopeTo(InstanceScope.Singleton); + var factory = new InstanceFactory(originalFamily); + + var originalPolicy = factory.Policy; + + + var family = new PluginFamily(typeof(IWidget)); + family.SetScopeTo(InstanceScope.Singleton); + + factory.ImportFrom(family); + + factory.Policy.ShouldBeTheSameAs(originalPolicy); + } + + + + [Test] public void Merge_from_PluginFamily_will_not_replace_an_existing_instance() { var factory = new InstanceFactory(typeof (IWidget)); Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-12-20 18:50:25 UTC (rev 206) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-12-20 19:44:05 UTC (rev 207) @@ -180,6 +180,7 @@ <Compile Include="AutoMocking\RhinoMockRepositoryProxyTester.cs" /> <Compile Include="Bugs\IDictionaryAndXmlBugTester.cs" /> <Compile Include="Bugs\ScanIndexerBugTester.cs" /> + <Compile Include="Bugs\SpecifyScopeInConfigureTester.cs" /> <Compile Include="BuildSessionTester.cs" /> <Compile Include="Configuration\ConfigurationParserBuilderTester.cs" /> <Compile Include="Configuration\ConfigurationParserTester.cs" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |