#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>
<Folder Include="Factories\" />
<Folder Include="Interfaces\Factories\" />
<Folder Include="Validation" />
</ItemGroup>
<ItemGroup>

@ -1515,6 +1515,7 @@
<member name="M:LightweightIocContainer.Validation.IocValidator.Validate">
<summary>
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>
</member>
</members>

@ -17,6 +17,8 @@ namespace LightweightIocContainer.Validation
private readonly IocContainer _iocContainer;
private readonly List<(Type type, object parameter)> _parameters;
private List<Exception> _validationExceptions;
/// <summary>
/// Validator for your <see cref="IocContainer"/> to check if everything can be resolved with your current setup
/// </summary>
@ -37,6 +39,7 @@ namespace LightweightIocContainer.Validation
/// <summary>
/// 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>
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<Exception>();
_validationExceptions.Add(exception);
}
}
}

@ -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<ITest, Test>().WithFactory<ITestFactory>();
}
private class InvalidTestInstaller : IIocInstaller
{
public void Install(IIocContainer container) => container.Register<ITest, Test>().WithFactory<IInvalidFactory>();
}
[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<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