From 46cc9b1f9422aec14168afad437dc37d68fdab0e Mon Sep 17 00:00:00 2001 From: Chris Rudolphi <1702962+clrudolphi@users.noreply.github.com> Date: Sun, 10 Nov 2024 16:34:45 -0600 Subject: [PATCH 1/5] 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. --- CHANGELOG.md | 1 + Reqnroll/BoDi/ObjectContainer.cs | 4 ++-- Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98078a661..d9b7f5680 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Improvements: ## Bug fixes: +* Enhance BoDi error handling to provide the name of the Interface being registered when that Interface has already been resolved. *Contributors of this release (in alphabetical order):* diff --git a/Reqnroll/BoDi/ObjectContainer.cs b/Reqnroll/BoDi/ObjectContainer.cs index 9f7df16c6..8c007a43e 100644 --- a/Reqnroll/BoDi/ObjectContainer.cs +++ b/Reqnroll/BoDi/ObjectContainer.cs @@ -364,7 +364,7 @@ public IStrategyRegistration RegisterTypeAs(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($"type mapping is not valid: {implementationType} -> {interfaceType}"); return RegisterTypeAsInternal(implementationType, interfaceType, name); } @@ -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 been resolved for this interface ({interfaceType.ToString()}) already.", null); } private void ClearRegistrations(RegistrationKey registrationKey) diff --git a/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs b/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs index 762dfff5d..5b82d645d 100644 --- a/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs +++ b/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs @@ -65,7 +65,8 @@ public void ShouldNotAllowOverrideRegistrationAfterResolve() // when Action act = () => container.RegisterTypeAs(); - act.Should().ThrowExactly(); + var e = act.Should().ThrowExactly(); + e.Which.Message.Should().Contain("IInterface1"); } [Fact] From b1508f6deabfcde28defd807716b8dfa8e0d90ea Mon Sep 17 00:00:00 2001 From: Chris Rudolphi <1702962+clrudolphi@users.noreply.github.com> Date: Sun, 10 Nov 2024 16:41:01 -0600 Subject: [PATCH 2/5] Update CHANGELOG.md Added committer name to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9b7f5680..1ae2d320d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * Enhance BoDi error handling to provide the name of the Interface being registered when that Interface has already been resolved. *Contributors of this release (in alphabetical order):* +clrudolphi # v2.2.1 - 2024-11-08 From 1f6088b8ab86147f42814c4734a83a939e6944cc Mon Sep 17 00:00:00 2001 From: Chris Rudolphi <1702962+clrudolphi@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:31:16 -0600 Subject: [PATCH 3/5] Update Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs Co-authored-by: Paul "Code Grump" Turner --- Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs b/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs index 5b82d645d..9af8d5e2a 100644 --- a/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs +++ b/Tests/Reqnroll.RuntimeTests/BoDi/RegisterTypeTests.cs @@ -65,8 +65,8 @@ public void ShouldNotAllowOverrideRegistrationAfterResolve() // when Action act = () => container.RegisterTypeAs(); - var e = act.Should().ThrowExactly(); - e.Which.Message.Should().Contain("IInterface1"); + act.Should().ThrowExactly() + .And.Message.Should().Contain("IInterface1"); } [Fact] From 16e4e9f88c1ee574d325d5d4d68b5dbd1e8e802b Mon Sep 17 00:00:00 2001 From: Chris Rudolphi <1702962+clrudolphi@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:31:27 -0600 Subject: [PATCH 4/5] Update Reqnroll/BoDi/ObjectContainer.cs Co-authored-by: Paul "Code Grump" Turner --- Reqnroll/BoDi/ObjectContainer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Reqnroll/BoDi/ObjectContainer.cs b/Reqnroll/BoDi/ObjectContainer.cs index 8c007a43e..35905d159 100644 --- a/Reqnroll/BoDi/ObjectContainer.cs +++ b/Reqnroll/BoDi/ObjectContainer.cs @@ -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 ({interfaceType.ToString()}) already.", null); + throw new ObjectContainerException($"An object has already been resolved for the interface \"{interfaceType}\".", null); } private void ClearRegistrations(RegistrationKey registrationKey) From 9e7aea42faea8ec55003f9f39563ede92eff605d Mon Sep 17 00:00:00 2001 From: Chris Rudolphi <1702962+clrudolphi@users.noreply.github.com> Date: Tue, 12 Nov 2024 12:04:30 -0600 Subject: [PATCH 5/5] Modified registration error message per comment. --- Reqnroll/BoDi/ObjectContainer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Reqnroll/BoDi/ObjectContainer.cs b/Reqnroll/BoDi/ObjectContainer.cs index 35905d159..4b14d39b8 100644 --- a/Reqnroll/BoDi/ObjectContainer.cs +++ b/Reqnroll/BoDi/ObjectContainer.cs @@ -364,7 +364,7 @@ public IStrategyRegistration RegisterTypeAs(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: {implementationType} -> {interfaceType}"); + throw new InvalidOperationException($"The type mapping is not valid. The {implementationType} is not assignable to the interface {interfaceType}"); return RegisterTypeAsInternal(implementationType, interfaceType, name); }