- 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
{
TSource first;
if (predicate == null)
first = source.First();
else
first = source.First(predicate);
return first;
return predicate == null ? source.First() : source.First(predicate);
}
catch (Exception)
{

@ -44,7 +44,7 @@ namespace LightweightIocContainer.Exceptions
{
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())
return message.ToString();
@ -52,9 +52,7 @@ namespace LightweightIocContainer.Exceptions
message.Append($"\t`{ResolvingType}` resolved as dependency of\n");
for (int i = ResolveStack.Count - 1; i >= 1 ; i--)
{
message.Append($"\t`{ResolveStack[i]}` resolved as dependency of\n");
}
message.Append($"\t`{ResolveStack[0]}` which is the root type being resolved.");
return message.ToString();

@ -38,7 +38,7 @@ namespace LightweightIocContainer.Exceptions
/// <param name="innerException">The inner <see cref="Exception"/></param>
protected IocContainerException(string message, Exception innerException)
: base(message, innerException) =>
InnerExceptions = new List<Exception>() {innerException};
InnerExceptions = new List<Exception> {innerException};
/// <summary>
/// The inner exceptions of the <see cref="IocContainerException"/>

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

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

@ -36,8 +36,7 @@ namespace LightweightIocContainer.Interfaces
/// <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>
/// <returns>The created <see cref="IRegistration"/></returns>
IOpenGenericRegistration RegisterOpenGenerics(Type tInterface, Type tImplementation,
Lifestyle lifestyle = Lifestyle.Transient);
IOpenGenericRegistration RegisterOpenGenerics(Type tInterface, Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient);
/// <summary>
/// Register multiple interfaces for a <see cref="Type"/> that implements them

@ -25,8 +25,8 @@ namespace LightweightIocContainer
{
private readonly RegistrationFactory _registrationFactory;
private readonly List<(Type type, object instance)> _singletons = new List<(Type, object)>();
private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object> instances)> _multitons = new List<(Type, Type, ConditionalWeakTable<object, object>)>();
private readonly List<(Type type, object instance)> _singletons = new();
private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object> instances)> _multitons = new();
/// <summary>
/// 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)
{
foreach (IIocInstaller installer in installers)
{
installer.Install(this);
}
return this;
}
@ -119,8 +117,7 @@ namespace LightweightIocContainer
/// <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
{
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> multipleRegistration =
_registrationFactory.Register<TInterface1, TInterface2, TInterface3, TImplementation>(lifestyle);
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TImplementation> multipleRegistration = _registrationFactory.Register<TInterface1, TInterface2, TInterface3, TImplementation>(lifestyle);
Register(multipleRegistration);
return multipleRegistration;
@ -138,8 +135,7 @@ namespace LightweightIocContainer
/// <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
{
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> multipleRegistration =
_registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(lifestyle);
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation> multipleRegistration = _registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TImplementation>(lifestyle);
Register(multipleRegistration);
return multipleRegistration;
@ -158,8 +154,7 @@ namespace LightweightIocContainer
/// <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
{
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> multipleRegistration =
_registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(lifestyle);
IMultipleRegistration<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation> multipleRegistration = _registrationFactory.Register<TInterface1, TInterface2, TInterface3, TInterface4, TInterface5, TImplementation>(lifestyle);
Register(multipleRegistration);
return multipleRegistration;
@ -243,10 +238,8 @@ namespace LightweightIocContainer
/// <param name="multipleRegistration">The <see cref="IMultipleRegistration{TInterface1,TImplementation}"/></param>
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);
}
}
/// <summary>
@ -298,23 +291,14 @@ namespace LightweightIocContainer
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
T resolvedInstance;
if (registration is IRegistrationBase defaultRegistration)
{
if (defaultRegistration.Lifestyle == Lifestyle.Singleton)
resolvedInstance = GetOrCreateSingletonInstance<T>(defaultRegistration, arguments, resolveStack);
else if (defaultRegistration is IMultitonRegistration multitonRegistration && defaultRegistration.Lifestyle == Lifestyle.Multiton)
resolvedInstance = GetOrCreateMultitonInstance<T>(multitonRegistration, arguments, resolveStack);
else
resolvedInstance = CreateInstance<T>(defaultRegistration, arguments, resolveStack);
}
else if (registration is ITypedFactoryRegistration<T> typedFactoryRegistration)
T resolvedInstance = registration switch
{
resolvedInstance = typedFactoryRegistration.Factory.Factory;
}
else
throw new UnknownRegistrationException($"There is no registration of type {registration.GetType().Name}.");
IRegistrationBase { Lifestyle: Lifestyle.Singleton } defaultRegistration => GetOrCreateSingletonInstance<T>(defaultRegistration, arguments, resolveStack),
IRegistrationBase { Lifestyle: Lifestyle.Multiton } and IMultitonRegistration multitonRegistration => GetOrCreateMultitonInstance<T>(multitonRegistration, arguments, resolveStack),
IRegistrationBase defaultRegistration => CreateInstance<T>(defaultRegistration, arguments, resolveStack),
ITypedFactoryRegistration<T> typedFactoryRegistration => typedFactoryRegistration.Factory.Factory,
_ => 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
@ -331,13 +315,12 @@ namespace LightweightIocContainer
/// <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)
{
Type type;
if (registration is ITypedRegistration typedRegistration)
type = typedRegistration.ImplementationType;
else if (registration is ISingleTypeRegistration<T> singleTypeRegistration)
type = singleTypeRegistration.InterfaceType;
else
throw new UnknownRegistrationException($"There is no registration {registration.GetType().Name} that can have lifestyle singleton.");
Type type = registration switch
{
ITypedRegistration typedRegistration => typedRegistration.ImplementationType,
ISingleTypeRegistration<T> singleTypeRegistration => singleTypeRegistration.InterfaceType,
_ => throw new UnknownRegistrationException($"There is no registration {registration.GetType().Name} that can have lifestyle singleton.")
};
//if a singleton instance exists return it
object instance = _singletons.FirstOrDefault(s => s.type == type).instance;
@ -385,7 +368,7 @@ namespace LightweightIocContainer
T newInstance = CreateInstance<T>(registration, arguments, resolveStack);
ConditionalWeakTable<object, object> weakTable = new ConditionalWeakTable<object, object>();
ConditionalWeakTable<object, object> weakTable = new();
weakTable.Add(scopeArgument, newInstance);
_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
{
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;
continue;
@ -509,7 +492,7 @@ namespace LightweightIocContainer
try
{
List<object> argumentsList = arguments?.ToList();
List<object> ctorParams = new List<object>();
List<object> ctorParams = new();
ParameterInfo[] parameters = ctor.GetParameters();
foreach (ParameterInfo parameter in parameters)
@ -519,7 +502,8 @@ namespace LightweightIocContainer
{
fittingArgument = argumentsList.FirstOrGiven<object, InternalResolvePlaceholder>(a =>
a?.GetType() == parameter.ParameterType || parameter.ParameterType.IsInstanceOfType(a));
if (!(fittingArgument is InternalResolvePlaceholder))
if (fittingArgument is not InternalResolvePlaceholder)
{
int index = argumentsList.IndexOf(fittingArgument);
argumentsList[index] = new InternalResolvePlaceholder();
@ -534,7 +518,7 @@ namespace LightweightIocContainer
{
fittingArgument = argumentsList.FirstOrGiven<object, InternalResolvePlaceholder>(a => parameter.ParameterType.GetDefault() == a);
if (!(fittingArgument is InternalResolvePlaceholder))
if (fittingArgument is not InternalResolvePlaceholder)
{
int index = argumentsList.IndexOf(fittingArgument);
argumentsList[index] = new InternalResolvePlaceholder();
@ -586,10 +570,7 @@ namespace LightweightIocContainer
return null;
List<IRegistration> openGenericRegistrations = Registrations.Where(r => r.InterfaceType.ContainsGenericParameters).ToList();
if (!openGenericRegistrations.Any())
return null;
return openGenericRegistrations.FirstOrDefault(r => r.InterfaceType == typeof(T).GetGenericTypeDefinition());
return !openGenericRegistrations.Any() ? null : openGenericRegistrations.FirstOrDefault(r => r.InterfaceType == typeof(T).GetGenericTypeDefinition());
}
/// <summary>

@ -28,7 +28,7 @@ namespace LightweightIocContainer.Registrations
public MultipleMultitonRegistration(Type interfaceType1, Type interfaceType2, Type implementationType, Type scope, IocContainer container)
: base(interfaceType1, implementationType, scope, container)
{
Registrations = new List<IRegistration>()
Registrations = new List<IRegistration>
{
new MultitonRegistration<TInterface1, TImplementation>(interfaceType1, 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>
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)
interface2Registration.OnCreate(action);

@ -68,7 +68,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
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)
interface2Registration.OnCreate(action);
@ -116,7 +116,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
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)
interface3Registration.OnCreate(action);
@ -169,7 +169,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
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)
interface4Registration.OnCreate(action);
@ -227,7 +227,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="ITypedRegistration{TInterface,TImplementation}"/></returns>
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)
interface5Registration.OnCreate(action);

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

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

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

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

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

@ -100,7 +100,7 @@ namespace Test.LightweightIocContainer
_iocContainer.Register<IBar, Bar>();
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);
string message = $"Circular dependency has been detected when trying to resolve `{typeof(IFoo)}`.\n" +
@ -115,7 +115,7 @@ namespace Test.LightweightIocContainer
[Test]
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";
Assert.AreEqual(message, circularDependencyException.Message);
}

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

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

@ -4,7 +4,6 @@
using System;
using LightweightIocContainer;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Registrations;
using Moq;
@ -29,7 +28,7 @@ namespace Test.LightweightIocContainer
[Test]
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());
Test test = new Test();

@ -53,7 +53,7 @@ namespace Test.LightweightIocContainer
[Test]
public void TestWithParameters()
{
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
IBar bar = new Bar();
ITest test = new Test();
@ -67,7 +67,7 @@ namespace Test.LightweightIocContainer
[Test]
public void TestWithParametersDifferentOrder()
{
RegistrationFactory registrationFactory = new RegistrationFactory(new Mock<IocContainer>().Object);
RegistrationFactory registrationFactory = new(new Mock<IocContainer>().Object);
IBar bar = new Bar();
ITest test = new Test();
@ -83,7 +83,7 @@ namespace Test.LightweightIocContainer
[Test]
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((0, new Bar())).WithParameters((1, new Test())));
}
@ -91,7 +91,7 @@ namespace Test.LightweightIocContainer
[Test]
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(((int index, object parameter)[])null));
}

@ -48,7 +48,7 @@ namespace Test.LightweightIocContainer
Mock<IocContainer> iocContainerMock = new();
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>()));
IFoo foo = registration.FactoryMethod(iocContainerMock.Object);
@ -61,7 +61,7 @@ namespace Test.LightweightIocContainer
IIocContainer container = new IocContainer();
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>();

Loading…
Cancel
Save