- refactoring and adapt to new language features

master
Simon G. 1 year ago
parent 93428ee797
commit 45125eb926
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 23
      LightweightIocContainer.Validation/IocValidator.cs
  2. 1
      LightweightIocContainer/Exceptions/CircularDependencyException.cs
  3. 6
      LightweightIocContainer/Exceptions/IocContainerException.cs
  4. 2
      LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs
  5. 5
      LightweightIocContainer/Factories/CustomTypedFactory.cs
  6. 2
      LightweightIocContainer/Installers/AssemblyInstaller.cs
  7. 33
      LightweightIocContainer/IocContainer.cs
  8. 6
      LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs
  9. 32
      LightweightIocContainer/Registrations/MultipleRegistration.cs
  10. 8
      LightweightIocContainer/Registrations/RegistrationCollector.cs
  11. 14
      LightweightIocContainer/ResolvePlaceholders/InternalFactoryMethodPlaceholder.cs
  12. 5
      LightweightIocContainer/ResolvePlaceholders/InternalResolvePlaceholder.cs
  13. 19
      LightweightIocContainer/ResolvePlaceholders/InternalToBeResolvedPlaceholder.cs
  14. 20
      Test.LightweightIocContainer.Validation/IocValidatorTest.cs
  15. 23
      Test.LightweightIocContainer/AssemblyInstallerTest.cs
  16. 34
      Test.LightweightIocContainer/EnumerableExtensionTest.cs
  17. 15
      Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs
  18. 15
      Test.LightweightIocContainer/IgnoreConstructorAttributeTest.cs
  19. 20
      Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs
  20. 17
      Test.LightweightIocContainer/IocContainerParameterRegistrationTest.cs
  21. 30
      Test.LightweightIocContainer/IocContainerRecursionTest.cs
  22. 32
      Test.LightweightIocContainer/IocContainerTest.cs
  23. 11
      Test.LightweightIocContainer/MultipleMultitonRegistrationTest.cs
  24. 1
      Test.LightweightIocContainer/OnCreateTest.cs
  25. 20
      Test.LightweightIocContainer/OpenGenericRegistrationTest.cs
  26. 26
      Test.LightweightIocContainer/RegistrationBaseTest.cs
  27. 10
      Test.LightweightIocContainer/SingleTypeRegistrationTest.cs

@ -11,20 +11,9 @@ namespace LightweightIocContainer.Validation;
/// <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>
public class IocValidator public class IocValidator(IocContainer iocContainer)
{ {
private readonly IocContainer _iocContainer; private readonly List<(Type type, object? parameter)> _parameters = [];
private readonly List<(Type type, object? parameter)> _parameters;
/// <summary>
/// Validator for your <see cref="IocContainer"/> to check if everything can be resolved with your current setup
/// </summary>
/// <param name="iocContainer">The <see cref="IocContainer"/></param>
public IocValidator(IocContainer iocContainer)
{
_iocContainer = iocContainer;
_parameters = new List<(Type, object?)>();
}
/// <summary> /// <summary>
/// Add parameters that can't be default for your type to be created successfully /// Add parameters that can't be default for your type to be created successfully
@ -40,13 +29,13 @@ public class IocValidator
/// </summary> /// </summary>
public void Validate() public void Validate()
{ {
List<Exception> validationExceptions = new(); List<Exception> validationExceptions = [];
foreach (IRegistration registration in _iocContainer.Registrations) foreach (IRegistration registration in iocContainer.Registrations)
{ {
var definedParameters = _parameters.Where(p => p.type == registration.InterfaceType); var definedParameters = _parameters.Where(p => p.type == registration.InterfaceType);
if (registration is IWithFactoryInternal { Factory: { } } withFactoryRegistration) if (registration is IWithFactoryInternal { Factory: not null } withFactoryRegistration)
{ {
(from createMethod in withFactoryRegistration.Factory.CreateMethods.Where(m => m.ReturnType == registration.InterfaceType) (from createMethod in withFactoryRegistration.Factory.CreateMethods.Where(m => m.ReturnType == registration.InterfaceType)
select createMethod.GetParameters().Select(p => p.ParameterType) select createMethod.GetParameters().Select(p => p.ParameterType)
@ -71,7 +60,7 @@ public class IocValidator
private void TryResolve(Type type, object?[]? arguments, List<Exception> validationExceptions, bool isFactoryResolve = false) private void TryResolve(Type type, object?[]? arguments, List<Exception> validationExceptions, bool isFactoryResolve = false)
{ {
(bool success, object _, Exception? exception) = _iocContainer.TryResolveNonGeneric(type, arguments, null, isFactoryResolve); (bool success, object _, Exception? exception) = iocContainer.TryResolveNonGeneric(type, arguments, null, isFactoryResolve);
if (success) if (success)
return; return;

@ -22,7 +22,6 @@ internal class CircularDependencyException : IocContainerException
ResolveStack = resolveStack; ResolveStack = resolveStack;
} }
/// <summary> /// <summary>
/// The currently resolving <see cref="Type"/> /// The currently resolving <see cref="Type"/>
/// </summary> /// </summary>

@ -12,7 +12,7 @@ public abstract class IocContainerException : Exception
/// <summary> /// <summary>
/// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/> /// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/>
/// </summary> /// </summary>
protected IocContainerException() => InnerExceptions = new List<Exception>(); protected IocContainerException() => InnerExceptions = [];
/// <summary> /// <summary>
/// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/> /// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/>
@ -20,7 +20,7 @@ public abstract class IocContainerException : Exception
/// <param name="message">The message of the <see cref="Exception"/></param> /// <param name="message">The message of the <see cref="Exception"/></param>
protected IocContainerException(string message) protected IocContainerException(string message)
: base(message) => : base(message) =>
InnerExceptions = new List<Exception>(); InnerExceptions = [];
/// <summary> /// <summary>
/// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/> /// A base <see cref="Exception"/> for the <see cref="LightweightIocContainer"/>
@ -29,7 +29,7 @@ public abstract class IocContainerException : Exception
/// <param name="innerException">The inner <see cref="Exception"/></param> /// <param name="innerException">The inner <see cref="Exception"/></param>
protected IocContainerException(string message, Exception innerException) protected IocContainerException(string message, Exception innerException)
: base(message, innerException) => : base(message, innerException) =>
InnerExceptions = new List<Exception> {innerException}; InnerExceptions = [innerException];
/// <summary> /// <summary>
/// The inner exceptions of the <see cref="IocContainerException"/> /// The inner exceptions of the <see cref="IocContainerException"/>

@ -17,7 +17,7 @@ internal class NoMatchingConstructorFoundException : IocContainerException
: base($"No matching constructor for {type} found.") : base($"No matching constructor for {type} found.")
{ {
Type = type; Type = type;
InnerExceptions = new List<Exception>(); InnerExceptions = [];
} }

@ -9,7 +9,4 @@ namespace LightweightIocContainer.Factories;
/// <summary> /// <summary>
/// <see cref="ITypedFactory"/> implementation for custom implemented factories /// <see cref="ITypedFactory"/> implementation for custom implemented factories
/// </summary> /// </summary>
public class CustomTypedFactory<TFactory> : TypedFactoryBase<TFactory> public class CustomTypedFactory<TFactory> : TypedFactoryBase<TFactory>;
{
}

@ -20,7 +20,7 @@ public class AssemblyInstaller : IAssemblyInstaller
/// <param name="assembly">The <see cref="Assembly"/> from where the <see cref="IIocInstaller"/>s will be installed</param> /// <param name="assembly">The <see cref="Assembly"/> from where the <see cref="IIocInstaller"/>s will be installed</param>
public AssemblyInstaller(Assembly assembly) public AssemblyInstaller(Assembly assembly)
{ {
Installers = new List<IIocInstaller>(); Installers = [];
Type[] types = assembly.GetTypes(); Type[] types = assembly.GetTypes();
foreach (Type type in types) foreach (Type type in types)

@ -21,23 +21,18 @@ public class IocContainer : IIocContainer, IIocResolver
{ {
private readonly RegistrationFactory _registrationFactory; private readonly RegistrationFactory _registrationFactory;
private readonly List<(Type type, object? instance)> _singletons = new(); private readonly List<(Type type, object? instance)> _singletons = [];
private readonly List<(Type type, Type scope, Dictionary<object, object?> instances)> _multitons = new(); private readonly List<(Type type, Type scope, Dictionary<object, object?> instances)> _multitons = [];
private readonly List<Type> _ignoreConstructorAttributes; private readonly List<Type> _ignoreConstructorAttributes = [];
/// <summary> /// <summary>
/// The main container that carries all the <see cref="IRegistration"/>s and can resolve all the types you'll ever want /// The main container that carries all the <see cref="IRegistration"/>s and can resolve all the types you'll ever want
/// </summary> /// </summary>
public IocContainer() public IocContainer() => _registrationFactory = new RegistrationFactory(this);
{
_registrationFactory = new RegistrationFactory(this); internal List<IRegistration> Registrations { get; } = [];
_ignoreConstructorAttributes = new List<Type>();
Registrations = new List<IRegistration>();
}
internal List<IRegistration> Registrations { get; }
/// <summary> /// <summary>
/// Install the given installers for the current <see cref="IocContainer"/> /// Install the given installers for the current <see cref="IocContainer"/>
/// </summary> /// </summary>
@ -195,7 +190,7 @@ public class IocContainer : IIocContainer, IIocResolver
if (registration == null) if (registration == null)
return (false, new object(), new TypeNotRegisteredException(typeof(T))); return (false, new object(), new TypeNotRegisteredException(typeof(T)));
List<Type> internalResolveStack = resolveStack == null ? new List<Type>() : new List<Type>(resolveStack); List<Type> internalResolveStack = resolveStack == null ? [] : [..resolveStack];
(bool success, internalResolveStack, CircularDependencyException? circularDependencyException) = CheckForCircularDependencies<T>(internalResolveStack); (bool success, internalResolveStack, CircularDependencyException? circularDependencyException) = CheckForCircularDependencies<T>(internalResolveStack);
if (!success && circularDependencyException is not null) if (!success && circularDependencyException is not null)
@ -232,7 +227,7 @@ public class IocContainer : IIocContainer, IIocResolver
object multitonScopeArgument = TryGetMultitonScopeArgument(multitonRegistration, arguments); object multitonScopeArgument = TryGetMultitonScopeArgument(multitonRegistration, arguments);
parametersToResolve ??= new List<object?>(); parametersToResolve ??= [];
parametersToResolve.Insert(0, multitonScopeArgument); //insert scope at first place, won't be passed to ctor when creating multiton parametersToResolve.Insert(0, multitonScopeArgument); //insert scope at first place, won't be passed to ctor when creating multiton
} }
@ -267,7 +262,7 @@ public class IocContainer : IIocContainer, IIocResolver
if (genericMethod == null) if (genericMethod == null)
throw new GenericMethodNotFoundException(nameof(TryResolve)); throw new GenericMethodNotFoundException(nameof(TryResolve));
object? resolvedValue = genericMethod.Invoke(this, new object?[]{arguments, resolveStack, isFactoryResolve}); object? resolvedValue = genericMethod.Invoke(this, [arguments, resolveStack, isFactoryResolve]);
if (resolvedValue is not ValueTuple<bool, object, Exception?> resolvedTuple) if (resolvedValue is not ValueTuple<bool, object, Exception?> resolvedTuple)
throw new Exception("Invalid return value!"); throw new Exception("Invalid return value!");
@ -600,12 +595,12 @@ public class IocContainer : IIocContainer, IIocResolver
{ {
List<ParameterInfo> constructorParameters = constructor.GetParameters().ToList(); List<ParameterInfo> constructorParameters = constructor.GetParameters().ToList();
List<ConstructorNotMatchingException> exceptions = new(); List<ConstructorNotMatchingException> exceptions = [];
List<object?> parameters = new(); List<object?> parameters = [];
List<object?>? passedArguments = null; List<object?>? passedArguments = null;
if (arguments != null) if (arguments != null)
passedArguments = new List<object?>(arguments); passedArguments = [..arguments];
foreach (ParameterInfo parameter in constructorParameters) foreach (ParameterInfo parameter in constructorParameters)
{ {
@ -745,9 +740,9 @@ public class IocContainer : IIocContainer, IIocResolver
private (bool success, List<Type> resolveStack, CircularDependencyException? exception) CheckForCircularDependencies<T>(List<Type>? resolveStack) private (bool success, List<Type> resolveStack, CircularDependencyException? exception) CheckForCircularDependencies<T>(List<Type>? resolveStack)
{ {
if (resolveStack == null) //first resolve call if (resolveStack == null) //first resolve call
resolveStack = new List<Type> {typeof(T)}; //create new stack and add the currently resolving type to the stack resolveStack = [typeof(T)]; //create new stack and add the currently resolving type to the stack
else if (resolveStack.Contains(typeof(T))) else if (resolveStack.Contains(typeof(T)))
return (false, new List<Type>(), new CircularDependencyException(typeof(T), resolveStack)); //currently resolving type is still resolving -> circular dependency return (false, [], new CircularDependencyException(typeof(T), resolveStack)); //currently resolving type is still resolving -> circular dependency
else //not the first resolve call in chain but no circular dependencies for now else //not the first resolve call in chain but no circular dependencies for now
resolveStack.Add(typeof(T)); //add currently resolving type to the stack resolveStack.Add(typeof(T)); //add currently resolving type to the stack

@ -26,11 +26,11 @@ internal class MultipleMultitonRegistration<TInterface1, TInterface2, TImplement
public MultipleMultitonRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Type scope, IocContainer container) public MultipleMultitonRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Type scope, IocContainer container)
: base(interfaceType1, implementationType, scope, container) : base(interfaceType1, implementationType, scope, container)
{ {
Registrations = new List<IRegistration> Registrations =
{ [
new MultitonRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, scope, container), new MultitonRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, scope, container),
new MultitonRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, scope, container) new MultitonRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, scope, container)
}; ];
} }
/// <summary> /// <summary>

@ -22,13 +22,15 @@ internal abstract class MultipleRegistration<TInterface1, TImplementation> : Typ
/// <param name="lifestyle">The <see cref="Lifestyle"/> of this <see cref="MultipleRegistration{TInterface1,TInterface2}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> of this <see cref="MultipleRegistration{TInterface1,TInterface2}"/></param>
/// <param name="container">The current instance of the <see cref="IIocContainer"/></param> /// <param name="container">The current instance of the <see cref="IIocContainer"/></param>
protected MultipleRegistration(Type interfaceType1, Type implementationType, Lifestyle lifestyle, IocContainer container) protected MultipleRegistration(Type interfaceType1, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) => : base(interfaceType1, implementationType, lifestyle, container)
Registrations = new List<IRegistration>(); {
}
/// <summary> /// <summary>
/// A <see cref="List{T}"/> of <see cref="IRegistration"/>s that are registered within this <see cref="MultipleRegistration{TInterface1,TInterface2}"/> /// A <see cref="List{T}"/> of <see cref="IRegistration"/>s that are registered within this <see cref="MultipleRegistration{TInterface1,TInterface2}"/>
/// </summary> /// </summary>
public List<IRegistration> Registrations { get; protected init; } public List<IRegistration> Registrations { get; protected init; } = [];
} }
/// <summary> /// <summary>
@ -50,11 +52,11 @@ internal class MultipleRegistration<TInterface1, TInterface2, TImplementation> :
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Lifestyle lifestyle, IocContainer container) public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) : base(interfaceType1, implementationType, lifestyle, container)
{ {
Registrations = new List<IRegistration> Registrations =
{ [
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container), new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container) new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container)
}; ];
} }
/// <summary> /// <summary>
@ -97,12 +99,12 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TImpl
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type implementationType, Lifestyle lifestyle, IocContainer container) public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) : base(interfaceType1, implementationType, lifestyle, container)
{ {
Registrations = new List<IRegistration> Registrations =
{ [
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container), new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container), new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container),
new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container) new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container)
}; ];
} }
/// <summary> /// <summary>
@ -149,13 +151,13 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TInte
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type interfaceType4, Type implementationType, Lifestyle lifestyle, IocContainer container) public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type interfaceType4, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) : base(interfaceType1, implementationType, lifestyle, container)
{ {
Registrations = new List<IRegistration> Registrations =
{ [
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container), new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container), new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container),
new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container), new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container),
new TypedRegistration<TInterface4, TImplementation>(interfaceType4, implementationType, lifestyle, container) new TypedRegistration<TInterface4, TImplementation>(interfaceType4, implementationType, lifestyle, container)
}; ];
} }
/// <summary> /// <summary>
@ -206,14 +208,14 @@ internal class MultipleRegistration<TInterface1, TInterface2, TInterface3, TInte
public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type interfaceType4, Type interfaceType5, Type implementationType, Lifestyle lifestyle, IocContainer container) public MultipleRegistration(Type interfaceType1, Type interfaceType2, Type interfaceType3, Type interfaceType4, Type interfaceType5, Type implementationType, Lifestyle lifestyle, IocContainer container)
: base(interfaceType1, implementationType, lifestyle, container) : base(interfaceType1, implementationType, lifestyle, container)
{ {
Registrations = new List<IRegistration> Registrations =
{ [
new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container), new TypedRegistration<TInterface1, TImplementation>(interfaceType1, implementationType, lifestyle, container),
new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container), new TypedRegistration<TInterface2, TImplementation>(interfaceType2, implementationType, lifestyle, container),
new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container), new TypedRegistration<TInterface3, TImplementation>(interfaceType3, implementationType, lifestyle, container),
new TypedRegistration<TInterface4, TImplementation>(interfaceType4, implementationType, lifestyle, container), new TypedRegistration<TInterface4, TImplementation>(interfaceType4, implementationType, lifestyle, container),
new TypedRegistration<TInterface5, TImplementation>(interfaceType5, implementationType, lifestyle, container) new TypedRegistration<TInterface5, TImplementation>(interfaceType5, implementationType, lifestyle, container)
}; ];
} }
/// <summary> /// <summary>

@ -14,16 +14,12 @@ public class RegistrationCollector : IRegistrationCollector
{ {
private readonly RegistrationFactory _registrationFactory; private readonly RegistrationFactory _registrationFactory;
internal RegistrationCollector(RegistrationFactory registrationFactory) internal RegistrationCollector(RegistrationFactory registrationFactory) => _registrationFactory = registrationFactory;
{
_registrationFactory = registrationFactory;
Registrations = new List<IRegistration>();
}
/// <summary> /// <summary>
/// The collected <see cref="IRegistration"/>s /// The collected <see cref="IRegistration"/>s
/// </summary> /// </summary>
internal List<IRegistration> Registrations { get; } internal List<IRegistration> Registrations { get; } = [];
/// <summary> /// <summary>
/// Add an Interface with a Type that implements it /// Add an Interface with a Type that implements it

@ -9,21 +9,15 @@ namespace LightweightIocContainer.ResolvePlaceholders;
/// <summary> /// <summary>
/// An internal placeholder that is used to hold factory methods for types that need to be resolved during the resolve process /// An internal placeholder that is used to hold factory methods for types that need to be resolved during the resolve process
/// </summary> /// </summary>
internal class InternalFactoryMethodPlaceholder<T> : IInternalToBeResolvedPlaceholder internal class InternalFactoryMethodPlaceholder<T>(ISingleTypeRegistration<T> singleTypeRegistration) : IInternalToBeResolvedPlaceholder
{ {
public InternalFactoryMethodPlaceholder(ISingleTypeRegistration<T> singleTypeRegistration)
{
ResolvedType = singleTypeRegistration.InterfaceType;
SingleTypeRegistration = singleTypeRegistration;
}
/// <summary> /// <summary>
/// The <see cref="Type"/> to be resolved /// The <see cref="Type"/> to be resolved
/// </summary> /// </summary>
public Type ResolvedType { get; } public Type ResolvedType { get; } = singleTypeRegistration.InterfaceType;
/// <summary> /// <summary>
/// The <see cref="ISingleTypeRegistration{T}"/> /// The <see cref="ISingleTypeRegistration{T}"/>
/// </summary> /// </summary>
public ISingleTypeRegistration<T> SingleTypeRegistration { get; } public ISingleTypeRegistration<T> SingleTypeRegistration { get; } = singleTypeRegistration;
} }

@ -7,7 +7,4 @@ namespace LightweightIocContainer.ResolvePlaceholders;
/// <summary> /// <summary>
/// An internal placeholder that is used during the resolving process /// An internal placeholder that is used during the resolving process
/// </summary> /// </summary>
internal class InternalResolvePlaceholder internal class InternalResolvePlaceholder;
{
}

@ -9,27 +9,20 @@ namespace LightweightIocContainer.ResolvePlaceholders;
/// <summary> /// <summary>
/// An internal placeholder that is used to hold types that need to be resolved during the resolving process /// An internal placeholder that is used to hold types that need to be resolved during the resolving process
/// </summary> /// </summary>
internal class InternalToBeResolvedPlaceholder : IInternalToBeResolvedPlaceholder internal class InternalToBeResolvedPlaceholder(Type resolvedType, IRegistration resolvedRegistration, List<object?>? parameters) : IInternalToBeResolvedPlaceholder
{ {
public InternalToBeResolvedPlaceholder(Type resolvedType, IRegistration resolvedRegistration, List<object?>? parameters)
{
ResolvedType = resolvedType;
ResolvedRegistration = resolvedRegistration;
Parameters = parameters;
}
/// <summary> /// <summary>
/// The <see cref="Type"/> to be resolved /// The <see cref="Type"/> to be resolved
/// </summary> /// </summary>
public Type ResolvedType { get; } public Type ResolvedType { get; } = resolvedType;
/// <summary> /// <summary>
/// The <see cref="IRegistration"/> to be resolved /// The <see cref="IRegistration"/> to be resolved
/// </summary> /// </summary>
public IRegistration ResolvedRegistration { get; } public IRegistration ResolvedRegistration { get; } = resolvedRegistration;
/// <summary> /// <summary>
/// The parameters needed to resolve the <see cref="ResolvedRegistration"/> /// The parameters needed to resolve the <see cref="ResolvedRegistration"/>
/// </summary> /// </summary>
public List<object?>? Parameters { get; } public List<object?>? Parameters { get; } = parameters;
} }

@ -16,10 +16,7 @@ namespace Test.LightweightIocContainer.Validation;
[TestFixture] [TestFixture]
public class IocValidatorTest public class IocValidatorTest
{ {
public interface ITest public interface ITest;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IParameter public interface IParameter
@ -33,22 +30,13 @@ public class IocValidatorTest
} }
[UsedImplicitly] [UsedImplicitly]
public interface IConstraint public interface IConstraint;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IGenericTest<T> where T : IConstraint, new() public interface IGenericTest<T> where T : IConstraint, new();
{
}
[UsedImplicitly] [UsedImplicitly]
public class GenericTest<T> : IGenericTest<T> where T : IConstraint, new() public class GenericTest<T> : IGenericTest<T> where T : IConstraint, new();
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IGenericTestFactory public interface IGenericTestFactory

@ -6,7 +6,6 @@ using System.Reflection;
using JetBrains.Annotations; using JetBrains.Annotations;
using LightweightIocContainer; using LightweightIocContainer;
using LightweightIocContainer.Installers; using LightweightIocContainer.Installers;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Installers; using LightweightIocContainer.Interfaces.Installers;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
using NSubstitute; using NSubstitute;
@ -24,20 +23,16 @@ public class AssemblyInstallerTest
} }
[UsedImplicitly] [UsedImplicitly]
public class AssemblyWrapper : Assembly public class AssemblyWrapper : Assembly;
{
}
[Test] [Test]
public void TestInstall() public void TestInstall()
{ {
List<Type> types = new() List<Type> types =
{ [
typeof(object), typeof(object),
typeof(TestInstaller) typeof(TestInstaller)
}; ];
AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>(); AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>();
assemblyMock.GetTypes().Returns(types.ToArray()); assemblyMock.GetTypes().Returns(types.ToArray());
@ -53,23 +48,23 @@ public class AssemblyInstallerTest
[Test] [Test]
public void TestFromAssemblyThis() public void TestFromAssemblyThis()
{ {
IIocContainer iocContainer = new IocContainer(); IocContainer iocContainer = new();
iocContainer.Install(FromAssembly.This()); iocContainer.Install(FromAssembly.This());
} }
[Test] [Test]
public void TestFromAssemblyInstance() public void TestFromAssemblyInstance()
{ {
List<Type> types = new() List<Type> types =
{ [
typeof(object), typeof(object),
typeof(TestInstaller) typeof(TestInstaller)
}; ];
AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>(); AssemblyWrapper assemblyMock = Substitute.For<AssemblyWrapper>();
assemblyMock.GetTypes().Returns(types.ToArray()); assemblyMock.GetTypes().Returns(types.ToArray());
IIocContainer iocContainer = new IocContainer(); IocContainer iocContainer = new();
iocContainer.Install(FromAssembly.Instance(assemblyMock)); iocContainer.Install(FromAssembly.Instance(assemblyMock));
} }
} }

@ -34,20 +34,20 @@ public class EnumerableExtensionTest
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public void TestFirstOrGivenNoPredicateEmpty() public void TestFirstOrGivenNoPredicateEmpty()
{ {
List<ListObject> list = new(); List<ListObject> list = [];
Assert.That(list.FirstOrGiven<ListObject, Given>(), Is.InstanceOf<Given>()); Assert.That(list.FirstOrGiven<ListObject, Given>(), Is.InstanceOf<Given>());
} }
[Test] [Test]
public void TestFirstOrGivenNoPredicate() public void TestFirstOrGivenNoPredicate()
{ {
List<ListObject> list = new() List<ListObject> list =
{ [
new ListObject {Index = 0}, new() { Index = 0 },
new ListObject {Index = 1}, new() { Index = 1 },
new ListObject {Index = 2}, new() { Index = 2 },
new ListObject {Index = 3} new() { Index = 3 }
}; ];
ListObject listObject = list.FirstOrGiven<ListObject, Given>(); ListObject listObject = list.FirstOrGiven<ListObject, Given>();
@ -59,20 +59,20 @@ public class EnumerableExtensionTest
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public void TestFirstOrGivenPredicateEmpty() public void TestFirstOrGivenPredicateEmpty()
{ {
List<ListObject> list = new(); List<ListObject> list = [];
Assert.That(list.FirstOrGiven<ListObject, Given>(o => o.Index == 2), Is.InstanceOf<Given>()); Assert.That(list.FirstOrGiven<ListObject, Given>(o => o.Index == 2), Is.InstanceOf<Given>());
} }
[Test] [Test]
public void TestFirstOrGivenPredicate() public void TestFirstOrGivenPredicate()
{ {
List<ListObject> list = new() List<ListObject> list =
{ [
new ListObject {Index = 0}, new() { Index = 0 },
new ListObject {Index = 1}, new() { Index = 1 },
new ListObject {Index = 2}, new() { Index = 2 },
new ListObject {Index = 3} new() { Index = 3 }
}; ];
ListObject listObject = list.FirstOrGiven<ListObject, Given>(o => o.Index == 2); ListObject listObject = list.FirstOrGiven<ListObject, Given>(o => o.Index == 2);
@ -88,7 +88,7 @@ public class EnumerableExtensionTest
ITest test3 = Substitute.For<ITest>(); ITest test3 = Substitute.For<ITest>();
ITest test4 = Substitute.For<ITest>(); ITest test4 = Substitute.For<ITest>();
IEnumerable<ITest> enumerable = new[] { test1, test2, test3, test4 }; IEnumerable<ITest> enumerable = [test1, test2, test3, test4];
enumerable.ForEach(t => t.DoSomething()); enumerable.ForEach(t => t.DoSomething());

@ -12,15 +12,9 @@ namespace Test.LightweightIocContainer;
[TestFixture] [TestFixture]
public class FluentFactoryRegistrationTest public class FluentFactoryRegistrationTest
{ {
public interface ITest public interface ITest;
{
}
private class Test : ITest private class Test : ITest;
{
}
private class TestByte : ITest private class TestByte : ITest
{ {
@ -130,10 +124,7 @@ public class FluentFactoryRegistrationTest
public MultitonScope Create(); public MultitonScope Create();
} }
public class MultitonScope public class MultitonScope;
{
}
private IocContainer _iocContainer; private IocContainer _iocContainer;

@ -12,22 +12,13 @@ namespace Test.LightweightIocContainer;
public class IgnoreConstructorAttributeTest public class IgnoreConstructorAttributeTest
{ {
[AttributeUsage(AttributeTargets.Constructor)] [AttributeUsage(AttributeTargets.Constructor)]
private class IgnoreAttribute : Attribute private class IgnoreAttribute : Attribute;
{
}
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
private class InvalidIgnoreAttribute : Attribute private class InvalidIgnoreAttribute : Attribute;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface ITest public interface ITest;
{
}
[UsedImplicitly] [UsedImplicitly]
private class TestIgnoreCtor : ITest private class TestIgnoreCtor : ITest

@ -11,29 +11,17 @@ namespace Test.LightweightIocContainer;
[TestFixture] [TestFixture]
public class IocContainerInterfaceSegregationTest public class IocContainerInterfaceSegregationTest
{ {
private interface IBar private interface IBar;
{
}
private interface IFoo private interface IFoo
{ {
void ThrowFoo(); void ThrowFoo();
} }
private interface IAnotherBar private interface IAnotherBar;
{
}
private interface IAnotherFoo private interface IAnotherFoo;
{
}
private interface IAnotherOne private interface IAnotherOne;
{
}
[UsedImplicitly] [UsedImplicitly]
private class Foo : IFoo, IBar, IAnotherFoo, IAnotherBar, IAnotherOne private class Foo : IFoo, IBar, IAnotherFoo, IAnotherBar, IAnotherOne

@ -20,16 +20,10 @@ public class IocContainerParameterRegistrationTest
} }
[UsedImplicitly] [UsedImplicitly]
public interface IB public interface IB;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IC public interface IC;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface ID public interface ID
@ -64,10 +58,7 @@ public class IocContainerParameterRegistrationTest
} }
[UsedImplicitly] [UsedImplicitly]
private class C : IC private class C : IC;
{
}
[UsedImplicitly] [UsedImplicitly]
private class D : ID private class D : ID
@ -87,7 +78,6 @@ public class IocContainerParameterRegistrationTest
public IB B { get; } public IB B { get; }
public IC C { get; } public IC C { get; }
} }
private IocContainer _iocContainer; private IocContainer _iocContainer;
@ -97,7 +87,6 @@ public class IocContainerParameterRegistrationTest
[TearDown] [TearDown]
public void TearDown() => _iocContainer.Dispose(); public void TearDown() => _iocContainer.Dispose();
[Test] [Test]
public void TestResolveOnlyRegistrationParameters() public void TestResolveOnlyRegistrationParameters()
{ {

@ -14,16 +14,10 @@ namespace Test.LightweightIocContainer;
public class IocContainerRecursionTest public class IocContainerRecursionTest
{ {
[UsedImplicitly] [UsedImplicitly]
public interface IFoo public interface IFoo;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IBar public interface IBar;
{
}
[UsedImplicitly] [UsedImplicitly]
private class Foo : IFoo private class Foo : IFoo
@ -42,22 +36,13 @@ public class IocContainerRecursionTest
} }
[UsedImplicitly] [UsedImplicitly]
public interface IA public interface IA;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IB public interface IB;
{
}
[UsedImplicitly] [UsedImplicitly]
public interface IC public interface IC;
{
}
[UsedImplicitly] [UsedImplicitly]
private class A : IA private class A : IA
@ -78,10 +63,7 @@ public class IocContainerRecursionTest
} }
[UsedImplicitly] [UsedImplicitly]
private class C : IC private class C : IC;
{
}
[UsedImplicitly] [UsedImplicitly]
private class ATwoCtor : IA private class ATwoCtor : IA

@ -12,20 +12,11 @@ namespace Test.LightweightIocContainer;
[TestFixture] [TestFixture]
public class IocContainerTest public class IocContainerTest
{ {
private interface ITest private interface ITest;
{
}
private interface IFoo
{
} private interface IFoo;
private class Test : ITest
{
} private class Test : ITest;
private class TestMultiton : ITest private class TestMultiton : ITest
{ {
@ -35,10 +26,7 @@ public class IocContainerTest
} }
} }
private class TestMultitonIntScope(int scope) : ITest private class TestMultitonIntScope(int scope) : ITest;
{
}
[UsedImplicitly] [UsedImplicitly]
private class TestConstructor : ITest private class TestConstructor : ITest
@ -92,10 +80,7 @@ public class IocContainerTest
} }
[UsedImplicitly] [UsedImplicitly]
private class Foo : IFoo private class Foo : IFoo;
{
}
[UsedImplicitly] [UsedImplicitly]
private class FooConstructor : IFoo private class FooConstructor : IFoo
@ -106,11 +91,7 @@ public class IocContainerTest
} }
} }
private class MultitonScope private class MultitonScope;
{
}
private IocContainer _iocContainer; private IocContainer _iocContainer;
@ -120,7 +101,6 @@ public class IocContainerTest
[TearDown] [TearDown]
public void TearDown() => _iocContainer.Dispose(); public void TearDown() => _iocContainer.Dispose();
[Test] [Test]
public void TestInstall() public void TestInstall()
{ {

@ -14,10 +14,7 @@ public class MultipleMultitonRegistrationTest
private IocContainer _iocContainer; private IocContainer _iocContainer;
[UsedImplicitly] [UsedImplicitly]
public interface ITest : IProvider public interface ITest : IProvider;
{
}
public interface IProvider public interface IProvider
{ {
@ -38,11 +35,7 @@ public class MultipleMultitonRegistrationTest
public void DoSomething(int number) => Number = number; public void DoSomething(int number) => Number = number;
} }
private class MultitonScope private class MultitonScope;
{
}
[SetUp] [SetUp]
public void SetUp() => _iocContainer = new IocContainer(); public void SetUp() => _iocContainer = new IocContainer();

@ -23,7 +23,6 @@ public class OnCreateTest
public void DoSomething() => throw new Exception(); public void DoSomething() => throw new Exception();
public Task InitializeAsync() => throw new Exception(); public Task InitializeAsync() => throw new Exception();
} }
[Test] [Test]
public void TestOnCreate() public void TestOnCreate()

@ -16,29 +16,17 @@ public class OpenGenericRegistrationTest
private IocContainer _iocContainer; private IocContainer _iocContainer;
[UsedImplicitly] [UsedImplicitly]
public interface IConstraint public interface IConstraint;
{
}
[UsedImplicitly] [UsedImplicitly]
public class Constraint : IConstraint public class Constraint : IConstraint;
{
}
[UsedImplicitly] [UsedImplicitly]
[SuppressMessage("ReSharper", "UnusedTypeParameter")] [SuppressMessage("ReSharper", "UnusedTypeParameter")]
public interface ITest<T> where T : IConstraint, new() public interface ITest<T> where T : IConstraint, new();
{
}
[UsedImplicitly] [UsedImplicitly]
public class Test<T> : ITest<T> where T : IConstraint, new() public class Test<T> : ITest<T> where T : IConstraint, new();
{
}
[UsedImplicitly] [UsedImplicitly]
public class CtorTest<T> : ITest<T> where T : IConstraint, new() public class CtorTest<T> : ITest<T> where T : IConstraint, new()

@ -15,25 +15,13 @@ namespace Test.LightweightIocContainer;
[TestFixture] [TestFixture]
public class RegistrationBaseTest public class RegistrationBaseTest
{ {
private interface ITest private interface ITest;
{
}
private interface IFoo
{
}
private interface IBar
{
} private interface IFoo;
private class Test : ITest private interface IBar;
{
} private class Test : ITest;
[UsedImplicitly] [UsedImplicitly]
private class Foo : IFoo private class Foo : IFoo
@ -44,11 +32,7 @@ public class RegistrationBaseTest
} }
} }
private class Bar : IBar private class Bar : IBar;
{
}
[Test] [Test]
public void TestWithParameters() public void TestWithParameters()

@ -27,10 +27,7 @@ public class SingleTypeRegistrationTest
} }
[UsedImplicitly] [UsedImplicitly]
public interface IBar public interface IBar;
{
}
[UsedImplicitly] [UsedImplicitly]
private class Foo : IFoo private class Foo : IFoo
@ -41,10 +38,7 @@ public class SingleTypeRegistrationTest
} }
[UsedImplicitly] [UsedImplicitly]
private class Bar : IBar private class Bar : IBar;
{
}
[Test] [Test]
public void TestSingleTypeRegistrationWithFactoryMethod() public void TestSingleTypeRegistrationWithFactoryMethod()

Loading…
Cancel
Save