#46: start code cleanup

pull/53/head
Simon G 5 years ago
parent 6590efb388
commit cfa1eb816c
  1. 4
      LightweightIocContainer/Exceptions/ConstructorNotMatchingException.cs
  2. 4
      LightweightIocContainer/Exceptions/IllegalAbstractMethodCreationException.cs
  3. 9
      LightweightIocContainer/Exceptions/IocContainerException.cs
  4. 4
      LightweightIocContainer/Exceptions/MultipleRegistrationException.cs
  5. 4
      LightweightIocContainer/Exceptions/MultitonResolveException.cs
  6. 11
      LightweightIocContainer/Exceptions/NoMatchingConstructorFoundException.cs
  7. 4
      LightweightIocContainer/Exceptions/NoPublicConstructorFoundException.cs
  8. 4
      LightweightIocContainer/Exceptions/TypeNotRegisteredException.cs
  9. 5
      LightweightIocContainer/Installers/FromAssembly.cs
  10. 26
      LightweightIocContainer/IocContainer.cs
  11. 4
      LightweightIocContainer/Registrations/DefaultRegistration.cs
  12. 4
      LightweightIocContainer/Registrations/MultitonRegistration.cs
  13. 69
      LightweightIocContainer/Registrations/RegistrationFactory.cs
  14. 4
      LightweightIocContainer/Registrations/SingleTypeRegistration.cs
  15. 4
      LightweightIocContainer/Registrations/TypedRegistrationBase.cs
  16. 8
      Test.LightweightIocContainer/ActionExtensionTest.cs
  17. 8
      Test.LightweightIocContainer/AssemblyInstallerTest.cs
  18. 4
      Test.LightweightIocContainer/EnumerableExtensionTest.cs
  19. 24
      Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs
  20. 15
      Test.LightweightIocContainer/IocContainerParameterRegistrationTest.cs
  21. 21
      Test.LightweightIocContainer/IocContainerRecursionTest.cs
  22. 56
      Test.LightweightIocContainer/IocContainerTest.cs
  23. 8
      Test.LightweightIocContainer/OnCreateTest.cs
  24. 8
      Test.LightweightIocContainer/OpenGenericRegistrationTest.cs
  25. 4
      Test.LightweightIocContainer/RegistrationBaseTest.cs
  26. 5
      Test.LightweightIocContainer/SingleTypeRegistrationTest.cs

@ -18,10 +18,8 @@ namespace LightweightIocContainer.Exceptions
/// <param name="constructor">The constructor that does not match</param> /// <param name="constructor">The constructor that does not match</param>
/// <param name="exception">The inner exception</param> /// <param name="exception">The inner exception</param>
public ConstructorNotMatchingException(ConstructorInfo constructor, Exception exception) public ConstructorNotMatchingException(ConstructorInfo constructor, Exception exception)
: base($"Constructor {constructor} does not match the given or resolvable arguments.", exception) : base($"Constructor {constructor} does not match the given or resolvable arguments.", exception) =>
{
Constructor = constructor; Constructor = constructor;
}
/// <summary> /// <summary>
/// The constructor that does not match /// The constructor that does not match

@ -17,10 +17,8 @@ namespace LightweightIocContainer.Exceptions
/// <param name="message">The exception message</param> /// <param name="message">The exception message</param>
/// <param name="method">The method that is illegal to create</param> /// <param name="method">The method that is illegal to create</param>
public IllegalAbstractMethodCreationException(string message, MethodInfo method) public IllegalAbstractMethodCreationException(string message, MethodInfo method)
: base(message) : base(message) =>
{
Method = method; Method = method;
}
/// <summary> /// <summary>
/// The Method whose creation is illegal /// The Method whose creation is illegal

@ -37,13 +37,8 @@ namespace LightweightIocContainer.Exceptions
/// <param name="message">The message of the <see cref="Exception"/></param> /// <param name="message">The message of the <see cref="Exception"/></param>
/// <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 = new List<Exception>()
{
innerException
};
}
/// <summary> /// <summary>
/// The inner exceptions of the <see cref="IocContainerException"/> /// The inner exceptions of the <see cref="IocContainerException"/>

@ -17,10 +17,8 @@ namespace LightweightIocContainer.Exceptions
/// </summary> /// </summary>
/// <param name="type">The <see cref="System.Type"/> that is already registered in this <see cref="IIocContainer"/></param> /// <param name="type">The <see cref="System.Type"/> that is already registered in this <see cref="IIocContainer"/></param>
public MultipleRegistrationException(Type type) public MultipleRegistrationException(Type type)
: base($"Type {type.Name} is already registered in this IocContainer.") : base($"Type {type.Name} is already registered in this IocContainer.") =>
{
Type = type; Type = type;
}
/// <summary> /// <summary>
/// The registered <see cref="System.Type"/> /// The registered <see cref="System.Type"/>

@ -17,10 +17,8 @@ namespace LightweightIocContainer.Exceptions
/// <param name="message">The exception message</param> /// <param name="message">The exception message</param>
/// <param name="type">The <see cref="System.Type"/> of the multiton that's responsible for the exception</param> /// <param name="type">The <see cref="System.Type"/> of the multiton that's responsible for the exception</param>
public MultitonResolveException(string message, Type type) public MultitonResolveException(string message, Type type)
: base(message) : base(message) =>
{
Type = type; Type = type;
}
/// <summary> /// <summary>
/// The <see cref="System.Type"/> of the multiton that's responsible for the exception /// The <see cref="System.Type"/> of the multiton that's responsible for the exception

@ -22,11 +22,7 @@ namespace LightweightIocContainer.Exceptions
: base($"No matching constructor for {type} found.") : base($"No matching constructor for {type} found.")
{ {
Type = type; Type = type;
InnerExceptions = exceptions == null ? new List<Exception>() : exceptions.OfType<Exception>().ToList();
if (exceptions == null)
InnerExceptions = new List<Exception>();
else
InnerExceptions = exceptions.OfType<Exception>().ToList();
} }
@ -39,9 +35,6 @@ namespace LightweightIocContainer.Exceptions
/// Add an inner exception to the <see cref="IocContainerException.InnerExceptions"/> /// Add an inner exception to the <see cref="IocContainerException.InnerExceptions"/>
/// </summary> /// </summary>
/// <param name="exception">The <see cref="ConstructorNotMatchingException"/></param> /// <param name="exception">The <see cref="ConstructorNotMatchingException"/></param>
public void AddInnerException(ConstructorNotMatchingException exception) public void AddInnerException(ConstructorNotMatchingException exception) => InnerExceptions.Add(exception);
{
InnerExceptions.Add(exception);
}
} }
} }

@ -16,10 +16,8 @@ namespace LightweightIocContainer.Exceptions
/// </summary> /// </summary>
/// <param name="type">The <see cref="Type"/> with no public constructor</param> /// <param name="type">The <see cref="Type"/> with no public constructor</param>
public NoPublicConstructorFoundException(Type type) public NoPublicConstructorFoundException(Type type)
: base($"No public constructor for {type} found.") : base($"No public constructor for {type} found.") =>
{
Type = type; Type = type;
}
/// <summary> /// <summary>
/// The <see cref="Type"/> with no public constructor /// The <see cref="Type"/> with no public constructor

@ -17,10 +17,8 @@ namespace LightweightIocContainer.Exceptions
/// </summary> /// </summary>
/// <param name="type">The unregistered <see cref="System.Type"/></param> /// <param name="type">The unregistered <see cref="System.Type"/></param>
public TypeNotRegisteredException(Type type) public TypeNotRegisteredException(Type type)
: base($"Type {type.Name} is not registered in this IocContainer.") : base($"Type {type.Name} is not registered in this IocContainer.") =>
{
Type = type; Type = type;
}
/// <summary> /// <summary>
/// The unregistered <see cref="System.Type"/> /// The unregistered <see cref="System.Type"/>

@ -27,9 +27,6 @@ namespace LightweightIocContainer.Installers
/// </summary> /// </summary>
/// <param name="assembly">The given <see cref="Assembly"/></param> /// <param name="assembly">The given <see cref="Assembly"/></param>
/// <returns>A new <see cref="IAssemblyInstaller"/> with the given <see cref="Assembly"/></returns> /// <returns>A new <see cref="IAssemblyInstaller"/> with the given <see cref="Assembly"/></returns>
public static IAssemblyInstaller Instance(Assembly assembly) public static IAssemblyInstaller Instance(Assembly assembly) => new AssemblyInstaller(assembly);
{
return new AssemblyInstaller(assembly);
}
} }
} }

@ -32,11 +32,7 @@ namespace LightweightIocContainer
/// <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);
}
/// <summary> /// <summary>
/// Install the given installers for the current <see cref="IocContainer"/> /// Install the given installers for the current <see cref="IocContainer"/>
@ -259,10 +255,7 @@ namespace LightweightIocContainer
/// </summary> /// </summary>
/// <typeparam name="T">The given <see cref="Type"/></typeparam> /// <typeparam name="T">The given <see cref="Type"/></typeparam>
/// <returns>An instance of the given <see cref="Type"/></returns> /// <returns>An instance of the given <see cref="Type"/></returns>
public T Resolve<T>() public T Resolve<T>() => ResolveInternal<T>(null);
{
return ResolveInternal<T>(null);
}
/// <summary> /// <summary>
/// Gets an instance of the given <see cref="Type"/> /// Gets an instance of the given <see cref="Type"/>
@ -270,10 +263,7 @@ namespace LightweightIocContainer
/// <typeparam name="T">The given <see cref="Type"/></typeparam> /// <typeparam name="T">The given <see cref="Type"/></typeparam>
/// <param name="arguments">The constructor arguments</param> /// <param name="arguments">The constructor arguments</param>
/// <returns>An instance of the given <see cref="Type"/></returns> /// <returns>An instance of the given <see cref="Type"/></returns>
public T Resolve<T>(params object[] arguments) public T Resolve<T>(params object[] arguments) => ResolveInternal<T>(arguments);
{
return ResolveInternal<T>(arguments);
}
/// <summary> /// <summary>
/// Gets an instance of the given <see cref="Type"/> /// Gets an instance of the given <see cref="Type"/>
@ -283,10 +273,8 @@ namespace LightweightIocContainer
/// <param name="resolveStack">The current resolve stack</param> /// <param name="resolveStack">The current resolve stack</param>
/// <returns>An instance of the given <see cref="Type"/></returns> /// <returns>An instance of the given <see cref="Type"/></returns>
/// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception> /// <exception cref="InternalResolveException">Could not find function <see cref="ResolveInternal{T}"/></exception>
private object Resolve(Type type, object[] arguments, List<Type> resolveStack) private object Resolve(Type type, object[] arguments, List<Type> resolveStack) =>
{ GenericMethodCaller.Call(this, nameof(ResolveInternal), type, BindingFlags.NonPublic | BindingFlags.Instance, arguments, resolveStack);
return GenericMethodCaller.Call(this, nameof(ResolveInternal), type, BindingFlags.NonPublic | BindingFlags.Instance, arguments, resolveStack);
}
/// <summary> /// <summary>
/// Gets an instance of a given registered <see cref="Type"/> /// Gets an instance of a given registered <see cref="Type"/>
@ -582,9 +570,7 @@ namespace LightweightIocContainer
} }
catch (Exception ex) catch (Exception ex)
{ {
if (noMatchingConstructorFoundException == null) noMatchingConstructorFoundException ??= new NoMatchingConstructorFoundException(type);
noMatchingConstructorFoundException = new NoMatchingConstructorFoundException(type);
noMatchingConstructorFoundException.AddInnerException(new ConstructorNotMatchingException(ctor, ex)); noMatchingConstructorFoundException.AddInnerException(new ConstructorNotMatchingException(ctor, ex));
} }
} }

@ -21,9 +21,7 @@ namespace LightweightIocContainer.Registrations
/// <param name="implementationType">The <see cref="Type"/> of the implementation</param> /// <param name="implementationType">The <see cref="Type"/> of the implementation</param>
/// <param name="lifestyle">The <see cref="Lifestyle"/> of the <see cref="RegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> of the <see cref="RegistrationBase{TInterface}"/></param>
public DefaultRegistration(Type interfaceType, Type implementationType, Lifestyle lifestyle) public DefaultRegistration(Type interfaceType, Type implementationType, Lifestyle lifestyle)
: base(interfaceType, implementationType, lifestyle) : base(interfaceType, implementationType, lifestyle) =>
{
Name = $"{InterfaceType.Name}, {ImplementationType.Name}, Lifestyle: {Lifestyle.ToString()}"; Name = $"{InterfaceType.Name}, {ImplementationType.Name}, Lifestyle: {Lifestyle.ToString()}";
} }
}
} }

@ -21,10 +21,8 @@ namespace LightweightIocContainer.Registrations
/// <param name="implementationType">The <see cref="Type"/> of the Implementation</param> /// <param name="implementationType">The <see cref="Type"/> of the Implementation</param>
/// <param name="scope">The <see cref="Type"/> of the Multiton Scope</param> /// <param name="scope">The <see cref="Type"/> of the Multiton Scope</param>
public MultitonRegistration(Type interfaceType, Type implementationType, Type scope) public MultitonRegistration(Type interfaceType, Type implementationType, Type scope)
: base(interfaceType, implementationType, Lifestyle.Multiton) : base(interfaceType, implementationType, Lifestyle.Multiton) =>
{
Scope = scope; Scope = scope;
}
/// <summary> /// <summary>
/// The <see cref="Type"/> of the multiton scope /// The <see cref="Type"/> of the multiton scope

@ -16,10 +16,7 @@ namespace LightweightIocContainer.Registrations
{ {
private readonly IIocContainer _iocContainer; private readonly IIocContainer _iocContainer;
internal RegistrationFactory(IIocContainer container) internal RegistrationFactory(IIocContainer container) => _iocContainer = container;
{
_iocContainer = container;
}
/// <summary> /// <summary>
/// Register an Interface with a Type that implements it and create a <see cref="IDefaultRegistration{TInterface,TImplementation}"/> /// Register an Interface with a Type that implements it and create a <see cref="IDefaultRegistration{TInterface,TImplementation}"/>
@ -28,15 +25,17 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The Type that implements the interface</typeparam> /// <typeparam name="TImplementation">The Type that implements the interface</typeparam>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IDefaultRegistration{TInterface,TImplementation}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IDefaultRegistration{TInterface,TImplementation}"/></param>
/// <returns>A new created <see cref="IDefaultRegistration{TInterface,TImplementation}"/> with the given parameters</returns> /// <returns>A new created <see cref="IDefaultRegistration{TInterface,TImplementation}"/> with the given parameters</returns>
public IDefaultRegistration<TInterface, TImplementation> Register<TInterface, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface public IDefaultRegistration<TInterface, TImplementation> Register<TInterface, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface =>
{ new DefaultRegistration<TInterface, TImplementation>(typeof(TInterface), typeof(TImplementation), lifestyle);
return new DefaultRegistration<TInterface, TImplementation>(typeof(TInterface), typeof(TImplementation), lifestyle);
}
public IOpenGenericRegistration Register(Type tInterface, Type tImplementation, Lifestyle lifestyle) /// <summary>
{ /// Register an open generic Interface with an open generic Type that implements it and create a <see cref="IOpenGenericRegistration"/>
return new OpenGenericRegistration(tInterface, tImplementation, lifestyle); /// </summary>
} /// <param name="tInterface">The open generic Interface to register</param>
/// <param name="tImplementation">The open generic Type that implements the interface</param>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IOpenGenericRegistration"/></param>
/// <returns>The created <see cref="IOpenGenericRegistration"/></returns>
public IOpenGenericRegistration Register(Type tInterface, Type tImplementation, Lifestyle lifestyle) => new OpenGenericRegistration(tInterface, tImplementation, lifestyle);
/// <summary> /// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2}"/> /// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2}"/>
@ -46,10 +45,8 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam> /// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param>
/// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2}"/></returns> /// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2}"/></returns>
public IMultipleRegistration<TInterface1, TInterface2, TImplementation> Register<TInterface1, TInterface2, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2 public IMultipleRegistration<TInterface1, TInterface2, TImplementation> Register<TInterface1, TInterface2, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2 =>
{ new MultipleRegistration<TInterface1, TInterface2, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TImplementation), lifestyle);
return new MultipleRegistration<TInterface1, TInterface2, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TImplementation), lifestyle);
}
/// <summary> /// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3}"/> /// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3}"/>
@ -60,10 +57,8 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam> /// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param>
/// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3}"/></returns> /// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3}"/></returns>
public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> Register<TInterface1, TInterface2, TInterface3, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3 public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> Register<TInterface1, TInterface2, TInterface3, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3 =>
{ new MultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TImplementation), lifestyle);
return new MultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TImplementation), lifestyle);
}
/// <summary> /// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4}"/> /// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4}"/>
@ -75,10 +70,8 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam> /// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param>
/// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4}"/></returns> /// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4}"/></returns>
public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4 public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4 =>
{ new MultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TImplementation), lifestyle);
return new MultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TImplementation), lifestyle);
}
/// <summary> /// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4,TInterface5}"/> /// Register multiple interfaces for a <see cref="Type"/> that implements them and create a <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4,TInterface5}"/>
@ -91,10 +84,8 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam> /// <typeparam name="TImplementation">The <see cref="Type"/> that implements both interfaces</typeparam>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistrationBase{TInterface}"/></param>
/// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4,TInterface5}"/></returns> /// <returns>The created <see cref="IMultipleRegistration{TInterface1,TInterface2,TInterface3,TInterface4,TInterface5}"/></returns>
public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4, TInterface5 public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(Lifestyle lifestyle) where TImplementation : TInterface1, TInterface2, TInterface3, TInterface4, TInterface5 =>
{ new MultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TInterface5), typeof(TImplementation), lifestyle);
return new MultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TInterface3), typeof(TInterface4), typeof(TInterface5), typeof(TImplementation), lifestyle);
}
/// <summary> /// <summary>
/// Register a <see cref="Type"/> without an interface and create a <see cref="ISingleTypeRegistration{TInterface}"/> /// Register a <see cref="Type"/> without an interface and create a <see cref="ISingleTypeRegistration{TInterface}"/>
@ -102,10 +93,7 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="T">The <see cref="Type"/> to register</typeparam> /// <typeparam name="T">The <see cref="Type"/> to register</typeparam>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="ISingleTypeRegistration{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="ISingleTypeRegistration{TInterface}"/></param>
/// <returns>A new created <see cref="ISingleTypeRegistration{TInterface}"/> with the given parameters</returns> /// <returns>A new created <see cref="ISingleTypeRegistration{TInterface}"/> with the given parameters</returns>
public ISingleTypeRegistration<T> Register<T>(Lifestyle lifestyle) public ISingleTypeRegistration<T> Register<T>(Lifestyle lifestyle) => new SingleTypeRegistration<T>(typeof(T), lifestyle);
{
return new SingleTypeRegistration<T>(typeof(T), lifestyle);
}
/// <summary> /// <summary>
/// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface,TImplementation}"/> /// Register an Interface with a Type that implements it as a multiton and create a <see cref="IMultitonRegistration{TInterface,TImplementation}"/>
@ -114,10 +102,8 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The Type that implements the interface</typeparam> /// <typeparam name="TImplementation">The Type that implements the interface</typeparam>
/// <typeparam name="TScope">The Type of the multiton scope</typeparam> /// <typeparam name="TScope">The Type of the multiton scope</typeparam>
/// <returns>A new created <see cref="IMultitonRegistration{TInterface,TImplementation}"/> with the given parameters</returns> /// <returns>A new created <see cref="IMultitonRegistration{TInterface,TImplementation}"/> with the given parameters</returns>
public IMultitonRegistration<TInterface, TImplementation> RegisterMultiton<TInterface, TImplementation, TScope>() where TImplementation : TInterface public IMultitonRegistration<TInterface, TImplementation> RegisterMultiton<TInterface, TImplementation, TScope>() where TImplementation : TInterface =>
{ new MultitonRegistration<TInterface, TImplementation>(typeof(TInterface), typeof(TImplementation), typeof(TScope));
return new MultitonRegistration<TInterface, TImplementation>(typeof(TInterface), typeof(TImplementation), typeof(TScope));
}
/// <summary> /// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them as a multiton /// Register multiple interfaces for a <see cref="Type"/> that implements them as a multiton
@ -127,19 +113,14 @@ namespace LightweightIocContainer.Registrations
/// <typeparam name="TImplementation">The Type that implements the interface</typeparam> /// <typeparam name="TImplementation">The Type that implements the interface</typeparam>
/// <typeparam name="TScope">The Type of the multiton scope</typeparam> /// <typeparam name="TScope">The Type of the multiton scope</typeparam>
/// <returns>A new created <see cref="IMultipleMultitonRegistration{TInterface1,TInterface2,TImplementation}"/> with the given parameters</returns> /// <returns>A new created <see cref="IMultipleMultitonRegistration{TInterface1,TInterface2,TImplementation}"/> with the given parameters</returns>
public IMultipleMultitonRegistration<TInterface1, TInterface2, TImplementation> RegisterMultiton<TInterface1, TInterface2, TImplementation, TScope>() where TImplementation : TInterface1, TInterface2 public IMultipleMultitonRegistration<TInterface1, TInterface2, TImplementation> RegisterMultiton<TInterface1, TInterface2, TImplementation, TScope>() where TImplementation : TInterface1, TInterface2 =>
{ new MultipleMultitonRegistration<TInterface1, TInterface2, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TImplementation), typeof(TScope));
return new MultipleMultitonRegistration<TInterface1, TInterface2, TImplementation>(typeof(TInterface1), typeof(TInterface2), typeof(TImplementation), typeof(TScope));
}
/// <summary> /// <summary>
/// Register an Interface as an abstract typed factory and create a <see cref="ITypedFactoryRegistration{TFactory}"/> /// Register an Interface as an abstract typed factory and create a <see cref="ITypedFactoryRegistration{TFactory}"/>
/// </summary> /// </summary>
/// <typeparam name="TFactory">The abstract typed factory to register</typeparam> /// <typeparam name="TFactory">The abstract typed factory to register</typeparam>
/// <returns>A new created <see cref="ITypedFactoryRegistration{TFactory}"/> with the given parameters</returns> /// <returns>A new created <see cref="ITypedFactoryRegistration{TFactory}"/> with the given parameters</returns>
public ITypedFactoryRegistration<TFactory> RegisterFactory<TFactory>() public ITypedFactoryRegistration<TFactory> RegisterFactory<TFactory>() => new TypedFactoryRegistration<TFactory>(typeof(TFactory), _iocContainer);
{
return new TypedFactoryRegistration<TFactory>(typeof(TFactory), _iocContainer);
}
} }
} }

@ -20,10 +20,8 @@ namespace LightweightIocContainer.Registrations
/// <param name="interfaceType">The <see cref="Type"/> of the interface or <see cref="Type"/></param> /// <param name="interfaceType">The <see cref="Type"/> of the interface or <see cref="Type"/></param>
/// <param name="lifestyle">The <see cref="Lifestyle"/> of the <see cref="RegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> of the <see cref="RegistrationBase{TInterface}"/></param>
public SingleTypeRegistration(Type interfaceType, Lifestyle lifestyle) public SingleTypeRegistration(Type interfaceType, Lifestyle lifestyle)
: base(interfaceType, lifestyle) : base(interfaceType, lifestyle) =>
{
Name = $"{InterfaceType.Name}, Lifestyle: {Lifestyle.ToString()}"; Name = $"{InterfaceType.Name}, Lifestyle: {Lifestyle.ToString()}";
}
/// <summary> /// <summary>
/// <see cref="Func{T,TResult}"/> that is invoked instead of creating an instance of this <see cref="Type"/> the default way /// <see cref="Func{T,TResult}"/> that is invoked instead of creating an instance of this <see cref="Type"/> the default way

@ -21,10 +21,8 @@ namespace LightweightIocContainer.Registrations
/// <param name="implementationType">The <see cref="Type"/> of the implementation type</param> /// <param name="implementationType">The <see cref="Type"/> of the implementation type</param>
/// <param name="lifestyle">The <see cref="Lifestyle"/> of this <see cref="IRegistrationBase{TInterface}"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> of this <see cref="IRegistrationBase{TInterface}"/></param>
protected TypedRegistrationBase(Type interfaceType, Type implementationType, Lifestyle lifestyle) protected TypedRegistrationBase(Type interfaceType, Type implementationType, Lifestyle lifestyle)
: base(interfaceType, lifestyle) : base(interfaceType, lifestyle) =>
{
ImplementationType = implementationType; ImplementationType = implementationType;
}
/// <summary> /// <summary>
/// The <see cref="Type"/> that implements the <see cref="IRegistration.InterfaceType"/> that is registered with this <see cref="IRegistrationBase{TInterface}"/> /// The <see cref="Type"/> that implements the <see cref="IRegistration.InterfaceType"/> that is registered with this <see cref="IRegistrationBase{TInterface}"/>

@ -11,8 +11,6 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class ActionExtensionTest public class ActionExtensionTest
{ {
#region TestClasses
private interface IBar private interface IBar
{ {
void Throw(); void Throw();
@ -25,13 +23,9 @@ namespace Test.LightweightIocContainer
private class Foo : IFoo private class Foo : IFoo
{ {
public void Throw() public void Throw() => throw new Exception();
{
throw new Exception();
}
} }
#endregion TestClasses
[Test] [Test]
public void TestConvert() public void TestConvert()

@ -19,14 +19,9 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class AssemblyInstallerTest public class AssemblyInstallerTest
{ {
#region TestClasses
private class TestInstaller : IIocInstaller private class TestInstaller : IIocInstaller
{ {
public void Install(IIocContainer container) public void Install(IIocContainer container) => container.Register<Mock<IRegistration>>();
{
container.Register<Mock<IRegistration>>();
}
} }
[UsedImplicitly] [UsedImplicitly]
@ -35,7 +30,6 @@ namespace Test.LightweightIocContainer
} }
#endregion TestClasses
[Test] [Test]
public void TestInstall() public void TestInstall()

@ -12,8 +12,6 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class EnumerableExtensionTest public class EnumerableExtensionTest
{ {
#region TestClasses
private class ListObject private class ListObject
{ {
public int Index { get; set; } public int Index { get; set; }
@ -24,8 +22,6 @@ namespace Test.LightweightIocContainer
} }
#endregion TestClasses
[Test] [Test]
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]

@ -3,6 +3,7 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System; using System;
using JetBrains.Annotations;
using LightweightIocContainer; using LightweightIocContainer;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using NUnit.Framework; using NUnit.Framework;
@ -12,10 +13,9 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class IocContainerInterfaceSegregationTest public class IocContainerInterfaceSegregationTest
{ {
#region TestClasses
private interface IBar private interface IBar
{ {
} }
private interface IFoo private interface IFoo
@ -24,39 +24,33 @@ namespace Test.LightweightIocContainer
} }
private interface IAnotherBar private interface IAnotherBar
{ {
} }
private interface IAnotherFoo private interface IAnotherFoo
{ {
} }
private interface IAnotherOne private interface IAnotherOne
{ {
} }
[UsedImplicitly]
private class Foo : IFoo, IBar, IAnotherFoo, IAnotherBar, IAnotherOne private class Foo : IFoo, IBar, IAnotherFoo, IAnotherBar, IAnotherOne
{ {
public void ThrowFoo() public void ThrowFoo() => throw new Exception("Foo");
{
throw new Exception("Foo");
}
} }
#endregion TestClasses
private IIocContainer _container; private IIocContainer _container;
[SetUp] [SetUp]
public void SetUp() public void SetUp() => _container = new IocContainer();
{
_container = new IocContainer();
}
[TearDown] [TearDown]
public void TearDown() public void TearDown() => _container.Dispose();
{
_container.Dispose();
}
[Test] [Test]
public void TestRegistrationOnCreate2() public void TestRegistrationOnCreate2()

@ -13,8 +13,6 @@ namespace Test.LightweightIocContainer
// ReSharper disable MemberHidesStaticFromOuterClass // ReSharper disable MemberHidesStaticFromOuterClass
public class IocContainerParameterRegistrationTest public class IocContainerParameterRegistrationTest
{ {
#region TestClasses
[UsedImplicitly] [UsedImplicitly]
public interface IA public interface IA
{ {
@ -61,6 +59,7 @@ namespace Test.LightweightIocContainer
{ {
public B(IC c) public B(IC c)
{ {
} }
} }
@ -87,22 +86,14 @@ namespace Test.LightweightIocContainer
public IC C { get; } public IC C { get; }
} }
#endregion TestClasses
private IIocContainer _iocContainer; private IIocContainer _iocContainer;
[SetUp] [SetUp]
public void SetUp() public void SetUp() => _iocContainer = new IocContainer();
{
_iocContainer = new IocContainer();
}
[TearDown] [TearDown]
public void TearDown() public void TearDown() => _iocContainer.Dispose();
{
_iocContainer.Dispose();
}
[Test] [Test]

@ -14,8 +14,6 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class IocContainerRecursionTest public class IocContainerRecursionTest
{ {
#region TestClassesCircularDependency
[UsedImplicitly] [UsedImplicitly]
public interface IFoo public interface IFoo
{ {
@ -44,10 +42,6 @@ namespace Test.LightweightIocContainer
} }
} }
#endregion TestClassesCircularDependency
#region TestClassesNonCircularDependency
[UsedImplicitly] [UsedImplicitly]
public interface IA public interface IA
{ {
@ -71,6 +65,7 @@ namespace Test.LightweightIocContainer
{ {
public A(IB b, IC c) public A(IB b, IC c)
{ {
} }
} }
@ -79,6 +74,7 @@ namespace Test.LightweightIocContainer
{ {
public B(IC c) public B(IC c)
{ {
} }
} }
@ -88,23 +84,14 @@ namespace Test.LightweightIocContainer
} }
#endregion TestClassesNonCircularDependency
private IIocContainer _iocContainer; private IIocContainer _iocContainer;
[SetUp] [SetUp]
public void SetUp() public void SetUp() => _iocContainer = new IocContainer();
{
_iocContainer = new IocContainer();
}
[TearDown] [TearDown]
public void TearDown() public void TearDown() => _iocContainer.Dispose();
{
_iocContainer.Dispose();
}
[Test] [Test]
public void TestCircularDependencies() public void TestCircularDependencies()

@ -11,9 +11,6 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class IocContainerTest public class IocContainerTest
{ {
#region TestClasses
//some of the test classes have to be public to allow the implementation of the factory
public interface ITest public interface ITest
{ {
@ -101,10 +98,7 @@ namespace Test.LightweightIocContainer
[UsedImplicitly] [UsedImplicitly]
private readonly byte _id; private readonly byte _id;
public TestByte(byte id) public TestByte(byte id) => _id = id;
{
_id = id;
}
} }
[UsedImplicitly] [UsedImplicitly]
@ -118,23 +112,14 @@ namespace Test.LightweightIocContainer
} }
#endregion TestClasses
private IIocContainer _iocContainer; private IIocContainer _iocContainer;
[SetUp] [SetUp]
public void SetUp() public void SetUp() => _iocContainer = new IocContainer();
{
_iocContainer = new IocContainer();
}
[TearDown] [TearDown]
public void TearDown() public void TearDown() => _iocContainer.Dispose();
{
_iocContainer.Dispose();
}
[Test] [Test]
@ -165,34 +150,19 @@ namespace Test.LightweightIocContainer
} }
[Test] [Test]
public void TestRegister() public void TestRegister() => Assert.DoesNotThrow(() => _iocContainer.Register<ITest, Test>());
{
Assert.DoesNotThrow(() => _iocContainer.Register<ITest, Test>());
}
[Test] [Test]
public void TestRegisterTypeWithoutInterface() public void TestRegisterTypeWithoutInterface() => Assert.DoesNotThrow(() => _iocContainer.Register<Test>());
{
Assert.DoesNotThrow(() => _iocContainer.Register<Test>());
}
[Test] [Test]
public void TestRegisterMultiton() public void TestRegisterMultiton() => Assert.DoesNotThrow(() => _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>());
{
Assert.DoesNotThrow(() => _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>());
}
[Test] [Test]
public void TestInvalidMultitonRegistration() public void TestInvalidMultitonRegistration() => Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register<ITest, Test>(Lifestyle.Multiton));
{
Assert.Throws<InvalidRegistrationException>(() => _iocContainer.Register<ITest, Test>(Lifestyle.Multiton));
}
[Test] [Test]
public void TestRegisterFactory() public void TestRegisterFactory() => Assert.DoesNotThrow(() => _iocContainer.RegisterFactory<ITestFactory>());
{
Assert.DoesNotThrow(() => _iocContainer.RegisterFactory<ITestFactory>());
}
[Test] [Test]
public void TestRegisterMultiple() public void TestRegisterMultiple()
@ -203,16 +173,10 @@ namespace Test.LightweightIocContainer
} }
[Test] [Test]
public void TestRegisterFactoryWithoutCreate() public void TestRegisterFactoryWithoutCreate() => Assert.Throws<InvalidFactoryRegistrationException>(() => _iocContainer.RegisterFactory<ITestFactoryNoCreate>());
{
Assert.Throws<InvalidFactoryRegistrationException>(() => _iocContainer.RegisterFactory<ITestFactoryNoCreate>());
}
[Test] [Test]
public void TestRegisterFactoryClearMultitonsNonGeneric() public void TestRegisterFactoryClearMultitonsNonGeneric() => Assert.Throws<IllegalAbstractMethodCreationException>(() => _iocContainer.RegisterFactory<ITestFactoryNonGenericClear>());
{
Assert.Throws<IllegalAbstractMethodCreationException>(() => _iocContainer.RegisterFactory<ITestFactoryNonGenericClear>());
}
[Test] [Test]
public void TestResolveNotRegistered() public void TestResolveNotRegistered()

@ -15,8 +15,6 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class OnCreateTest public class OnCreateTest
{ {
#region TestClasses
private interface ITest private interface ITest
{ {
void DoSomething(); void DoSomething();
@ -24,13 +22,9 @@ namespace Test.LightweightIocContainer
private class Test : ITest private class Test : ITest
{ {
public void DoSomething() public void DoSomething() => throw new Exception();
{
throw new Exception();
}
} }
#endregion
[Test] [Test]
public void TestOnCreate() public void TestOnCreate()

@ -60,11 +60,11 @@ namespace Test.LightweightIocContainer
} }
[Test] [Test]
public void TestRegisterOpenGenericTypeAsMultitonThrowsException() public void TestRegisterOpenGenericTypeAsMultitonThrowsException() =>
=> Assert.Throws<InvalidRegistrationException>(() => _iocContainer.RegisterOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Multiton)); Assert.Throws<InvalidRegistrationException>(() => _iocContainer.RegisterOpenGenerics(typeof(ITest<>), typeof(Test<>), Lifestyle.Multiton));
[Test] [Test]
public void TestRegisterNonOpenGenericTypeWithOpenGenericsFunctionThrowsException() public void TestRegisterNonOpenGenericTypeWithOpenGenericsFunctionThrowsException() =>
=> Assert.Throws<InvalidRegistrationException>(() => _iocContainer.RegisterOpenGenerics(typeof(int), typeof(int))); Assert.Throws<InvalidRegistrationException>(() => _iocContainer.RegisterOpenGenerics(typeof(int), typeof(int)));
} }
} }

@ -16,8 +16,6 @@ namespace Test.LightweightIocContainer
[TestFixture] [TestFixture]
public class RegistrationBaseTest public class RegistrationBaseTest
{ {
#region TestClasses
private interface ITest private interface ITest
{ {
@ -52,8 +50,6 @@ namespace Test.LightweightIocContainer
} }
#endregion
[Test] [Test]
public void TestWithParameters() public void TestWithParameters()

@ -29,10 +29,7 @@ namespace Test.LightweightIocContainer
[UsedImplicitly] [UsedImplicitly]
private class Foo : IFoo private class Foo : IFoo
{ {
public Foo(IBar bar) public Foo(IBar bar) => Bar = bar;
{
Bar = bar;
}
public IBar Bar { get; } public IBar Bar { get; }
} }

Loading…
Cancel
Save