From: <jer...@us...> - 2008-12-21 00:34:38
|
Revision: 209 http://structuremap.svn.sourceforge.net/structuremap/?rev=209&view=rev Author: jeremydmiller Date: 2008-12-21 00:34:36 +0000 (Sun, 21 Dec 2008) Log Message: ----------- making "EjectAll" work for singletons Modified Paths: -------------- trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/Pipeline/BuildPolicy.cs trunk/Source/StructureMap/Pipeline/CacheInterceptor.cs trunk/Source/StructureMap/Pipeline/HybridBuildPolicy.cs trunk/Source/StructureMap/Pipeline/IBuildPolicy.cs trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap/InstanceFactory.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -87,6 +87,7 @@ public IBuildPolicy Policy { get { return _policy; } + set { _policy = value; } } public void ForEachInstance(Action<Instance> action) @@ -147,6 +148,7 @@ public void EjectAllInstances() { + _policy.EjectAll(); _instances.Clear(); } Modified: trunk/Source/StructureMap/Pipeline/BuildPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/BuildPolicy.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap/Pipeline/BuildPolicy.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -30,6 +30,12 @@ return this; } + public void EjectAll() + { + // no-op. Unlike other Container's, StructureMap doesn't hang on to + // objects it created as "Transients" + } + #endregion public bool Equals(BuildPolicy obj) Modified: trunk/Source/StructureMap/Pipeline/CacheInterceptor.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/CacheInterceptor.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap/Pipeline/CacheInterceptor.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -44,6 +44,16 @@ return clonedCache; } + public void EjectAll() + { + ejectAll(); + _innerPolicy.EjectAll(); + } + + protected virtual void ejectAll() + { + } + #endregion protected abstract CacheInterceptor clone(); Modified: trunk/Source/StructureMap/Pipeline/HybridBuildPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HybridBuildPolicy.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap/Pipeline/HybridBuildPolicy.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -45,6 +45,11 @@ } public abstract IBuildPolicy Clone(); + public void EjectAll() + { + _http.EjectAll(); + _nonHttp.EjectAll(); + } } Modified: trunk/Source/StructureMap/Pipeline/IBuildPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IBuildPolicy.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap/Pipeline/IBuildPolicy.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -9,5 +9,6 @@ { object Build(BuildSession buildSession, Type pluginType, Instance instance); IBuildPolicy Clone(); + void EjectAll(); } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -1,3 +1,5 @@ +using System; + namespace StructureMap.Pipeline { [Pluggable("Singleton")] @@ -22,6 +24,35 @@ return _cache; } + public InstanceCache Cache + { + get + { + return findCache(); + } + } + + protected override void ejectAll() + { + lock (_locker) + { + _cache.Each(o => + { + IDisposable disposable = o as IDisposable; + if (disposable != null) + { + try + { + disposable.Dispose(); + } + catch (Exception){} + } + }); + + _cache.Clear(); + } + } + protected override CacheInterceptor clone() { return new SingletonPolicy(); Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -289,6 +289,11 @@ throw new NotImplementedException(); } + public void EjectAll() + { + throw new System.NotImplementedException(); + } + #endregion } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/GenericFamilyExpressionTester.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -61,6 +61,11 @@ throw new NotImplementedException(); } + public void EjectAll() + { + throw new System.NotImplementedException(); + } + #endregion } Modified: trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap.Testing/Graph/DynamicInjectionTester.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -364,6 +364,11 @@ throw new NotImplementedException(); } + public void EjectAll() + { + throw new System.NotImplementedException(); + } + #endregion } Modified: trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap.Testing/Graph/InstanceFactoryTester.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -1,10 +1,12 @@ using NUnit.Framework; +using Rhino.Mocks; using StructureMap.Attributes; using StructureMap.Configuration.DSL; using StructureMap.Graph; using StructureMap.Pipeline; using StructureMap.Testing.Widget; using StructureMap.Testing.Widget3; +using System.Linq; namespace StructureMap.Testing.Graph { @@ -58,6 +60,23 @@ } [Test] + public void eject_all_instances_removes_all_instances_and_ejects_from_the_build_policy() + { + var factory = new InstanceFactory(typeof (IGateway)); + factory.AddInstance(new SmartInstance<DefaultGateway>()); + factory.AddInstance(new SmartInstance<DefaultGateway>()); + + var policy = MockRepository.GenerateMock<IBuildPolicy>(); + factory.Policy = policy; + + factory.EjectAllInstances(); + + factory.Instances.Count().ShouldEqual(0); + + policy.AssertWasCalled(x => x.EjectAll()); + } + + [Test] public void Import_from_family_picks_up_new_instances() { var factory = new InstanceFactory(typeof (IWidget)); Modified: trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-12-20 20:50:53 UTC (rev 208) +++ trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-12-21 00:34:36 UTC (rev 209) @@ -102,6 +102,7 @@ Assert.IsInstanceOfType(typeof (BuildPolicy), clone.InnerPolicy); } + [Test] public void Singleton_build_policy() { @@ -124,4 +125,81 @@ Assert.AreSame(green1, green3); } } + + [TestFixture] + public class when_the_singleton_policy_ejects_all + { + private SingletonPolicy policy; + private StubBuildPolicy inner; + private StubDisposable disposable1; + private StubDisposable disposable2; + + + [SetUp] + public void SetUp() + { + policy = new SingletonPolicy(); + inner = new StubBuildPolicy(); + policy.InnerPolicy = inner; + + disposable1 = new StubDisposable(); + disposable2 = new StubDisposable(); + + policy.Cache[new InstanceKey() { Name = "a", PluginType = typeof(IGateway) }] = disposable1; + policy.Cache[new InstanceKey() {Name = "b", PluginType = typeof (IGateway)}] = disposable2; + policy.Cache[new InstanceKey() {Name = "c", PluginType = typeof (IGateway)}] = new object(); + + policy.EjectAll(); + } + + [Test] + public void the_count_should_be_zero() + { + policy.Cache.Count.ShouldEqual(0); + } + + [Test] + public void should_have_called_dispose_if_it_was_there() + { + disposable1.DisposedWasCalled.ShouldBeTrue(); + disposable2.DisposedWasCalled.ShouldBeTrue(); + } + + [Test] + public void inner_policy_was_ejected() + { + inner.WasEjected.ShouldBeTrue(); + } + + } + + public class StubDisposable : IDisposable + { + public bool DisposedWasCalled; + + public void Dispose() + { + DisposedWasCalled = true; + } + } + + public class StubBuildPolicy : IBuildPolicy + { + public bool WasEjected; + + public object Build(BuildSession buildSession, Type pluginType, Instance instance) + { + throw new System.NotImplementedException(); + } + + public IBuildPolicy Clone() + { + throw new System.NotImplementedException(); + } + + public void EjectAll() + { + WasEjected = true; + } + } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |