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

Enhance bodi exceptions to include name of interface when duplicate registrations attempted #324

Merged
merged 6 commits into from
Nov 19, 2024
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

## Improvements:

* Enhance BoDi error handling to provide the name of the interface being registered when that interface has already been resolved (#324)

## Bug fixes:

*Contributors of this release (in alphabetical order):* @obligaron
*Contributors of this release (in alphabetical order):* @clrudolphi, @obligaron

# v2.2.1 - 2024-11-08

Expand Down
4 changes: 2 additions & 2 deletions Reqnroll/BoDi/ObjectContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public IStrategyRegistration RegisterTypeAs<TType, TInterface>(string name = nul
public IStrategyRegistration RegisterTypeAs(Type implementationType, Type interfaceType, string name = null)
{
if (!IsValidTypeMapping(implementationType, interfaceType))
throw new InvalidOperationException("type mapping is not valid");
throw new InvalidOperationException($"The type mapping is not valid. The {implementationType} is not assignable to the interface {interfaceType}");
return RegisterTypeAsInternal(implementationType, interfaceType, name);
}

Expand Down Expand Up @@ -500,7 +500,7 @@ public bool IsRegistered(Type type, string name = null)
private void AssertNotResolved(RegistrationKey interfaceType)
{
if (_resolvedKeys.Contains(interfaceType))
throw new ObjectContainerException("An object has been resolved for this interface already.", null);
throw new ObjectContainerException($"An object has already been resolved for the interface \"{interfaceType}\".", null);
}

private void ClearRegistrations(RegistrationKey registrationKey)
Expand Down
3 changes: 2 additions & 1 deletion Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public void ShouldNotAllowOverrideRegistrationAfterResolve()

// when
Action act = () => container.RegisterTypeAs<SimpleClassWithDefaultCtor, IInterface1>();
act.Should().ThrowExactly<ObjectContainerException>();
act.Should().ThrowExactly<ObjectContainerException>()
.And.Message.Should().Contain("IInterface1");
}

[Fact]
Expand Down