Add support for remove in servicecontainers (#27)

This commit is contained in:
2022-09-11 14:00:21 +02:00
committed by GitHub
parent 65c00b540b
commit b27e7a984a
3 changed files with 33 additions and 12 deletions
@@ -1,5 +1,6 @@
using System.ComponentModel.Design; using System.ComponentModel.Design;
using FluentAssertions; using FluentAssertions;
using Slim.Exceptions;
using Slim.Integration.ServiceContainer; using Slim.Integration.ServiceContainer;
using Slim.Integration.Tests.Models; using Slim.Integration.Tests.Models;
@@ -65,18 +66,27 @@ public class SlimServiceContainerTests
} }
[TestMethod] [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<NotSupportedException>(); var action = () => this.slimServiceContainer.GetService(typeof(ITestService));
action.Should().Throw<DependencyInjectionException>();
} }
[TestMethod] [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<ServiceManager>();
var container = new SlimServiceContainer(child);
action.Should().Throw<NotSupportedException>(); container.RemoveService(typeof(TestService), true);
var action = () => parent.GetService(typeof(ITestService));
action.Should().Throw<DependencyInjectionException>();
} }
} }
@@ -66,11 +66,22 @@ public class SlimServiceContainer : IServiceContainer
public void RemoveService(Type serviceType) 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) 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;
}
} }
} }
+4 -4
View File
@@ -8,10 +8,10 @@
<Company /> <Company />
<Product></Product> <Product></Product>
<Description>Integration with Microsoft.DependencyInjection library.</Description> <Description>Integration with Microsoft.DependencyInjection library.</Description>
<AssemblyVersion>1.0.3</AssemblyVersion> <AssemblyVersion>1.0.4</AssemblyVersion>
<FileVersion>1.0.3</FileVersion> <FileVersion>1.0.4</FileVersion>
<PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl> <PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl>
<Version>1.0.3</Version> <Version>1.0.4</Version>
<EnableNETAnalyzers>true</EnableNETAnalyzers> <EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
@@ -19,7 +19,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Slim" Version="1.9.1" /> <PackageReference Include="Slim" Version="1.9.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>