diff --git a/src/ZeroIoC.Core/ZeroIoCContainer.cs b/src/ZeroIoC.Core/ZeroIoCContainer.cs index 2248308..0edf8db 100644 --- a/src/ZeroIoC.Core/ZeroIoCContainer.cs +++ b/src/ZeroIoC.Core/ZeroIoCContainer.cs @@ -7,7 +7,9 @@ namespace ZeroIoC public abstract class ZeroIoCContainer : IZeroIoCResolver { protected readonly Dictionary Resolvers = new Dictionary(); - protected readonly Dictionary ScopedResolvers = new Dictionary(); + + protected readonly Dictionary ScopedResolvers = + new Dictionary(); protected readonly bool Scoped; @@ -34,7 +36,7 @@ public virtual IZeroIoCResolver Clone() } protected abstract void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper); - + public object Resolve(Type type) { if (Resolvers.TryGetValue(type, out var entry)) @@ -58,7 +60,7 @@ public object Resolve(Type type) ExceptionHelper.ServiceIsNotRegistered(type.FullName); return null; } - + public object Resolve(Type type, IOverrides overrides) { if (Resolvers.TryGetValue(type, out var entry)) @@ -83,7 +85,8 @@ public object Resolve(Type type, IOverrides overrides) return null; } - public void AddDelegate(Func resolver, Type interfaceType, Reuse reuse = Reuse.Transient) + public void AddDelegate(Func resolver, Type interfaceType, + Reuse reuse = Reuse.Transient) { switch (reuse) { @@ -100,13 +103,14 @@ public void AddDelegate(Func resolver, Type interfaceT throw new ArgumentOutOfRangeException(nameof(reuse), reuse, null); } } - - public void ReplaceDelegate(Func resolver, Type interfaceType, Reuse reuse = Reuse.Transient) + + public void ReplaceDelegate(Func resolver, Type interfaceType, + Reuse reuse = Reuse.Transient) { switch (reuse) { case Reuse.Scoped: - ScopedResolvers[interfaceType] =new SingletonResolver(resolver); + ScopedResolvers[interfaceType] = new SingletonResolver(resolver); break; case Reuse.Singleton: Resolvers[interfaceType] = new SingletonResolver(resolver); @@ -123,7 +127,7 @@ public void AddInstance(TValue value) { Resolvers.Add(typeof(TValue), new SingletonResolver(o => value)); } - + public void ReplaceInstance(TValue value) { Resolvers[typeof(TValue)] = new SingletonResolver(o => value); @@ -144,9 +148,12 @@ public void Merge(ZeroIoCContainer container) public void Dispose() { - foreach (var resolver in Resolvers.Values) + if (!Scoped) { - resolver.Dispose(); + foreach (var resolver in Resolvers.Values) + { + resolver.Dispose(); + } } foreach (var resolver in ScopedResolvers.Values) diff --git a/src/ZeroIoC.Tests/ScopedContainerTest.cs b/src/ZeroIoC.Tests/ScopedContainerTest.cs index ed198c4..e47c05d 100644 --- a/src/ZeroIoC.Tests/ScopedContainerTest.cs +++ b/src/ZeroIoC.Tests/ScopedContainerTest.cs @@ -93,12 +93,17 @@ public async Task ServicesWithinTheScopeIsDisposed() { var project = await TestProject.Project.ApplyToProgram(@" - public interface IService : IDisposable + public class SingletonService : IDisposable { - bool Disposed { get; set; } + public bool Disposed { get; set; } + + public void Dispose() + { + Disposed = true; + } } - public class Service : IService + public class Service : IDisposable { public bool Disposed { get; set; } @@ -112,7 +117,8 @@ public partial class TestContainer : ZeroIoCContainer { protected override void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper) { - bootstrapper.AddScoped(); + bootstrapper.AddScoped(); + bootstrapper.AddSingleton(); } } "); @@ -121,20 +127,23 @@ protected override void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper) var assembly = await newProject.CompileToRealAssembly(); var containerType = assembly.GetType("TestProject.TestContainer"); - var serviceType = assembly.GetType("TestProject.IService"); + var serviceType = assembly.GetType("TestProject.Service"); + var singletonServiceType = assembly.GetType("TestProject.SingletonService"); var container = (IZeroIoCResolver)Activator.CreateInstance(containerType); object service = null; + object singletonService = null; using (var scoped = container.CreateScope()) { service = scoped.Resolve(serviceType); - var initialValue = (bool)service.ReflectionGetValue("Disposed"); + Assert.False((bool)service.ReflectionGetValue("Disposed")); - Assert.False(initialValue); + singletonService = scoped.Resolve(singletonServiceType); + Assert.False((bool)service.ReflectionGetValue("Disposed")); } - var value = (bool)service.ReflectionGetValue("Disposed"); - Assert.True(value); + Assert.True((bool)service.ReflectionGetValue("Disposed")); + Assert.False((bool)singletonService.ReflectionGetValue("Disposed")); } } \ No newline at end of file