|
|
|
@ -17,8 +17,6 @@ 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> |
|
|
|
@ -43,6 +41,8 @@ namespace LightweightIocContainer.Validation |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public void Validate() |
|
|
|
public void Validate() |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
List<Exception> validationExceptions = new(); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var registration in _iocContainer.Registrations) |
|
|
|
foreach (var registration in _iocContainer.Registrations) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (registration is IWithFactory { Factory: { } } withFactoryRegistration) |
|
|
|
if (registration is IWithFactory { Factory: { } } withFactoryRegistration) |
|
|
|
@ -56,17 +56,17 @@ 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 => TryResolve(registration.InterfaceType, p)); |
|
|
|
.ForEach(p => TryResolve(registration.InterfaceType, p, validationExceptions)); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
TryResolve(registration.InterfaceType); |
|
|
|
TryResolve(registration.InterfaceType, null, validationExceptions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (_validationExceptions != null) |
|
|
|
if (validationExceptions.Any()) |
|
|
|
throw new AggregateException("Validation failed.", _validationExceptions); |
|
|
|
throw new AggregateException("Validation failed.", validationExceptions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void TryResolve(Type type, object[] arguments = null) |
|
|
|
private void TryResolve(Type type, object[] arguments, List<Exception> validationExceptions) |
|
|
|
{ |
|
|
|
{ |
|
|
|
try |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
@ -74,8 +74,7 @@ namespace LightweightIocContainer.Validation |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception exception) |
|
|
|
catch (Exception exception) |
|
|
|
{ |
|
|
|
{ |
|
|
|
_validationExceptions ??= new List<Exception>(); |
|
|
|
validationExceptions.Add(exception); |
|
|
|
_validationExceptions.Add(exception); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|