Skip to content

Commit

Permalink
Added IsRegistered
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Csajtai committed May 14, 2017
1 parent ec08f14 commit 1b7fc6e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/stashbox.tests/ContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ITest1, Test1>();
container.RegisterType<ITest2, Test2>(context => context.WithName("test"));

var child = container.CreateChildContainer();

Assert.IsTrue(container.IsRegistered<ITest1>());
Assert.IsTrue(container.IsRegistered<ITest2>("test"));
Assert.IsTrue(container.IsRegistered(typeof(ITest1)));
Assert.IsFalse(container.IsRegistered<IEnumerable<ITest1>>());

Assert.IsFalse(child.IsRegistered<ITest1>());
Assert.IsFalse(child.IsRegistered(typeof(ITest1)));
Assert.IsFalse(child.IsRegistered<IEnumerable<ITest1>>());
}

[TestMethod]
public void ContainerTests_ResolverTest()
{
Expand Down Expand Up @@ -180,7 +199,7 @@ public class Test4 : ITest1
{
public Test4(ITest3 test3)
{

}
}

Expand Down
16 changes: 16 additions & 0 deletions src/stashbox/Infrastructure/IStashboxContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public interface IStashboxContainer : IDependencyRegistrator, IDependencyResolve
/// <returns>True if the service can be resolved, otherwise false.</returns>
bool CanResolve(Type typeFrom, object name = null);

/// <summary>
/// Checks a type is registered in the container.
/// </summary>
/// <typeparam name="TFrom">The service type.</typeparam>
/// <param name="name">The registration name.</param>
/// <returns>True if the service is registered, otherwise false.</returns>
bool IsRegistered<TFrom>(object name = null);

/// <summary>
/// Checks a type is registered in the container.
/// </summary>
/// <param name="typeFrom">The service type.</param>
/// <param name="name">The registration name.</param>
/// <returns>True if the service is registered, otherwise false.</returns>
bool IsRegistered(Type typeFrom, object name = null);

/// <summary>
/// Configures the container.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/stashbox/StashboxContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

/// <inheritdoc />
public bool IsRegistered<TFrom>(object name = null) =>
this.IsRegistered(typeof(TFrom), name);

/// <inheritdoc />
public bool IsRegistered(Type typeFrom, object name = null) =>
this.registrationRepository.ContainsRegistration(typeFrom, name);

/// <inheritdoc />
public void Validate()
{
Expand Down

0 comments on commit 1b7fc6e

Please sign in to comment.