From 0b5f24ea0494dc39f87aaca883c662a67b8c98bb Mon Sep 17 00:00:00 2001 From: JCH2k Date: Fri, 22 Apr 2022 18:02:16 +0200 Subject: [PATCH 1/3] Fixed validation with parameter for registrations without factory --- .../Validation/IocValidator.cs | 6 ++++- .../IocValidatorTest.cs | 26 +++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 231e801..6381704 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -61,7 +61,11 @@ namespace LightweightIocContainer.Validation .ForEach(p => TryResolve(registration.InterfaceType, p, validationExceptions)); } else - TryResolve(registration.InterfaceType, null, validationExceptions); + { + var parameters = _parameters.Where(p => p.type == registration.InterfaceType); + var arguments = parameters.Select(p => p.parameter).ToArray(); + TryResolve(registration.InterfaceType, arguments, validationExceptions); + } } if (validationExceptions.Any()) diff --git a/Test.LightweightIocContainer/IocValidatorTest.cs b/Test.LightweightIocContainer/IocValidatorTest.cs index a9037d3..f423d3d 100644 --- a/Test.LightweightIocContainer/IocValidatorTest.cs +++ b/Test.LightweightIocContainer/IocValidatorTest.cs @@ -45,12 +45,17 @@ namespace Test.LightweightIocContainer ITest Create(); } - private class TestInstaller : IIocInstaller + private class TestInstallerNoFactory : IIocInstaller + { + public void Install(IRegistrationCollector registration) => registration.Add(); + } + + private class TestInstallerWithFactory : IIocInstaller { public void Install(IRegistrationCollector registration) => registration.Add().WithFactory(); } - private class InvalidTestInstaller : IIocInstaller + private class TestInstallerWithInvalidFactory : IIocInstaller { public void Install(IRegistrationCollector registration) => registration.Add().WithFactory(); } @@ -59,7 +64,18 @@ namespace Test.LightweightIocContainer public void TestValidate() { IocContainer iocContainer = new(); - iocContainer.Install(new TestInstaller()); + iocContainer.Install(new TestInstallerNoFactory()); + + IocValidator validator = new(iocContainer); + + validator.Validate(); + } + + [Test] + public void TestValidate_WithFactory() + { + IocContainer iocContainer = new(); + iocContainer.Install(new TestInstallerWithFactory()); IocValidator validator = new(iocContainer); @@ -70,7 +86,7 @@ namespace Test.LightweightIocContainer public void TestValidateWithParameter() { IocContainer iocContainer = new(); - iocContainer.Install(new TestInstaller()); + iocContainer.Install(new TestInstallerNoFactory()); IocValidator validator = new(iocContainer); @@ -88,7 +104,7 @@ namespace Test.LightweightIocContainer public void TestValidateInvalidFactory() { IocContainer iocContainer = new(); - iocContainer.Install(new InvalidTestInstaller()); + iocContainer.Install(new TestInstallerWithInvalidFactory()); IocValidator validator = new(iocContainer); From 75233628d084106296a90e74c0c1630ae02f9444 Mon Sep 17 00:00:00 2001 From: JCH2k Date: Wed, 15 Jun 2022 14:11:28 +0200 Subject: [PATCH 2/3] refactoring as suggested in PR #57 --- LightweightIocContainer/Validation/IocValidator.cs | 6 +++--- Test.LightweightIocContainer/IocValidatorTest.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 6381704..4c61068 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -47,12 +47,13 @@ namespace LightweightIocContainer.Validation foreach (IRegistration registration in _iocContainer.Registrations) { + var definedParameters = _parameters.Where(p => p.type == registration.InterfaceType); + if (registration is IWithFactoryInternal { Factory: { } } withFactoryRegistration) { (from createMethod in withFactoryRegistration.Factory.CreateMethods select createMethod.GetParameters().Select(p => p.ParameterType) into parameterTypes - let definedParameters = _parameters.Where(p => p.type == registration.InterfaceType) select (from parameterType in parameterTypes let definedParameter = definedParameters .FirstOrDefault(p => parameterType.IsInstanceOfType(p.parameter)) @@ -62,8 +63,7 @@ namespace LightweightIocContainer.Validation } else { - var parameters = _parameters.Where(p => p.type == registration.InterfaceType); - var arguments = parameters.Select(p => p.parameter).ToArray(); + var arguments = definedParameters.Select(p => p.parameter).ToArray(); TryResolve(registration.InterfaceType, arguments, validationExceptions); } } diff --git a/Test.LightweightIocContainer/IocValidatorTest.cs b/Test.LightweightIocContainer/IocValidatorTest.cs index f423d3d..181308e 100644 --- a/Test.LightweightIocContainer/IocValidatorTest.cs +++ b/Test.LightweightIocContainer/IocValidatorTest.cs @@ -72,7 +72,7 @@ namespace Test.LightweightIocContainer } [Test] - public void TestValidate_WithFactory() + public void TestValidateWithFactory() { IocContainer iocContainer = new(); iocContainer.Install(new TestInstallerWithFactory()); From 9547685b51206c30f17ace8d65f135ccd8cd2d58 Mon Sep 17 00:00:00 2001 From: JCH2k Date: Wed, 15 Jun 2022 14:21:53 +0200 Subject: [PATCH 3/3] Adapted test PR #57 --- .../IocValidatorTest.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Test.LightweightIocContainer/IocValidatorTest.cs b/Test.LightweightIocContainer/IocValidatorTest.cs index 181308e..7af50ee 100644 --- a/Test.LightweightIocContainer/IocValidatorTest.cs +++ b/Test.LightweightIocContainer/IocValidatorTest.cs @@ -68,7 +68,9 @@ namespace Test.LightweightIocContainer IocValidator validator = new(iocContainer); - validator.Validate(); + var aggregateException = Assert.Throws(() => validator.Validate()); + + AssertNoMatchingConstructorFoundForType(aggregateException); } [Test] @@ -108,13 +110,18 @@ namespace Test.LightweightIocContainer IocValidator validator = new(iocContainer); - AggregateException aggregateException = Assert.Throws(() => validator.Validate()); + var aggregateException = Assert.Throws(() => validator.Validate()); - Exception exception = aggregateException?.InnerExceptions[0]; + AssertNoMatchingConstructorFoundForType(aggregateException); + } + + private static void AssertNoMatchingConstructorFoundForType(AggregateException aggregateException) + { + Exception exception = aggregateException?.InnerExceptions[0]; Assert.IsInstanceOf(exception); - - NoMatchingConstructorFoundException noMatchingConstructorFoundException = (NoMatchingConstructorFoundException) exception; - Assert.AreEqual(typeof(Test), noMatchingConstructorFoundException?.Type); + + NoMatchingConstructorFoundException noMatchingConstructorFoundException = (NoMatchingConstructorFoundException)exception; + Assert.AreEqual(typeof(T), noMatchingConstructorFoundException?.Type); } } } \ No newline at end of file