Skip to content

Commit

Permalink
Enhance bodi exceptions to include name of interface when duplicate r…
Browse files Browse the repository at this point in the history
…egistrations attempted (#324)

* Enhance BoDi error handling exception message to include the name of the Interface being registered when an instance for that interface has already been registered and resolved.

* Update CHANGELOG.md

Added committer name to changelog

* Update Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs

Co-authored-by: Paul "Code Grump" Turner <[email protected]>

* Update Reqnroll/BoDi/ObjectContainer.cs

Co-authored-by: Paul "Code Grump" Turner <[email protected]>

* Modified registration error message per comment.

---------

Co-authored-by: Paul "Code Grump" Turner <[email protected]>
Co-authored-by: Gáspár Nagy <[email protected]>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent 4396c11 commit 030e854
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
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

0 comments on commit 030e854

Please sign in to comment.