From 7f4ea569293ac0f5601e1cba8fb50932c586be3e Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 3 Dec 2021 18:00:31 +0100 Subject: [PATCH] #44: remove backing field and pass local list --- .../Validation/IocValidator.cs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 7be0935..6c48223 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -17,8 +17,6 @@ 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 /// @@ -43,6 +41,8 @@ namespace LightweightIocContainer.Validation /// public void Validate() { + List validationExceptions = new(); + foreach (var registration in _iocContainer.Registrations) { if (registration is IWithFactory { Factory: { } } withFactoryRegistration) @@ -56,17 +56,17 @@ namespace LightweightIocContainer.Validation .FirstOrDefault(p => parameterType.IsInstanceOfType(p.parameter)) select definedParameter == default ? parameterType.GetDefault() : definedParameter.parameter).ToArray()) .ToList() - .ForEach(p => TryResolve(registration.InterfaceType, p)); + .ForEach(p => TryResolve(registration.InterfaceType, p, validationExceptions)); } else - TryResolve(registration.InterfaceType); + TryResolve(registration.InterfaceType, null, validationExceptions); } - if (_validationExceptions != null) - throw new AggregateException("Validation failed.", _validationExceptions); + if (validationExceptions.Any()) + throw new AggregateException("Validation failed.", validationExceptions); } - private void TryResolve(Type type, object[] arguments = null) + private void TryResolve(Type type, object[] arguments, List validationExceptions) { try { @@ -74,8 +74,7 @@ namespace LightweightIocContainer.Validation } catch (Exception exception) { - _validationExceptions ??= new List(); - _validationExceptions.Add(exception); + validationExceptions.Add(exception); } } }