#44: throw collection of all exceptions thrown during validation

pull/57/head
Simon G 4 years ago
parent 82fd53b2b0
commit f904491d5d
  1. 1
      LightweightIocContainer/LightweightIocContainer.csproj
  2. 1
      LightweightIocContainer/LightweightIocContainer.xml
  3. 23
      LightweightIocContainer/Validation/IocValidator.cs
  4. 30
      Test.LightweightIocContainer/IocValidatorTest.cs

@ -35,7 +35,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Factories\" /> <Folder Include="Factories\" />
<Folder Include="Interfaces\Factories\" /> <Folder Include="Interfaces\Factories\" />
<Folder Include="Validation" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

@ -1515,6 +1515,7 @@
<member name="M:LightweightIocContainer.Validation.IocValidator.Validate"> <member name="M:LightweightIocContainer.Validation.IocValidator.Validate">
<summary> <summary>
Validates your given <see cref="T:LightweightIocContainer.IocContainer"/> and checks if everything can be resolved with the current setup Validates your given <see cref="T:LightweightIocContainer.IocContainer"/> and checks if everything can be resolved with the current setup
<exception cref="T:System.AggregateException">Collection of all exceptions that are thrown during validation</exception>
</summary> </summary>
</member> </member>
</members> </members>

@ -17,6 +17,8 @@ namespace LightweightIocContainer.Validation
private readonly IocContainer _iocContainer; private readonly IocContainer _iocContainer;
private readonly List<(Type type, object parameter)> _parameters; private readonly List<(Type type, object parameter)> _parameters;
private List<Exception> _validationExceptions;
/// <summary> /// <summary>
/// Validator for your <see cref="IocContainer"/> to check if everything can be resolved with your current setup /// Validator for your <see cref="IocContainer"/> to check if everything can be resolved with your current setup
/// </summary> /// </summary>
@ -37,6 +39,7 @@ namespace LightweightIocContainer.Validation
/// <summary> /// <summary>
/// Validates your given <see cref="IocContainer"/> and checks if everything can be resolved with the current setup /// Validates your given <see cref="IocContainer"/> and checks if everything can be resolved with the current setup
/// <exception cref="AggregateException">Collection of all exceptions that are thrown during validation</exception>
/// </summary> /// </summary>
public void Validate() public void Validate()
{ {
@ -53,10 +56,26 @@ namespace LightweightIocContainer.Validation
.FirstOrDefault(p => parameterType.IsInstanceOfType(p.parameter)) .FirstOrDefault(p => parameterType.IsInstanceOfType(p.parameter))
select definedParameter == default ? parameterType.GetDefault() : definedParameter.parameter).ToArray()) select definedParameter == default ? parameterType.GetDefault() : definedParameter.parameter).ToArray())
.ToList() .ToList()
.ForEach(p => _iocContainer.Resolve(registration.InterfaceType, p, null)); .ForEach(p => TryResolve(registration.InterfaceType, p));
} }
else 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<Exception>();
_validationExceptions.Add(exception);
} }
} }
} }

@ -2,8 +2,10 @@
// Created: 2021-12-03 // Created: 2021-12-03
// Copyright(c) 2021 SimonG. All Rights Reserved. // Copyright(c) 2021 SimonG. All Rights Reserved.
using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using LightweightIocContainer; using LightweightIocContainer;
using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Validation; using LightweightIocContainer.Validation;
@ -37,11 +39,22 @@ namespace Test.LightweightIocContainer
ITest Create(IParameter parameter); ITest Create(IParameter parameter);
} }
[UsedImplicitly]
public interface IInvalidFactory
{
ITest Create();
}
private class TestInstaller : IIocInstaller private class TestInstaller : IIocInstaller
{ {
public void Install(IIocContainer container) => container.Register<ITest, Test>().WithFactory<ITestFactory>(); public void Install(IIocContainer container) => container.Register<ITest, Test>().WithFactory<ITestFactory>();
} }
private class InvalidTestInstaller : IIocInstaller
{
public void Install(IIocContainer container) => container.Register<ITest, Test>().WithFactory<IInvalidFactory>();
}
[Test] [Test]
public void TestValidate() public void TestValidate()
{ {
@ -70,5 +83,22 @@ namespace Test.LightweightIocContainer
parameterMock.Verify(p => p.Method(), Times.Once); 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<AggregateException>(() => validator.Validate());
Exception exception = aggregateException?.InnerExceptions[0];
Assert.IsInstanceOf<NoMatchingConstructorFoundException>(exception);
NoMatchingConstructorFoundException noMatchingConstructorFoundException = (NoMatchingConstructorFoundException) exception;
Assert.AreEqual(typeof(Test), noMatchingConstructorFoundException?.Type);
}
} }
} }
Loading…
Cancel
Save