From f904491d5d2f2a3a7c74537a36fdf83dcfae2a93 Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 3 Dec 2021 17:45:26 +0100 Subject: [PATCH] #44: throw collection of all exceptions thrown during validation --- .../LightweightIocContainer.csproj | 1 - .../LightweightIocContainer.xml | 1 + .../Validation/IocValidator.cs | 23 ++++++++++++-- .../IocValidatorTest.cs | 30 +++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/LightweightIocContainer/LightweightIocContainer.csproj b/LightweightIocContainer/LightweightIocContainer.csproj index cacdc6c..c5ac3c9 100644 --- a/LightweightIocContainer/LightweightIocContainer.csproj +++ b/LightweightIocContainer/LightweightIocContainer.csproj @@ -35,7 +35,6 @@ - diff --git a/LightweightIocContainer/LightweightIocContainer.xml b/LightweightIocContainer/LightweightIocContainer.xml index 8e891cd..cee4b5a 100644 --- a/LightweightIocContainer/LightweightIocContainer.xml +++ b/LightweightIocContainer/LightweightIocContainer.xml @@ -1515,6 +1515,7 @@ Validates your given and checks if everything can be resolved with the current setup + Collection of all exceptions that are thrown during validation diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 9038750..7be0935 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -17,6 +17,8 @@ namespace LightweightIocContainer.Validation private readonly IocContainer _iocContainer; private readonly List<(Type type, object parameter)> _parameters; + private List _validationExceptions; + /// /// Validator for your to check if everything can be resolved with your current setup /// @@ -37,6 +39,7 @@ namespace LightweightIocContainer.Validation /// /// Validates your given and checks if everything can be resolved with the current setup + /// Collection of all exceptions that are thrown during validation /// public void Validate() { @@ -53,10 +56,26 @@ namespace LightweightIocContainer.Validation .FirstOrDefault(p => parameterType.IsInstanceOfType(p.parameter)) select definedParameter == default ? parameterType.GetDefault() : definedParameter.parameter).ToArray()) .ToList() - .ForEach(p => _iocContainer.Resolve(registration.InterfaceType, p, null)); + .ForEach(p => TryResolve(registration.InterfaceType, p)); } else - _iocContainer.Resolve(registration.InterfaceType, null, null); + TryResolve(registration.InterfaceType); + } + + if (_validationExceptions != null) + throw new AggregateException("Validation failed.", _validationExceptions); + } + + private void TryResolve(Type type, object[] arguments = null) + { + try + { + _iocContainer.Resolve(type, arguments, null); + } + catch (Exception exception) + { + _validationExceptions ??= new List(); + _validationExceptions.Add(exception); } } } diff --git a/Test.LightweightIocContainer/IocValidatorTest.cs b/Test.LightweightIocContainer/IocValidatorTest.cs index ae26aea..a79ed89 100644 --- a/Test.LightweightIocContainer/IocValidatorTest.cs +++ b/Test.LightweightIocContainer/IocValidatorTest.cs @@ -2,8 +2,10 @@ // Created: 2021-12-03 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System; using JetBrains.Annotations; using LightweightIocContainer; +using LightweightIocContainer.Exceptions; using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Validation; @@ -37,11 +39,22 @@ namespace Test.LightweightIocContainer ITest Create(IParameter parameter); } + [UsedImplicitly] + public interface IInvalidFactory + { + ITest Create(); + } + private class TestInstaller : IIocInstaller { public void Install(IIocContainer container) => container.Register().WithFactory(); } + private class InvalidTestInstaller : IIocInstaller + { + public void Install(IIocContainer container) => container.Register().WithFactory(); + } + [Test] public void TestValidate() { @@ -70,5 +83,22 @@ namespace Test.LightweightIocContainer parameterMock.Verify(p => p.Method(), Times.Once); } + + [Test] + public void TestValidateInvalidFactory() + { + IocContainer iocContainer = new(); + iocContainer.Install(new InvalidTestInstaller()); + + IocValidator validator = new(iocContainer); + + AggregateException aggregateException = Assert.Throws(() => validator.Validate()); + + Exception exception = aggregateException?.InnerExceptions[0]; + Assert.IsInstanceOf(exception); + + NoMatchingConstructorFoundException noMatchingConstructorFoundException = (NoMatchingConstructorFoundException) exception; + Assert.AreEqual(typeof(Test), noMatchingConstructorFoundException?.Type); + } } } \ No newline at end of file