Make Slim respect nullable contract of System.IServiceProvider

This commit is contained in:
2025-07-30 00:57:55 +02:00
parent 6c4edd3ef8
commit 6d49832566
4 changed files with 20 additions and 43 deletions
+10 -28
View File
@@ -68,10 +68,11 @@ public class ServiceManagerTests
}
[TestMethod]
public void RetrieveThrowsNotRegistered()
public void RetrieveNotRegisteredReturnsNull()
{
var di = new ServiceManager();
Assert.ThrowsException<DependencyInjectionException>(() => { di.GetService<IDependentService>(); });
di.GetService<IDependentService>().Should().BeNull();
}
[TestMethod]
@@ -311,18 +312,14 @@ public class ServiceManagerTests
}
[TestMethod]
public void ClearAndGetShouldThrow()
public void ClearAndGetShouldReturnNull()
{
var di = new ServiceManager();
di.RegisterSingleton<IIndependentService, IndependentService>();
var action = new Action(() =>
{
di.GetService<IndependentService>();
});
di.Clear();
action.Should().Throw<DependencyInjectionException>();
di.GetService<IndependentService>().Should().BeNull();
}
[TestMethod]
@@ -635,17 +632,8 @@ public class ServiceManagerTests
di.RegisterSingleton<MultiInterfaceService>();
di.GetService<MultiInterfaceService>().Should().NotBeNull();
var action = new Action(() =>
{
di.GetService<IMultiInterfaceService1>();
});
var action2 = new Action(() =>
{
di.GetService<IMultiInterfaceService2>();
});
action.Should().Throw<Exception>();
action2.Should().Throw<Exception>();
di.GetService<IMultiInterfaceService1>().Should().BeNull();
di.GetService<IMultiInterfaceService2>().Should().BeNull();
}
[TestMethod]
@@ -791,12 +779,7 @@ public class ServiceManagerTests
var di = new ServiceManager();
di.RegisterSingleton<DoNotInjectConstructorService>();
var action = () =>
{
var service = di.GetService<ServiceWithPrefferedConstructor>();
};
action.Should().Throw<DependencyInjectionException>();
di.GetService<ServiceWithPrefferedConstructor>().Should().BeNull();
}
[TestMethod]
@@ -842,10 +825,9 @@ public class ServiceManagerTests
var service = di.GetService<IIndependentService>();
di.Remove<IIndependentService>();
var action = () => di.GetService<IIndependentService>();
service.Should().BeOfType<IndependentService>();
action.Should().Throw<DependencyInjectionException>();
di.GetService<IIndependentService>().Should().BeNull();
}
[TestMethod]
+1 -1
View File
@@ -30,5 +30,5 @@ public interface IServiceProvider : System.IServiceProvider, IDisposable
/// <typeparam name="TInterface">Type of required service.</typeparam>
/// <returns>Required service.</returns>
/// <exception cref="DependencyInjectionException">Thrown when unable to resolve required service.</exception>
TInterface GetService<TInterface>() where TInterface : class;
TInterface? GetService<TInterface>() where TInterface : class;
}
+6 -11
View File
@@ -453,14 +453,9 @@ public sealed class ServiceManager : IServiceManager
/// <typeparam name="TInterface">Type of required service.</typeparam>
/// <returns>Required service.</returns>
/// <exception cref="DependencyInjectionException">Thrown when unable to resolve required service.</exception>
public TInterface GetService<TInterface>() where TInterface : class
public TInterface? GetService<TInterface>() where TInterface : class
{
if (this.PrepareAndGetService(typeof(TInterface)) is not TInterface result)
{
throw new DependencyInjectionException($"Unable to resolve service of type {typeof(TInterface).FullName}.");
}
return result;
return this.PrepareAndGetService(typeof(TInterface)) as TInterface;
}
/// <summary>
@@ -469,9 +464,9 @@ public sealed class ServiceManager : IServiceManager
/// <param name="type">Type of required service.</param>
/// <returns>Required service.</returns>
/// <exception cref="DependencyInjectionException">Thrown when unable to resolve required service.</exception>
public object GetService(Type type)
public object? GetService(Type type)
{
return this.PrepareAndGetService(type) ?? throw new DependencyInjectionException($"Unable to resolve service of type {type.FullName}");
return this.PrepareAndGetService(type);
}
/// <summary>
@@ -724,7 +719,7 @@ public sealed class ServiceManager : IServiceManager
});
}
private object GetObject(Type tInterface)
private object? GetObject(Type tInterface)
{
if (IsServiceManagerDependency(tInterface))
{
@@ -734,7 +729,7 @@ public sealed class ServiceManager : IServiceManager
if (!this.InterfaceMapping.TryGetValue(tInterface, out var mappingTuples) &&
this.Resolvers.Any(resolver => resolver.CanResolve(tInterface)) is false)
{
throw new DependencyInjectionException($"No registered service and no resolver can resolve for type {tInterface.Name}.");
return default;
}
var tuple = mappingTuples?.FirstOrDefault();
+3 -3
View File
@@ -8,10 +8,10 @@
<Company />
<Product></Product>
<Description>Service lifetime management and dependency injection framework.</Description>
<AssemblyVersion>1.10.2</AssemblyVersion>
<FileVersion>1.10.2</FileVersion>
<AssemblyVersion>1.11.0</AssemblyVersion>
<FileVersion>1.11.0</FileVersion>
<PackageProjectUrl>https://github.com/AlexMacocian/Slim</PackageProjectUrl>
<Version>1.10.2</Version>
<Version>1.11.0</Version>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>