From 1b7fc6e65625c905f95b7b3ad3b84b5b9d5f6793 Mon Sep 17 00:00:00 2001 From: Peter Csajtai Date: Sun, 14 May 2017 13:35:07 +0200 Subject: [PATCH] Added IsRegistered --- src/stashbox.tests/ContainerTests.cs | 21 ++++++++++++++++++- .../Infrastructure/IStashboxContainer.cs | 16 ++++++++++++++ src/stashbox/StashboxContainer.cs | 8 +++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/stashbox.tests/ContainerTests.cs b/src/stashbox.tests/ContainerTests.cs index 5934979b..58cac489 100644 --- a/src/stashbox.tests/ContainerTests.cs +++ b/src/stashbox.tests/ContainerTests.cs @@ -123,6 +123,25 @@ public void ContainerTests_CanResolve() Assert.IsFalse(container.CanResolve(typeof(ITest1), "test")); } + [TestMethod] + public void ContainerTests_IsRegistered() + { + var container = new StashboxContainer(); + container.RegisterType(); + container.RegisterType(context => context.WithName("test")); + + var child = container.CreateChildContainer(); + + Assert.IsTrue(container.IsRegistered()); + Assert.IsTrue(container.IsRegistered("test")); + Assert.IsTrue(container.IsRegistered(typeof(ITest1))); + Assert.IsFalse(container.IsRegistered>()); + + Assert.IsFalse(child.IsRegistered()); + Assert.IsFalse(child.IsRegistered(typeof(ITest1))); + Assert.IsFalse(child.IsRegistered>()); + } + [TestMethod] public void ContainerTests_ResolverTest() { @@ -180,7 +199,7 @@ public class Test4 : ITest1 { public Test4(ITest3 test3) { - + } } diff --git a/src/stashbox/Infrastructure/IStashboxContainer.cs b/src/stashbox/Infrastructure/IStashboxContainer.cs index 47ac7b58..c7956af2 100644 --- a/src/stashbox/Infrastructure/IStashboxContainer.cs +++ b/src/stashbox/Infrastructure/IStashboxContainer.cs @@ -58,6 +58,22 @@ public interface IStashboxContainer : IDependencyRegistrator, IDependencyResolve /// True if the service can be resolved, otherwise false. bool CanResolve(Type typeFrom, object name = null); + /// + /// Checks a type is registered in the container. + /// + /// The service type. + /// The registration name. + /// True if the service is registered, otherwise false. + bool IsRegistered(object name = null); + + /// + /// Checks a type is registered in the container. + /// + /// The service type. + /// The registration name. + /// True if the service is registered, otherwise false. + bool IsRegistered(Type typeFrom, object name = null); + /// /// Configures the container. /// diff --git a/src/stashbox/StashboxContainer.cs b/src/stashbox/StashboxContainer.cs index addae3fc..691d967e 100644 --- a/src/stashbox/StashboxContainer.cs +++ b/src/stashbox/StashboxContainer.cs @@ -88,6 +88,14 @@ public bool CanResolve(Type typeFrom, object name = null) => this.registrationRepository.ContainsRegistration(typeFrom, name) || this.resolverSelector.CanResolve(this.ContainerContext, new TypeInformation { Type = typeFrom, DependencyName = name }, ResolutionInfo.New(this, this)); + /// + public bool IsRegistered(object name = null) => + this.IsRegistered(typeof(TFrom), name); + + /// + public bool IsRegistered(Type typeFrom, object name = null) => + this.registrationRepository.ContainsRegistration(typeFrom, name); + /// public void Validate() {