diff --git a/Slim.Tests/ServiceManagerTests.cs b/Slim.Tests/ServiceManagerTests.cs index a0d1854..293309a 100644 --- a/Slim.Tests/ServiceManagerTests.cs +++ b/Slim.Tests/ServiceManagerTests.cs @@ -68,10 +68,11 @@ public class ServiceManagerTests } [TestMethod] - public void RetrieveThrowsNotRegistered() + public void RetrieveNotRegisteredReturnsNull() { var di = new ServiceManager(); - Assert.ThrowsException(() => { di.GetService(); }); + + di.GetService().Should().BeNull(); } [TestMethod] @@ -311,18 +312,14 @@ public class ServiceManagerTests } [TestMethod] - public void ClearAndGetShouldThrow() + public void ClearAndGetShouldReturnNull() { var di = new ServiceManager(); di.RegisterSingleton(); - var action = new Action(() => - { - di.GetService(); - }); di.Clear(); - action.Should().Throw(); + di.GetService().Should().BeNull(); } [TestMethod] @@ -635,17 +632,8 @@ public class ServiceManagerTests di.RegisterSingleton(); di.GetService().Should().NotBeNull(); - var action = new Action(() => - { - di.GetService(); - }); - var action2 = new Action(() => - { - di.GetService(); - }); - - action.Should().Throw(); - action2.Should().Throw(); + di.GetService().Should().BeNull(); + di.GetService().Should().BeNull(); } [TestMethod] @@ -791,12 +779,7 @@ public class ServiceManagerTests var di = new ServiceManager(); di.RegisterSingleton(); - var action = () => - { - var service = di.GetService(); - }; - - action.Should().Throw(); + di.GetService().Should().BeNull(); } [TestMethod] @@ -842,10 +825,9 @@ public class ServiceManagerTests var service = di.GetService(); di.Remove(); - var action = () => di.GetService(); - + service.Should().BeOfType(); - action.Should().Throw(); + di.GetService().Should().BeNull(); } [TestMethod] diff --git a/Slim/IServiceProvider.cs b/Slim/IServiceProvider.cs index 1782f6a..9eeb637 100644 --- a/Slim/IServiceProvider.cs +++ b/Slim/IServiceProvider.cs @@ -30,5 +30,5 @@ public interface IServiceProvider : System.IServiceProvider, IDisposable /// Type of required service. /// Required service. /// Thrown when unable to resolve required service. - TInterface GetService() where TInterface : class; + TInterface? GetService() where TInterface : class; } diff --git a/Slim/ServiceManager.cs b/Slim/ServiceManager.cs index 7a6f134..ba48833 100644 --- a/Slim/ServiceManager.cs +++ b/Slim/ServiceManager.cs @@ -453,14 +453,9 @@ public sealed class ServiceManager : IServiceManager /// Type of required service. /// Required service. /// Thrown when unable to resolve required service. - public TInterface GetService() where TInterface : class + public TInterface? GetService() 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; } /// @@ -469,9 +464,9 @@ public sealed class ServiceManager : IServiceManager /// Type of required service. /// Required service. /// Thrown when unable to resolve required service. - 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); } /// @@ -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(); diff --git a/Slim/Slim.csproj b/Slim/Slim.csproj index f1e74fa..130d98b 100644 --- a/Slim/Slim.csproj +++ b/Slim/Slim.csproj @@ -8,10 +8,10 @@ Service lifetime management and dependency injection framework. - 1.10.2 - 1.10.2 + 1.11.0 + 1.11.0 https://github.com/AlexMacocian/Slim - 1.10.2 + 1.11.0 true true LICENSE