diff --git a/Slim.Tests/ServiceManagerTests.cs b/Slim.Tests/ServiceManagerTests.cs index aef3af6..a438a20 100644 --- a/Slim.Tests/ServiceManagerTests.cs +++ b/Slim.Tests/ServiceManagerTests.cs @@ -642,5 +642,31 @@ namespace Slim.Tests action.Should().Throw(); action2.Should().Throw(); } + + [TestMethod] + public void IsRegisteredReturnsTrueForRegisteredService() + { + var di = new ServiceManager(); + di.RegisterSingleton(); + di.RegisterSingleton(typeof(IDependentService), typeof(DependentService)); + + var isRegistered1 = di.IsRegistered(); + var isRegistered2 = di.IsRegistered(typeof(IDependentService)); + + isRegistered1.Should().BeTrue(); + isRegistered2.Should().BeTrue(); + } + + [TestMethod] + public void IsRegisteredReturnsFalseForNonRegisteredService() + { + var di = new ServiceManager(); + + var isRegistered1 = di.IsRegistered(); + var isRegistered2 = di.IsRegistered(typeof(IDependentService)); + + isRegistered1.Should().BeFalse(); + isRegistered2.Should().BeFalse(); + } } } diff --git a/Slim/IServiceManager.cs b/Slim/IServiceManager.cs index 426af6c..9264f02 100644 --- a/Slim/IServiceManager.cs +++ b/Slim/IServiceManager.cs @@ -9,6 +9,18 @@ namespace Slim /// public interface IServiceManager : IServiceProvider, IServiceProducer { + /// + /// Returns true if there exists a registration for . + /// + /// Type of registered service. + /// True if service is registered. Otherwise returns false. + public bool IsRegistered(Type tInterface); + /// + /// Returns true if there exists a registration for . + /// + /// Type of registered service. + /// True if service is registered. Otherwise returns false. + public bool IsRegistered(); /// /// Returns all services that can be cast to provided type. /// diff --git a/Slim/ServiceManager.cs b/Slim/ServiceManager.cs index 794a202..61d2438 100644 --- a/Slim/ServiceManager.cs +++ b/Slim/ServiceManager.cs @@ -43,6 +43,24 @@ namespace Slim this.scoped = true; } + /// + /// Returns true if there exists a registration for . + /// + /// Type of registered service. + /// True if service is registered. Otherwise returns false. + public bool IsRegistered(Type tInterface) + { + return this.IsMapped(tInterface); + } + /// + /// Returns true if there exists a registration for . + /// + /// Type of registered service. + /// True if service is registered. Otherwise returns false. + public bool IsRegistered() + { + return this.IsMapped(typeof(T)); + } /// /// Register a service into with . /// @@ -565,6 +583,16 @@ namespace Slim } }); } + private bool IsMapped(Type tinterface) + { + return this.TryFunc(() => + { + lock (this) + { + return this.InterfaceMapping.ContainsKey(tinterface); + } + }); + } private object GetObject(Type tInterface) { if (!this.InterfaceMapping.TryGetValue(tInterface, out var mappingTuple) && diff --git a/Slim/Slim.csproj b/Slim/Slim.csproj index fd72971..7132fb1 100644 --- a/Slim/Slim.csproj +++ b/Slim/Slim.csproj @@ -8,10 +8,10 @@ Service lifetime management and dependency injection framework. - 1.5.4 - 1.5.4 + 1.5.5 + 1.5.5 https://github.com/AlexMacocian/Slim - 1.5.4 + 1.5.5 true true LICENSE diff --git a/Slim/Slim.xml b/Slim/Slim.xml index 2b408b2..25d29b9 100644 --- a/Slim/Slim.xml +++ b/Slim/Slim.xml @@ -39,6 +39,20 @@ Interface for . + + + Returns true if there exists a registration for . + + Type of registered service. + True if service is registered. Otherwise returns false. + + + + Returns true if there exists a registration for . + + Type of registered service. + True if service is registered. Otherwise returns false. + Returns all services that can be cast to provided type. @@ -367,6 +381,20 @@ Creates an instance of . + + + Returns true if there exists a registration for . + + Type of registered service. + True if service is registered. Otherwise returns false. + + + + Returns true if there exists a registration for . + + Type of registered service. + True if service is registered. Otherwise returns false. + Register a service into with .