- refactoring

pull/57/head
Simon G 4 years ago
parent 7f4ea56929
commit d83d94e393
  1. 8
      LightweightIocContainer/EnumerableExtension.cs
  2. 4
      LightweightIocContainer/Exceptions/CircularDependencyException.cs
  3. 2
      LightweightIocContainer/Exceptions/IocContainerException.cs
  4. 1
      LightweightIocContainer/Factories/TypedFactory.cs
  5. 2
      LightweightIocContainer/Installers/AssemblyInstaller.cs
  6. 3
      LightweightIocContainer/Interfaces/IIocContainer.cs
  7. 71
      LightweightIocContainer/IocContainer.cs
  8. 4
      LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs
  9. 8
      LightweightIocContainer/Registrations/MultipleRegistration.cs
  10. 3
      LightweightIocContainer/Validation/IocValidator.cs
  11. 12
      Test.LightweightIocContainer/AssemblyInstallerTest.cs
  12. 24
      Test.LightweightIocContainer/EnumerableExtensionTest.cs
  13. 8
      Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs
  14. 28
      Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs
  15. 4
      Test.LightweightIocContainer/IocContainerRecursionTest.cs
  16. 24
      Test.LightweightIocContainer/IocContainerTest.cs
  17. 8
      Test.LightweightIocContainer/MultipleMultitonRegistrationTest.cs
  18. 3
      Test.LightweightIocContainer/OnCreateTest.cs
  19. 8
      Test.LightweightIocContainer/RegistrationBaseTest.cs
  20. 4
      Test.LightweightIocContainer/SingleTypeRegistrationTest.cs

@ -43,13 +43,7 @@ namespace LightweightIocContainer
{ {
try try
{ {
TSource first; return predicate == null ? source.First() : source.First(predicate);
if (predicate == null)
first = source.First();
else
first = source.First(predicate);
return first;
} }
catch (Exception) catch (Exception)
{ {

@ -44,7 +44,7 @@ namespace LightweightIocContainer.Exceptions
{ {
get get
{ {
StringBuilder message = new StringBuilder($"Circular dependency has been detected when trying to resolve `{ResolvingType}`.\n"); StringBuilder message = new($"Circular dependency has been detected when trying to resolve `{ResolvingType}`.\n");
if (ResolveStack == null || !ResolveStack.Any()) if (ResolveStack == null || !ResolveStack.Any())
return message.ToString(); return message.ToString();
@ -52,9 +52,7 @@ namespace LightweightIocContainer.Exceptions
message.Append($"\t`{ResolvingType}` resolved as dependency of\n"); message.Append($"\t`{ResolvingType}` resolved as dependency of\n");
for (int i = ResolveStack.Count - 1; i >= 1 ; i--) for (int i = ResolveStack.Count - 1; i >= 1 ; i--)
{
message.Append($"\t`{ResolveStack[i]}` resolved as dependency of\n"); message.Append($"\t`{ResolveStack[i]}` resolved as dependency of\n");
}
message.Append($"\t`{ResolveStack[0]}` which is the root type being resolved."); message.Append($"\t`{ResolveStack[0]}` which is the root type being resolved.");
return message.ToString(); return message.ToString();

@ -38,7 +38,7 @@ namespace LightweightIocContainer.Exceptions
/// <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"/>

@ -3,7 +3,6 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Reflection.Emit; using System.Reflection.Emit;

@ -45,9 +45,7 @@ namespace LightweightIocContainer.Installers
public void Install(IIocContainer container) public void Install(IIocContainer container)
{ {
foreach (IIocInstaller installer in Installers) foreach (IIocInstaller installer in Installers)
{
installer.Install(container); installer.Install(container);
} }
} }
} }
}

@ -36,8 +36,7 @@ namespace LightweightIocContainer.Interfaces
/// <param name="tImplementation">The open generic Type that implements the interface</param> /// <param name="tImplementation">The open generic Type that implements the interface</param>
/// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistration"/></param> /// <param name="lifestyle">The <see cref="Lifestyle"/> for this <see cref="IRegistration"/></param>
/// <returns>The created <see cref="IRegistration"/></returns> /// <returns>The created <see cref="IRegistration"/></returns>
IOpenGenericRegistration RegisterOpenGenerics(Type tInterface, Type tImplementation, IOpenGenericRegistration RegisterOpenGenerics(Type tInterface, Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient);
Lifestyle lifestyle = Lifestyle.Transient);
/// <summary> /// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them /// Register multiple interfaces for a <see cref="Type"/> that implements them

@ -25,8 +25,8 @@ namespace LightweightIocContainer
{ {
private readonly RegistrationFactory _registrationFactory; private readonly RegistrationFactory _registrationFactory;
private readonly List<(Type type, object instance)> _singletons = new List<(Type, object)>(); private readonly List<(Type type, object instance)> _singletons = new();
private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object> instances)> _multitons = new List<(Type, Type, ConditionalWeakTable<object, object>)>(); private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object> instances)> _multitons = new();
/// <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
@ -47,9 +47,7 @@ namespace LightweightIocContainer
public IIocContainer Install(params IIocInstaller[] installers) public IIocContainer Install(params IIocInstaller[] installers)
{ {
foreach (IIocInstaller installer in installers) foreach (IIocInstaller installer in installers)
{
installer.Install(this); installer.Install(this);
}
return this; return this;
} }
@ -119,8 +117,7 @@ namespace LightweightIocContainer
/// <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 = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1 public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> Register<TInterface1, TInterface2, TInterface3, TImplementation>(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1
{ {
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> multipleRegistration = IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> multipleRegistration = _registrationFactory.Register<TInterface1, TInterface2, TInterface3, TImplementation>(lifestyle);
_registrationFactory.Register<TInterface1, TInterface2, TInterface3, TImplementation>(lifestyle);
Register(multipleRegistration); Register(multipleRegistration);
return multipleRegistration; return multipleRegistration;
@ -138,8 +135,7 @@ namespace LightweightIocContainer
/// <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 = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1 public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1
{ {
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> multipleRegistration = IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> multipleRegistration = _registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(lifestyle);
_registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(lifestyle);
Register(multipleRegistration); Register(multipleRegistration);
return multipleRegistration; return multipleRegistration;
@ -158,8 +154,7 @@ namespace LightweightIocContainer
/// <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 = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1 public IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1
{ {
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> multipleRegistration = IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> multipleRegistration = _registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(lifestyle);
_registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(lifestyle);
Register(multipleRegistration); Register(multipleRegistration);
return multipleRegistration; return multipleRegistration;
@ -243,11 +238,9 @@ namespace LightweightIocContainer
/// <param name="multipleRegistration">The <see cref="IMultipleRegistration{TInterface1,TImplementation}"/></param> /// <param name="multipleRegistration">The <see cref="IMultipleRegistration{TInterface1,TImplementation}"/></param>
private void Register<TInterface1, TImplementation>(IMultipleRegistration<TInterface1, TImplementation> multipleRegistration) where TImplementation : TInterface1 private void Register<TInterface1, TImplementation>(IMultipleRegistration<TInterface1, TImplementation> multipleRegistration) where TImplementation : TInterface1
{ {
foreach (var registration in multipleRegistration.Registrations) foreach (IRegistration registration in multipleRegistration.Registrations)
{
Register(registration); Register(registration);
} }
}
/// <summary> /// <summary>
/// Gets an instance of the given <see cref="Type"/> /// Gets an instance of the given <see cref="Type"/>
@ -298,23 +291,14 @@ namespace LightweightIocContainer
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
T resolvedInstance; T resolvedInstance = registration switch
if (registration is IRegistrationBase defaultRegistration)
{ {
if (defaultRegistration.Lifestyle == Lifestyle.Singleton) IRegistrationBase { Lifestyle: Lifestyle.Singleton } defaultRegistration => GetOrCreateSingletonInstance<T>(defaultRegistration, arguments, resolveStack),
resolvedInstance = GetOrCreateSingletonInstance<T>(defaultRegistration, arguments, resolveStack); IRegistrationBase { Lifestyle: Lifestyle.Multiton } and IMultitonRegistration multitonRegistration => GetOrCreateMultitonInstance<T>(multitonRegistration, arguments, resolveStack),
else if (defaultRegistration is IMultitonRegistration multitonRegistration && defaultRegistration.Lifestyle == Lifestyle.Multiton) IRegistrationBase defaultRegistration => CreateInstance<T>(defaultRegistration, arguments, resolveStack),
resolvedInstance = GetOrCreateMultitonInstance<T>(multitonRegistration, arguments, resolveStack); ITypedFactoryRegistration<T> typedFactoryRegistration => typedFactoryRegistration.Factory.Factory,
else _ => throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}.")
resolvedInstance = CreateInstance<T>(defaultRegistration, arguments, resolveStack); };
}
else if (registration is ITypedFactoryRegistration<T> typedFactoryRegistration)
{
resolvedInstance = typedFactoryRegistration.Factory.Factory;
}
else
throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}.");
resolveStack.Remove(typeof(T)); //T was successfully resolved -> no circular dependency -> remove from resolve stack resolveStack.Remove(typeof(T)); //T was successfully resolved -> no circular dependency -> remove from resolve stack
@ -331,13 +315,12 @@ namespace LightweightIocContainer
/// <returns>An existing or newly created singleton instance of the given <see cref="Type"/></returns> /// <returns>An existing or newly created singleton instance of the given <see cref="Type"/></returns>
private T GetOrCreateSingletonInstance<T>(IRegistration registration, object[] arguments, List<Type> resolveStack) private T GetOrCreateSingletonInstance<T>(IRegistration registration, object[] arguments, List<Type> resolveStack)
{ {
Type type; Type type = registration switch
if (registration is ITypedRegistration typedRegistration) {
type = typedRegistration.ImplementationType; ITypedRegistration typedRegistration => typedRegistration.ImplementationType,
else if (registration is ISingleTypeRegistration<T> singleTypeRegistration) ISingleTypeRegistration<T> singleTypeRegistration => singleTypeRegistration.InterfaceType,
type = singleTypeRegistration.InterfaceType; _ => throw new UnknownRegistrationException($"There is no registration {registration.GetType().Name} that can have lifestyle singleton.")
else };
throw new UnknownRegistrationException($"There is no registration {registration.GetType().Name} that can have lifestyle singleton.");
//if a singleton instance exists return it //if a singleton instance exists return it
object instance = _singletons.FirstOrDefault(s => s.type == type).instance; object instance = _singletons.FirstOrDefault(s => s.type == type).instance;
@ -385,7 +368,7 @@ namespace LightweightIocContainer
T newInstance = CreateInstance<T>(registration, arguments, resolveStack); T newInstance = CreateInstance<T>(registration, arguments, resolveStack);
ConditionalWeakTable<object, object> weakTable = new ConditionalWeakTable<object, object>(); ConditionalWeakTable<object, object> weakTable = new();
weakTable.Add(scopeArgument, newInstance); weakTable.Add(scopeArgument, newInstance);
_multitons.Add((registration.ImplementationType, registration.Scope, weakTable)); _multitons.Add((registration.ImplementationType, registration.Scope, weakTable));
@ -461,7 +444,7 @@ namespace LightweightIocContainer
if (i < registration.Parameters.Length) //if `i` is bigger than the length of the parameters, take the given arguments if (i < registration.Parameters.Length) //if `i` is bigger than the length of the parameters, take the given arguments
{ {
object currentParameter = registration.Parameters[i]; object currentParameter = registration.Parameters[i];
if (!(currentParameter is InternalResolvePlaceholder)) //use the parameter at the current index if it is not a placeholder if (currentParameter is not InternalResolvePlaceholder) //use the parameter at the current index if it is not a placeholder
{ {
newArguments[i] = currentParameter; newArguments[i] = currentParameter;
continue; continue;
@ -509,7 +492,7 @@ namespace LightweightIocContainer
try try
{ {
List<object> argumentsList = arguments?.ToList(); List<object> argumentsList = arguments?.ToList();
List<object> ctorParams = new List<object>(); List<object> ctorParams = new();
ParameterInfo[] parameters = ctor.GetParameters(); ParameterInfo[] parameters = ctor.GetParameters();
foreach (ParameterInfo parameter in parameters) foreach (ParameterInfo parameter in parameters)
@ -519,7 +502,8 @@ namespace LightweightIocContainer
{ {
fittingArgument = argumentsList.FirstOrGiven<object, InternalResolvePlaceholder>(a => fittingArgument = argumentsList.FirstOrGiven<object, InternalResolvePlaceholder>(a =>
a?.GetType() == parameter.ParameterType || parameter.ParameterType.IsInstanceOfType(a)); a?.GetType() == parameter.ParameterType || parameter.ParameterType.IsInstanceOfType(a));
if (!(fittingArgument is InternalResolvePlaceholder))
if (fittingArgument is not InternalResolvePlaceholder)
{ {
int index = argumentsList.IndexOf(fittingArgument); int index = argumentsList.IndexOf(fittingArgument);
argumentsList[index] = new InternalResolvePlaceholder(); argumentsList[index] = new InternalResolvePlaceholder();
@ -534,7 +518,7 @@ namespace LightweightIocContainer
{ {
fittingArgument = argumentsList.FirstOrGiven<object, InternalResolvePlaceholder>(a => parameter.ParameterType.GetDefault() == a); fittingArgument = argumentsList.FirstOrGiven<object, InternalResolvePlaceholder>(a => parameter.ParameterType.GetDefault() == a);
if (!(fittingArgument is InternalResolvePlaceholder)) if (fittingArgument is not InternalResolvePlaceholder)
{ {
int index = argumentsList.IndexOf(fittingArgument); int index = argumentsList.IndexOf(fittingArgument);
argumentsList[index] = new InternalResolvePlaceholder(); argumentsList[index] = new InternalResolvePlaceholder();
@ -586,10 +570,7 @@ namespace LightweightIocContainer
return null; return null;
List<IRegistration> openGenericRegistrations = Registrations.Where(r => r.InterfaceType.ContainsGenericParameters).ToList(); List<IRegistration> openGenericRegistrations = Registrations.Where(r => r.InterfaceType.ContainsGenericParameters).ToList();
if (!openGenericRegistrations.Any()) return !openGenericRegistrations.Any() ? null : openGenericRegistrations.FirstOrDefault(r => r.InterfaceType == typeof(T).GetGenericTypeDefinition());
return null;
return openGenericRegistrations.FirstOrDefault(r => r.InterfaceType == typeof(T).GetGenericTypeDefinition());
} }
/// <summary> /// <summary>

@ -28,7 +28,7 @@ namespace LightweightIocContainer.Registrations
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 List<IRegistration>
{ {
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)
@ -47,7 +47,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{ {
foreach (var registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
if (registration is IMultitonRegistration<TInterface2, TImplementation> interface2Registration) if (registration is IMultitonRegistration<TInterface2, TImplementation> interface2Registration)
interface2Registration.OnCreate(action); interface2Registration.OnCreate(action);

@ -68,7 +68,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{ {
foreach (var registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
if (registration is ITypedRegistration<TInterface2, TImplementation> interface2Registration) if (registration is ITypedRegistration<TInterface2, TImplementation> interface2Registration)
interface2Registration.OnCreate(action); interface2Registration.OnCreate(action);
@ -116,7 +116,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{ {
foreach (var registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
if (registration is ITypedRegistration<TInterface3, TImplementation> interface3Registration) if (registration is ITypedRegistration<TInterface3, TImplementation> interface3Registration)
interface3Registration.OnCreate(action); interface3Registration.OnCreate(action);
@ -169,7 +169,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{ {
foreach (var registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
if (registration is ITypedRegistration<TInterface4, TImplementation> interface4Registration) if (registration is ITypedRegistration<TInterface4, TImplementation> interface4Registration)
interface4Registration.OnCreate(action); interface4Registration.OnCreate(action);
@ -227,7 +227,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns> /// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action) public override ITypedRegistration<TInterface1, TImplementation> OnCreate(Action<TImplementation> action)
{ {
foreach (var registration in Registrations) foreach (IRegistration registration in Registrations)
{ {
if (registration is ITypedRegistration<TInterface5, TImplementation> interface5Registration) if (registration is ITypedRegistration<TInterface5, TImplementation> interface5Registration)
interface5Registration.OnCreate(action); interface5Registration.OnCreate(action);

@ -5,6 +5,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Interfaces.Registrations.Fluent; using LightweightIocContainer.Interfaces.Registrations.Fluent;
namespace LightweightIocContainer.Validation namespace LightweightIocContainer.Validation
@ -43,7 +44,7 @@ namespace LightweightIocContainer.Validation
{ {
List<Exception> validationExceptions = new(); List<Exception> validationExceptions = new();
foreach (var registration in _iocContainer.Registrations) foreach (IRegistration registration in _iocContainer.Registrations)
{ {
if (registration is IWithFactory { Factory: { } } withFactoryRegistration) if (registration is IWithFactory { Factory: { } } withFactoryRegistration)
{ {

@ -35,18 +35,18 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestInstall() public void TestInstall()
{ {
List<Type> types = new List<Type> List<Type> types = new()
{ {
typeof(object), typeof(object),
typeof(TestInstaller) typeof(TestInstaller)
}; };
Mock<AssemblyWrapper> assemblyMock = new Mock<AssemblyWrapper>(); Mock<AssemblyWrapper> assemblyMock = new();
assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray); assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray);
Mock<IIocContainer> iocContainerMock = new Mock<IIocContainer>(); Mock<IIocContainer> iocContainerMock = new();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(assemblyMock.Object); AssemblyInstaller assemblyInstaller = new(assemblyMock.Object);
assemblyInstaller.Install(iocContainerMock.Object); assemblyInstaller.Install(iocContainerMock.Object);
iocContainerMock.Verify(ioc => ioc.Register<It.IsSubtype<Mock<IRegistration>>>(It.IsAny<Lifestyle>()), Times.Once); iocContainerMock.Verify(ioc => ioc.Register<It.IsSubtype<Mock<IRegistration>>>(It.IsAny<Lifestyle>()), Times.Once);
@ -62,13 +62,13 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestFromAssemblyInstance() public void TestFromAssemblyInstance()
{ {
List<Type> types = new List<Type> List<Type> types = new()
{ {
typeof(object), typeof(object),
typeof(TestInstaller) typeof(TestInstaller)
}; };
Mock<AssemblyWrapper> assemblyMock = new Mock<AssemblyWrapper>(); Mock<AssemblyWrapper> assemblyMock = new();
assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray); assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray);
IIocContainer iocContainer = new IocContainer(); IIocContainer iocContainer = new IocContainer();

@ -27,19 +27,19 @@ namespace Test.LightweightIocContainer
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public void TestFirstOrGivenNoPredicateEmpty() public void TestFirstOrGivenNoPredicateEmpty()
{ {
List<ListObject> list = new List<ListObject>(); List<ListObject> list = new();
Assert.IsInstanceOf<Given>(list.FirstOrGiven<ListObject, Given>()); Assert.IsInstanceOf<Given>(list.FirstOrGiven<ListObject, Given>());
} }
[Test] [Test]
public void TestFirstOrGivenNoPredicate() public void TestFirstOrGivenNoPredicate()
{ {
List<ListObject> list = new List<ListObject>() List<ListObject> list = new()
{ {
new ListObject() {Index = 0}, new ListObject {Index = 0},
new ListObject() {Index = 1}, new ListObject {Index = 1},
new ListObject() {Index = 2}, new ListObject {Index = 2},
new ListObject() {Index = 3} new ListObject {Index = 3}
}; };
ListObject listObject = list.FirstOrGiven<ListObject, Given>(); ListObject listObject = list.FirstOrGiven<ListObject, Given>();
@ -52,19 +52,19 @@ namespace Test.LightweightIocContainer
[SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
public void TestFirstOrGivenPredicateEmpty() public void TestFirstOrGivenPredicateEmpty()
{ {
List<ListObject> list = new List<ListObject>(); List<ListObject> list = new();
Assert.IsInstanceOf<Given>(list.FirstOrGiven<ListObject, Given>(o => o.Index == 2)); Assert.IsInstanceOf<Given>(list.FirstOrGiven<ListObject, Given>(o => o.Index == 2));
} }
[Test] [Test]
public void TestFirstOrGivenPredicate() public void TestFirstOrGivenPredicate()
{ {
List<ListObject> list = new List<ListObject>() List<ListObject> list = new()
{ {
new ListObject() {Index = 0}, new ListObject {Index = 0},
new ListObject() {Index = 1}, new ListObject {Index = 1},
new ListObject() {Index = 2}, new ListObject {Index = 2},
new ListObject() {Index = 3} new ListObject {Index = 3}
}; };
ListObject listObject = list.FirstOrGiven<ListObject, Given>(o => o.Index == 2); ListObject listObject = list.FirstOrGiven<ListObject, Given>(o => o.Index == 2);

@ -185,8 +185,8 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.RegisterMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>(); _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>();
MultitonScope scope1 = new MultitonScope(); MultitonScope scope1 = new();
MultitonScope scope2 = new MultitonScope(); MultitonScope scope2 = new();
ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>(); ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>();
@ -204,8 +204,8 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.RegisterMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>(); _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>().WithFactory<ITestFactory>();
MultitonScope scope1 = new MultitonScope(); MultitonScope scope1 = new();
MultitonScope scope2 = new MultitonScope(); MultitonScope scope2 = new();
ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>(); ITestFactory testFactory = _iocContainer.Resolve<ITestFactory>();

@ -58,10 +58,10 @@ namespace Test.LightweightIocContainer
_container.Register<IBar, IFoo, Foo>().OnCreate(foo => foo.ThrowFoo()); _container.Register<IBar, IFoo, Foo>().OnCreate(foo => foo.ThrowFoo());
Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>()); Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>());
Assert.AreEqual("Foo", fooException.Message); Assert.AreEqual("Foo", fooException?.Message);
Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>()); Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>());
Assert.AreEqual("Foo", barException.Message); Assert.AreEqual("Foo", barException?.Message);
} }
[Test] [Test]
@ -70,13 +70,13 @@ namespace Test.LightweightIocContainer
_container.Register<IBar, IFoo, IAnotherFoo, Foo>().OnCreate(foo => foo.ThrowFoo()); _container.Register<IBar, IFoo, IAnotherFoo, Foo>().OnCreate(foo => foo.ThrowFoo());
Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>()); Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>());
Assert.AreEqual("Foo", fooException.Message); Assert.AreEqual("Foo", fooException?.Message);
Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>()); Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>());
Assert.AreEqual("Foo", barException.Message); Assert.AreEqual("Foo", barException?.Message);
Exception anotherFooException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherFoo>()); Exception anotherFooException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherFoo>());
Assert.AreEqual("Foo", anotherFooException.Message); Assert.AreEqual("Foo", anotherFooException?.Message);
} }
[Test] [Test]
@ -85,16 +85,16 @@ namespace Test.LightweightIocContainer
_container.Register<IBar, IFoo, IAnotherFoo, IAnotherBar, Foo>().OnCreate(foo => foo.ThrowFoo()); _container.Register<IBar, IFoo, IAnotherFoo, IAnotherBar, Foo>().OnCreate(foo => foo.ThrowFoo());
Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>()); Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>());
Assert.AreEqual("Foo", fooException.Message); Assert.AreEqual("Foo", fooException?.Message);
Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>()); Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>());
Assert.AreEqual("Foo", barException.Message); Assert.AreEqual("Foo", barException?.Message);
Exception anotherFooException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherFoo>()); Exception anotherFooException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherFoo>());
Assert.AreEqual("Foo", anotherFooException.Message); Assert.AreEqual("Foo", anotherFooException?.Message);
Exception anotherBarException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherBar>()); Exception anotherBarException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherBar>());
Assert.AreEqual("Foo", anotherBarException.Message); Assert.AreEqual("Foo", anotherBarException?.Message);
} }
[Test] [Test]
@ -103,19 +103,19 @@ namespace Test.LightweightIocContainer
_container.Register<IBar, IFoo, IAnotherFoo, IAnotherBar, IAnotherOne, Foo>().OnCreate(foo => foo.ThrowFoo()); _container.Register<IBar, IFoo, IAnotherFoo, IAnotherBar, IAnotherOne, Foo>().OnCreate(foo => foo.ThrowFoo());
Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>()); Exception fooException = Assert.Throws<Exception>(() => _container.Resolve<IFoo>());
Assert.AreEqual("Foo", fooException.Message); Assert.AreEqual("Foo", fooException?.Message);
Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>()); Exception barException = Assert.Throws<Exception>(() => _container.Resolve<IBar>());
Assert.AreEqual("Foo", barException.Message); Assert.AreEqual("Foo", barException?.Message);
Exception anotherFooException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherFoo>()); Exception anotherFooException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherFoo>());
Assert.AreEqual("Foo", anotherFooException.Message); Assert.AreEqual("Foo", anotherFooException?.Message);
Exception anotherBarException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherBar>()); Exception anotherBarException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherBar>());
Assert.AreEqual("Foo", anotherBarException.Message); Assert.AreEqual("Foo", anotherBarException?.Message);
Exception anotherOneException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherOne>()); Exception anotherOneException = Assert.Throws<Exception>(() => _container.Resolve<IAnotherOne>());
Assert.AreEqual("Foo", anotherOneException.Message); Assert.AreEqual("Foo", anotherOneException?.Message);
} }
[Test] [Test]

@ -100,7 +100,7 @@ namespace Test.LightweightIocContainer
_iocContainer.Register<IBar, Bar>(); _iocContainer.Register<IBar, Bar>();
CircularDependencyException exception = Assert.Throws<CircularDependencyException>(() => _iocContainer.Resolve<IFoo>()); CircularDependencyException exception = Assert.Throws<CircularDependencyException>(() => _iocContainer.Resolve<IFoo>());
Assert.AreEqual(typeof(IFoo), exception.ResolvingType); Assert.AreEqual(typeof(IFoo), exception?.ResolvingType);
Assert.AreEqual(2, exception.ResolveStack.Count); Assert.AreEqual(2, exception.ResolveStack.Count);
string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n" + string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n" +
@ -115,7 +115,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestCircularDependencyExceptionNoStack() public void TestCircularDependencyExceptionNoStack()
{ {
CircularDependencyException circularDependencyException = new CircularDependencyException(typeof(IFoo), null); CircularDependencyException circularDependencyException = new(typeof(IFoo), null);
string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n"; string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n";
Assert.AreEqual(message, circularDependencyException.Message); Assert.AreEqual(message, circularDependencyException.Message);
} }

@ -92,7 +92,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestInstall() public void TestInstall()
{ {
Mock<IIocInstaller> installerMock = new Mock<IIocInstaller>(); Mock<IIocInstaller> installerMock = new();
IIocContainer returnedContainer = _iocContainer.Install(installerMock.Object); IIocContainer returnedContainer = _iocContainer.Install(installerMock.Object);
installerMock.Verify(m => m.Install(It.IsAny<IIocContainer>()), Times.Once); installerMock.Verify(m => m.Install(It.IsAny<IIocContainer>()), Times.Once);
@ -103,9 +103,9 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestInstallMultiple() public void TestInstallMultiple()
{ {
Mock<IIocInstaller> installer1Mock = new Mock<IIocInstaller>(); Mock<IIocInstaller> installer1Mock = new();
Mock<IIocInstaller> installer2Mock = new Mock<IIocInstaller>(); Mock<IIocInstaller> installer2Mock = new();
Mock<IIocInstaller> installer3Mock = new Mock<IIocInstaller>(); Mock<IIocInstaller> installer3Mock = new();
IIocContainer returnedContainer = _iocContainer.Install(installer1Mock.Object, installer2Mock.Object, installer3Mock.Object); IIocContainer returnedContainer = _iocContainer.Install(installer1Mock.Object, installer2Mock.Object, installer3Mock.Object);
@ -133,14 +133,14 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.Register<ITest, Test>(); _iocContainer.Register<ITest, Test>();
MultipleRegistrationException exception = Assert.Throws<MultipleRegistrationException>(() => _iocContainer.Register<ITest, TestConstructor>()); MultipleRegistrationException exception = Assert.Throws<MultipleRegistrationException>(() => _iocContainer.Register<ITest, TestConstructor>());
Assert.AreEqual(typeof(ITest), exception.Type); Assert.AreEqual(typeof(ITest), exception?.Type);
} }
[Test] [Test]
public void TestResolveNotRegistered() public void TestResolveNotRegistered()
{ {
TypeNotRegisteredException exception = Assert.Throws<TypeNotRegisteredException>(() => _iocContainer.Resolve<ITest>()); TypeNotRegisteredException exception = Assert.Throws<TypeNotRegisteredException>(() => _iocContainer.Resolve<ITest>());
Assert.AreEqual(typeof(ITest), exception.Type); Assert.AreEqual(typeof(ITest), exception?.Type);
} }
[Test] [Test]
@ -217,8 +217,8 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.RegisterMultiton<ITest, Test, MultitonScope>(); _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>();
MultitonScope scope1 = new MultitonScope(); MultitonScope scope1 = new();
MultitonScope scope2 = new MultitonScope(); MultitonScope scope2 = new();
ITest resolvedTest1 = _iocContainer.Resolve<ITest>(scope1); ITest resolvedTest1 = _iocContainer.Resolve<ITest>(scope1);
ITest resolvedTest2 = _iocContainer.Resolve<ITest>(scope1); ITest resolvedTest2 = _iocContainer.Resolve<ITest>(scope1);
@ -235,7 +235,7 @@ namespace Test.LightweightIocContainer
_iocContainer.RegisterMultiton<ITest, Test, MultitonScope>(); _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>();
MultitonResolveException exception = Assert.Throws<MultitonResolveException>(() => _iocContainer.Resolve<ITest>()); MultitonResolveException exception = Assert.Throws<MultitonResolveException>(() => _iocContainer.Resolve<ITest>());
Assert.AreEqual(typeof(ITest), exception.Type); Assert.AreEqual(typeof(ITest), exception?.Type);
} }
[Test] [Test]
@ -244,7 +244,7 @@ namespace Test.LightweightIocContainer
_iocContainer.RegisterMultiton<ITest, Test, MultitonScope>(); _iocContainer.RegisterMultiton<ITest, Test, MultitonScope>();
MultitonResolveException exception = Assert.Throws<MultitonResolveException>(() => _iocContainer.Resolve<ITest>(new object())); MultitonResolveException exception = Assert.Throws<MultitonResolveException>(() => _iocContainer.Resolve<ITest>(new object()));
Assert.AreEqual(typeof(ITest), exception.Type); Assert.AreEqual(typeof(ITest), exception?.Type);
} }
[Test] [Test]
@ -263,7 +263,7 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.Register<ITest, TestConstructor>(); _iocContainer.Register<ITest, TestConstructor>();
NoMatchingConstructorFoundException exception = Assert.Throws<NoMatchingConstructorFoundException>(() => _iocContainer.Resolve<ITest>()); NoMatchingConstructorFoundException exception = Assert.Throws<NoMatchingConstructorFoundException>(() => _iocContainer.Resolve<ITest>());
Assert.AreEqual(typeof(TestConstructor), exception.Type); Assert.AreEqual(typeof(TestConstructor), exception?.Type);
} }
[Test] [Test]
@ -278,7 +278,7 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.Register<ITest, TestPrivateConstructor>(); _iocContainer.Register<ITest, TestPrivateConstructor>();
NoPublicConstructorFoundException exception = Assert.Throws<NoPublicConstructorFoundException>(() => _iocContainer.Resolve<ITest>()); NoPublicConstructorFoundException exception = Assert.Throws<NoPublicConstructorFoundException>(() => _iocContainer.Resolve<ITest>());
Assert.AreEqual(typeof(TestPrivateConstructor), exception.Type); Assert.AreEqual(typeof(TestPrivateConstructor), exception?.Type);
} }
[Test] [Test]

@ -51,7 +51,7 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.RegisterMultiton<IProvider, ITest, Test, MultitonScope>(); _iocContainer.RegisterMultiton<IProvider, ITest, Test, MultitonScope>();
MultitonScope scope = new MultitonScope(); MultitonScope scope = new();
ITest test = _iocContainer.Resolve<ITest>(scope); ITest test = _iocContainer.Resolve<ITest>(scope);
Assert.NotNull(test); Assert.NotNull(test);
@ -67,8 +67,8 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.RegisterMultiton<IProvider, ITest, Test, MultitonScope>(); _iocContainer.RegisterMultiton<IProvider, ITest, Test, MultitonScope>();
MultitonScope scope = new MultitonScope(); MultitonScope scope = new();
MultitonScope differentScope = new MultitonScope(); MultitonScope differentScope = new();
ITest test = _iocContainer.Resolve<ITest>(scope); ITest test = _iocContainer.Resolve<ITest>(scope);
Assert.NotNull(test); Assert.NotNull(test);
@ -85,7 +85,7 @@ namespace Test.LightweightIocContainer
{ {
_iocContainer.RegisterMultiton<IProvider, ITest, Test, MultitonScope>().OnCreate(t => t.DoSomething(1)); _iocContainer.RegisterMultiton<IProvider, ITest, Test, MultitonScope>().OnCreate(t => t.DoSomething(1));
MultitonScope scope = new MultitonScope(); MultitonScope scope = new();
ITest test = _iocContainer.Resolve<ITest>(scope); ITest test = _iocContainer.Resolve<ITest>(scope);
Assert.NotNull(test); Assert.NotNull(test);

@ -4,7 +4,6 @@
using System; using System;
using LightweightIocContainer; using LightweightIocContainer;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Registrations; using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Registrations; using LightweightIocContainer.Registrations;
using Moq; using Moq;
@ -29,7 +28,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestOnCreate() public void TestOnCreate()
{ {
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object); RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
ITypedRegistration<ITest, Test> testRegistration = registrationFactory.Register<ITest, Test>(Lifestyle.Transient).OnCreate(t => t.DoSomething()); ITypedRegistration<ITest, Test> testRegistration = registrationFactory.Register<ITest, Test>(Lifestyle.Transient).OnCreate(t => t.DoSomething());
Test test = new Test(); Test test = new Test();

@ -53,7 +53,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestWithParameters() public void TestWithParameters()
{ {
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object); RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
IBar bar = new Bar(); IBar bar = new Bar();
ITest test = new Test(); ITest test = new Test();
@ -67,7 +67,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestWithParametersDifferentOrder() public void TestWithParametersDifferentOrder()
{ {
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object); RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
IBar bar = new Bar(); IBar bar = new Bar();
ITest test = new Test(); ITest test = new Test();
@ -83,7 +83,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestWithParametersCalledTwice() public void TestWithParametersCalledTwice()
{ {
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object); RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(new Bar()).WithParameters(new Test())); Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(new Bar()).WithParameters(new Test()));
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((0, new Bar())).WithParameters((1, new Test()))); Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((0, new Bar())).WithParameters((1, new Test())));
} }
@ -91,7 +91,7 @@ namespace Test.LightweightIocContainer
[Test] [Test]
public void TestWithParametersNoParametersGiven() public void TestWithParametersNoParametersGiven()
{ {
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object); RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((object[])null)); Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters((object[])null));
Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(((int index, object parameter)[])null)); Assert.Throws<InvalidRegistrationException>(() => registrationFactory.Register<IFoo, Foo>(Lifestyle.Transient).WithParameters(((int index, object parameter)[])null));
} }

@ -48,7 +48,7 @@ namespace Test.LightweightIocContainer
Mock<IocContainer> iocContainerMock = new(); Mock<IocContainer> iocContainerMock = new();
iocContainerMock.Setup(c => c.Resolve<IBar>()).Returns(bar); iocContainerMock.Setup(c => c.Resolve<IBar>()).Returns(bar);
RegistrationFactory registrationFactory = new RegistrationFactory(iocContainerMock.Object); RegistrationFactory registrationFactory = new(iocContainerMock.Object);
ISingleTypeRegistration<IFoo> registration = registrationFactory.Register<IFoo>(Lifestyle.Transient).WithFactoryMethod(c => new Foo(c.Resolve<IBar>())); ISingleTypeRegistration<IFoo> registration = registrationFactory.Register<IFoo>(Lifestyle.Transient).WithFactoryMethod(c => new Foo(c.Resolve<IBar>()));
IFoo foo = registration.FactoryMethod(iocContainerMock.Object); IFoo foo = registration.FactoryMethod(iocContainerMock.Object);
@ -61,7 +61,7 @@ namespace Test.LightweightIocContainer
IIocContainer container = new IocContainer(); IIocContainer container = new IocContainer();
IBar bar = new Bar(); IBar bar = new Bar();
container.Register<IFoo>(Lifestyle.Singleton).WithFactoryMethod(c => new Foo(bar)); container.Register<IFoo>(Lifestyle.Singleton).WithFactoryMethod(_ => new Foo(bar));
IFoo foo = container.Resolve<IFoo>(); IFoo foo = container.Resolve<IFoo>();

Loading…
Cancel
Save