From b27e7a984ab8d20ff0b430d0e5dfb197a9a6eefc Mon Sep 17 00:00:00 2001 From: Macocian Alexandru Victor Date: Sun, 11 Sep 2022 14:00:21 +0200 Subject: [PATCH] Add support for remove in servicecontainers (#27) --- .../SlimServiceContainerTests.cs | 22 ++++++++++++++----- .../ServiceContainer/SlimServiceContainer.cs | 15 +++++++++++-- Slim.Integration/Slim.Integration.csproj | 8 +++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/Slim.Integration.Tests/SlimServiceContainerTests.cs b/Slim.Integration.Tests/SlimServiceContainerTests.cs index 56b5bdd..b40af4d 100644 --- a/Slim.Integration.Tests/SlimServiceContainerTests.cs +++ b/Slim.Integration.Tests/SlimServiceContainerTests.cs @@ -1,5 +1,6 @@ using System.ComponentModel.Design; using FluentAssertions; +using Slim.Exceptions; using Slim.Integration.ServiceContainer; using Slim.Integration.Tests.Models; @@ -65,18 +66,27 @@ public class SlimServiceContainerTests } [TestMethod] - public void RemoveService_NotSupported() + public void RemoveService_RemovesService() { - var action = () => this.slimServiceContainer.RemoveService(typeof(ITestService)); + this.slimServiceContainer.AddService(typeof(ITestService), new TestService()); + this.slimServiceContainer.RemoveService(typeof(ITestService)); - action.Should().Throw(); + var action = () => this.slimServiceContainer.GetService(typeof(ITestService)); + + action.Should().Throw(); } [TestMethod] - public void RemoveServiceWithPromotion_NotSupported() + public void RemoveServiceWithPromotion_RemoveServiceFromParent() { - var action = () => this.slimServiceContainer.RemoveService(typeof(ITestService), true); + var testService = new TestService(); + var parent = new ServiceManager() { AllowScopedManagerModifications = true }; + var child = parent.CreateScope().As(); + var container = new SlimServiceContainer(child); - action.Should().Throw(); + container.RemoveService(typeof(TestService), true); + var action = () => parent.GetService(typeof(ITestService)); + + action.Should().Throw(); } } diff --git a/Slim.Integration/ServiceContainer/SlimServiceContainer.cs b/Slim.Integration/ServiceContainer/SlimServiceContainer.cs index 4e7f0c3..021c7ce 100644 --- a/Slim.Integration/ServiceContainer/SlimServiceContainer.cs +++ b/Slim.Integration/ServiceContainer/SlimServiceContainer.cs @@ -66,11 +66,22 @@ public class SlimServiceContainer : IServiceContainer public void RemoveService(Type serviceType) { - throw new NotSupportedException("Removing one service is not currently supported"); + this.serviceManager.Remove(serviceType); } public void RemoveService(Type serviceType, bool promote) { - throw new NotSupportedException("Removing one service is not currently supported"); + var manager = this.serviceManager; + if (promote is false) + { + manager.Remove(serviceType); + return; + } + + while (manager is not null) + { + manager.Remove(serviceType); + manager = manager.ParentServiceManager; + } } } diff --git a/Slim.Integration/Slim.Integration.csproj b/Slim.Integration/Slim.Integration.csproj index 4020360..10933aa 100644 --- a/Slim.Integration/Slim.Integration.csproj +++ b/Slim.Integration/Slim.Integration.csproj @@ -8,10 +8,10 @@ Integration with Microsoft.DependencyInjection library. - 1.0.3 - 1.0.3 + 1.0.4 + 1.0.4 https://github.com/AlexMacocian/Slim - 1.0.3 + 1.0.4 true true LICENSE @@ -19,7 +19,7 @@ - +