Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update behavior of ObjectContainer.IsRegistered() to check base container for registrations #367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Improve code-behind feature file compilation speed (#336)
* Improve parameter type naming for generic types (#343)
* Reduced MsBuild log output and consistent use of [Reqnroll] prefix (#381)
* Update behavior of `ObjectContainer.IsRegistered()` to check base container for registrations, to match `Resolve()` behavior (#367)
Copy link
Contributor Author

@DrEsteban DrEsteban Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this is a "breaking"/behavior change, is there any major/minor version specification I should be revving as part of this PR? Or will maintainers handle that as part of the next release?

Is this message clear enough to indicate it's a behavior change? I see historical change entries for bug fixes have a Fix: prefix, and new features/support seem to not have anything. Should there be a Breaking: (or similar) prefix to this one? (It could be argued it's a Fix for behavior it should have always had...or a "design bug fix"...but it's not really a traditional code bug.)

If you'd like any of the above☝️ concepts solidified, let me know and I can create another PR to update the CONTRIBUTING.md.


## Bug fixes:
* MsTest: Only use TestContext for output and not Console.WriteLine (#368)
Expand Down
6 changes: 3 additions & 3 deletions Reqnroll/BoDi/IObjectContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

namespace Reqnroll.BoDi;
Expand Down Expand Up @@ -109,15 +109,15 @@ public interface IObjectContainer : IDisposable
IEnumerable<T> ResolveAll<T>() where T : class;

/// <summary>
/// Determines whether the interface or type is registered optionally with the specified name.
/// Determines whether the interface or type is registered in the container, optionally with the specified name.
/// </summary>
/// <typeparam name="T">The interface or type.</typeparam>
/// <param name="name">The name or <c>null</c>.</param>
/// <returns><c>true</c> if the interface or type is registered; otherwise <c>false</c>.</returns>
bool IsRegistered<T>(string name = null);

/// <summary>
/// Determines whether the interface or type is registered with the specified name.
/// Determines whether the interface or type is registered in the container, optionally with the specified name.
/// </summary>
/// <param name="type">The interface or type.</param>
/// <param name="name">The name.</param>
Expand Down
15 changes: 14 additions & 1 deletion Reqnroll/BoDi/ObjectContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,26 @@ public IStrategyRegistration RegisterFactoryAs(Delegate factoryDelegate, Type in
return factoryRegistration;
}

/// <inheritdoc/>
public bool IsRegistered<T>(string name = null) => IsRegistered(typeof(T), name);

/// <inheritdoc/>
public bool IsRegistered(Type type, string name = null)
{
var keyToResolve = new RegistrationKey(type, name);

return _registrations.ContainsKey(keyToResolve);
if (_registrations.ContainsKey(keyToResolve))
{
return true;
}
else if (BaseContainer != null)
{
// Recursively check the base container
return BaseContainer.IsRegistered(type, name);
}

// We are at the top of the container hierarchy and the registration is not found
return false;
}

// ReSharper disable once UnusedParameter.Local
Expand Down
74 changes: 74 additions & 0 deletions Tests/Reqnroll.RuntimeTests/BoDi/IsRegisteredTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ public void ShouldReturnFalseIfTypeNotRegistered()
isRegistered.Should().BeFalse();
}

[Fact]
public void ShouldReturnFalseIfTypeNotRegisteredWithParent()
{
// given
var parentContainer = new ObjectContainer();
var container = new ObjectContainer(parentContainer);

// then
bool isRegistered = container.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeFalse();
}

[Fact]
public void ShouldReturnTrueIfInterfaceRegistered()
{
Expand Down Expand Up @@ -59,5 +71,67 @@ public void ShouldReturnTrueIfTypeRegistered()

isRegistered.Should().BeTrue();
}

[Fact]
public void ShouldReturnTrueIfTypeRegisteredInParent()
{
// given
var grandparentContainer = new ObjectContainer();
var parentContainer = new ObjectContainer(grandparentContainer);
var container = new ObjectContainer(parentContainer);

// when
parentContainer.RegisterInstanceAs(new SimpleClassWithDefaultCtor());

// then
bool isRegistered = container.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeTrue();

isRegistered = parentContainer.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeTrue();

isRegistered = grandparentContainer.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeFalse();
}

[Fact]
public void ShouldReturnTrueIfTypeRegisteredInGrandparent()
{
// given
var grandparentContainer = new ObjectContainer();
var parentContainer = new ObjectContainer(grandparentContainer);
var container = new ObjectContainer(parentContainer);

// when
grandparentContainer.RegisterInstanceAs(new SimpleClassWithDefaultCtor());

// then
bool isRegistered = container.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeTrue();

isRegistered = parentContainer.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeTrue();

isRegistered = grandparentContainer.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeTrue();
}

[Fact]
public void ShouldReturnTrueIfRegisteredInSelfButFalseInParent()
{
// given
var parentContainer = new ObjectContainer();
var container = new ObjectContainer(parentContainer);

// when
container.RegisterInstanceAs(new SimpleClassWithDefaultCtor());

// then
bool isRegistered = container.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeTrue();

isRegistered = parentContainer.IsRegistered<SimpleClassWithDefaultCtor>();
isRegistered.Should().BeFalse();
}
}
}
Loading