diff --git a/AutoDI.Tests/ContainerMapTests.cs b/AutoDI.Tests/ContainerMapTests.cs index edb350f..6f61e50 100644 --- a/AutoDI.Tests/ContainerMapTests.cs +++ b/AutoDI.Tests/ContainerMapTests.cs @@ -391,6 +391,22 @@ public void CanResolveMultipleArrayContructoreParameter() @class.Parameter.Select(x => x.GetType()).ToArray()); } + [TestMethod] + public void CanRemovedRegisteredMap() + { + var map = new ContainerMap(); + var services = new AutoDIServiceCollection(); + services.AddAutoDISingleton(); + services.AddAutoDISingleton(); + map.Add(services); + + bool wasRemoved = map.Remove(); + var interfaces = map.Get>(null); + + Assert.IsTrue(wasRemoved); + Assert.IsNull(interfaces); + } + private interface IInterface { } private interface IInterface2 { } diff --git a/AutoDI/ContainerMap.cs b/AutoDI/ContainerMap.cs index 1c5ea17..b6c777b 100644 --- a/AutoDI/ContainerMap.cs +++ b/AutoDI/ContainerMap.cs @@ -25,7 +25,6 @@ static ContainerMap() public void Add(IServiceCollection services) { - ////TODO: This re-grouping seems off somewhere... foreach (IGrouping serviceDescriptors in from ServiceDescriptor service in services let autoDIService = service as AutoDIServiceDescriptor @@ -63,6 +62,10 @@ private void AddInternal(DelegateContainer container, Type key) } } + public bool Remove() => Remove(typeof(T)); + + public bool Remove(Type serviceType) => _accessors.Remove(serviceType); + public T Get(IServiceProvider provider) { //https://github.com/Keboo/DoubleDownWat @@ -74,15 +77,15 @@ public T Get(IServiceProvider provider) return default(T); } - public object Get(Type key, IServiceProvider provider) + public object Get(Type serviceType, IServiceProvider provider) { - if (TryGet(key, provider ?? new ContainerServiceProvider(this), out object result)) + if (TryGet(serviceType, provider ?? new ContainerServiceProvider(this), out object result)) { return result; } //Type key not found - var args = new TypeKeyNotFoundEventArgs(key); + var args = new TypeKeyNotFoundEventArgs(serviceType); TypeKeyNotFound?.Invoke(this, args); return args.Instance; } diff --git a/AutoDI/IContainer.cs b/AutoDI/IContainer.cs index c50f505..27c78eb 100644 --- a/AutoDI/IContainer.cs +++ b/AutoDI/IContainer.cs @@ -8,7 +8,10 @@ public interface IContainer : IEnumerable event EventHandler TypeKeyNotFound; T Get(IServiceProvider provider); - object Get(Type key, IServiceProvider provider); + object Get(Type serviceType, IServiceProvider provider); + + bool Remove(); + bool Remove(Type serviceType); IContainer CreatedNestedContainer(); } diff --git a/AutoDI/TypeKeyNotFoundEventArgs.cs b/AutoDI/TypeKeyNotFoundEventArgs.cs index 3fcf7d5..3415315 100644 --- a/AutoDI/TypeKeyNotFoundEventArgs.cs +++ b/AutoDI/TypeKeyNotFoundEventArgs.cs @@ -4,13 +4,13 @@ namespace AutoDI { public class TypeKeyNotFoundEventArgs : EventArgs { - public Type ServiceKey { get; } + public Type ServiceType { get; } public object Instance { get; set; } - public TypeKeyNotFoundEventArgs(Type serviceKey) + public TypeKeyNotFoundEventArgs(Type serviceType) { - ServiceKey = serviceKey; + ServiceType = serviceType; } } }