diff --git a/LightweightIocContainer/EnumerableExtension.cs b/LightweightIocContainer/EnumerableExtension.cs index efd7c62..b2bd14f 100644 --- a/LightweightIocContainer/EnumerableExtension.cs +++ b/LightweightIocContainer/EnumerableExtension.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) { diff --git a/LightweightIocContainer/Exceptions/CircularDependencyException.cs b/LightweightIocContainer/Exceptions/CircularDependencyException.cs index 95f842e..9047db7 100644 --- a/LightweightIocContainer/Exceptions/CircularDependencyException.cs +++ b/LightweightIocContainer/Exceptions/CircularDependencyException.cs @@ -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(); diff --git a/LightweightIocContainer/Exceptions/IocContainerException.cs b/LightweightIocContainer/Exceptions/IocContainerException.cs index 2023355..dada45e 100644 --- a/LightweightIocContainer/Exceptions/IocContainerException.cs +++ b/LightweightIocContainer/Exceptions/IocContainerException.cs @@ -38,7 +38,7 @@ namespace LightweightIocContainer.Exceptions /// The inner protected IocContainerException(string message, Exception innerException) : base(message, innerException) => - InnerExceptions = new List() {innerException}; + InnerExceptions = new List {innerException}; /// /// The inner exceptions of the diff --git a/LightweightIocContainer/Factories/TypedFactory.cs b/LightweightIocContainer/Factories/TypedFactory.cs index 90808c3..c68cb23 100644 --- a/LightweightIocContainer/Factories/TypedFactory.cs +++ b/LightweightIocContainer/Factories/TypedFactory.cs @@ -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; diff --git a/LightweightIocContainer/Installers/AssemblyInstaller.cs b/LightweightIocContainer/Installers/AssemblyInstaller.cs index 89a1e6f..af63a20 100644 --- a/LightweightIocContainer/Installers/AssemblyInstaller.cs +++ b/LightweightIocContainer/Installers/AssemblyInstaller.cs @@ -45,9 +45,7 @@ namespace LightweightIocContainer.Installers public void Install(IIocContainer container) { foreach (IIocInstaller installer in Installers) - { installer.Install(container); - } } } } \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/IIocContainer.cs b/LightweightIocContainer/Interfaces/IIocContainer.cs index 802184c..141cdfb 100644 --- a/LightweightIocContainer/Interfaces/IIocContainer.cs +++ b/LightweightIocContainer/Interfaces/IIocContainer.cs @@ -36,8 +36,7 @@ namespace LightweightIocContainer.Interfaces /// The open generic Type that implements the interface /// The for this /// The created - IOpenGenericRegistration RegisterOpenGenerics(Type tInterface, Type tImplementation, - Lifestyle lifestyle = Lifestyle.Transient); + IOpenGenericRegistration RegisterOpenGenerics(Type tInterface, Type tImplementation, Lifestyle lifestyle = Lifestyle.Transient); /// /// Register multiple interfaces for a that implements them diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 4c47991..60ed864 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -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 instances)> _multitons = new List<(Type, Type, ConditionalWeakTable)>(); + private readonly List<(Type type, object instance)> _singletons = new(); + private readonly List<(Type type, Type scope, ConditionalWeakTable instances)> _multitons = new(); /// /// The main container that carries all the 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 /// The created public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface3, TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = - _registrationFactory.Register(lifestyle); + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; @@ -138,8 +135,7 @@ namespace LightweightIocContainer /// The created public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface4, TInterface3, TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = - _registrationFactory.Register(lifestyle); + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; @@ -158,8 +154,7 @@ namespace LightweightIocContainer /// The created public IMultipleRegistration Register(Lifestyle lifestyle = Lifestyle.Transient) where TImplementation : TInterface5, TInterface4, TInterface3, TInterface2, TInterface1 { - IMultipleRegistration multipleRegistration = - _registrationFactory.Register(lifestyle); + IMultipleRegistration multipleRegistration = _registrationFactory.Register(lifestyle); Register(multipleRegistration); return multipleRegistration; @@ -243,10 +238,8 @@ namespace LightweightIocContainer /// The private void Register(IMultipleRegistration multipleRegistration) where TImplementation : TInterface1 { - foreach (var registration in multipleRegistration.Registrations) - { + foreach (IRegistration registration in multipleRegistration.Registrations) Register(registration); - } } /// @@ -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(defaultRegistration, arguments, resolveStack); - else if (defaultRegistration is IMultitonRegistration multitonRegistration && defaultRegistration.Lifestyle == Lifestyle.Multiton) - resolvedInstance = GetOrCreateMultitonInstance(multitonRegistration, arguments, resolveStack); - else - resolvedInstance = CreateInstance(defaultRegistration, arguments, resolveStack); - } - else if (registration is ITypedFactoryRegistration 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(defaultRegistration, arguments, resolveStack), + IRegistrationBase { Lifestyle: Lifestyle.Multiton } and IMultitonRegistration multitonRegistration => GetOrCreateMultitonInstance(multitonRegistration, arguments, resolveStack), + IRegistrationBase defaultRegistration => CreateInstance(defaultRegistration, arguments, resolveStack), + ITypedFactoryRegistration 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 /// An existing or newly created singleton instance of the given private T GetOrCreateSingletonInstance(IRegistration registration, object[] arguments, List resolveStack) { - Type type; - if (registration is ITypedRegistration typedRegistration) - type = typedRegistration.ImplementationType; - else if (registration is ISingleTypeRegistration 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 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(registration, arguments, resolveStack); - ConditionalWeakTable weakTable = new ConditionalWeakTable(); + ConditionalWeakTable 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 argumentsList = arguments?.ToList(); - List ctorParams = new List(); + List ctorParams = new(); ParameterInfo[] parameters = ctor.GetParameters(); foreach (ParameterInfo parameter in parameters) @@ -519,7 +502,8 @@ namespace LightweightIocContainer { fittingArgument = argumentsList.FirstOrGiven(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(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 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()); } /// diff --git a/LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs b/LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs index 7bedc21..2792269 100644 --- a/LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs +++ b/LightweightIocContainer/Registrations/MultipleMultitonRegistration.cs @@ -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() + Registrations = new List { new MultitonRegistration(interfaceType1, implementationType, scope, container), new MultitonRegistration(interfaceType2, implementationType, scope, container) @@ -47,7 +47,7 @@ namespace LightweightIocContainer.Registrations /// The current instance of this public override ITypedRegistration OnCreate(Action action) { - foreach (var registration in Registrations) + foreach (IRegistration registration in Registrations) { if (registration is IMultitonRegistration interface2Registration) interface2Registration.OnCreate(action); diff --git a/LightweightIocContainer/Registrations/MultipleRegistration.cs b/LightweightIocContainer/Registrations/MultipleRegistration.cs index 002ee0e..7ccb963 100644 --- a/LightweightIocContainer/Registrations/MultipleRegistration.cs +++ b/LightweightIocContainer/Registrations/MultipleRegistration.cs @@ -68,7 +68,7 @@ namespace LightweightIocContainer.Registrations /// The current instance of this public override ITypedRegistration OnCreate(Action action) { - foreach (var registration in Registrations) + foreach (IRegistration registration in Registrations) { if (registration is ITypedRegistration interface2Registration) interface2Registration.OnCreate(action); @@ -116,7 +116,7 @@ namespace LightweightIocContainer.Registrations /// The current instance of this public override ITypedRegistration OnCreate(Action action) { - foreach (var registration in Registrations) + foreach (IRegistration registration in Registrations) { if (registration is ITypedRegistration interface3Registration) interface3Registration.OnCreate(action); @@ -169,7 +169,7 @@ namespace LightweightIocContainer.Registrations /// The current instance of this public override ITypedRegistration OnCreate(Action action) { - foreach (var registration in Registrations) + foreach (IRegistration registration in Registrations) { if (registration is ITypedRegistration interface4Registration) interface4Registration.OnCreate(action); @@ -227,7 +227,7 @@ namespace LightweightIocContainer.Registrations /// The current instance of this public override ITypedRegistration OnCreate(Action action) { - foreach (var registration in Registrations) + foreach (IRegistration registration in Registrations) { if (registration is ITypedRegistration interface5Registration) interface5Registration.OnCreate(action); diff --git a/LightweightIocContainer/Validation/IocValidator.cs b/LightweightIocContainer/Validation/IocValidator.cs index 6c48223..588c4ce 100644 --- a/LightweightIocContainer/Validation/IocValidator.cs +++ b/LightweightIocContainer/Validation/IocValidator.cs @@ -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 validationExceptions = new(); - foreach (var registration in _iocContainer.Registrations) + foreach (IRegistration registration in _iocContainer.Registrations) { if (registration is IWithFactory { Factory: { } } withFactoryRegistration) { diff --git a/Test.LightweightIocContainer/AssemblyInstallerTest.cs b/Test.LightweightIocContainer/AssemblyInstallerTest.cs index cbef0d9..df0a09b 100644 --- a/Test.LightweightIocContainer/AssemblyInstallerTest.cs +++ b/Test.LightweightIocContainer/AssemblyInstallerTest.cs @@ -35,18 +35,18 @@ namespace Test.LightweightIocContainer [Test] public void TestInstall() { - List types = new List + List types = new() { typeof(object), typeof(TestInstaller) }; - Mock assemblyMock = new Mock(); + Mock assemblyMock = new(); assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray); - Mock iocContainerMock = new Mock(); + Mock iocContainerMock = new(); - AssemblyInstaller assemblyInstaller = new AssemblyInstaller(assemblyMock.Object); + AssemblyInstaller assemblyInstaller = new(assemblyMock.Object); assemblyInstaller.Install(iocContainerMock.Object); iocContainerMock.Verify(ioc => ioc.Register>>(It.IsAny()), Times.Once); @@ -62,13 +62,13 @@ namespace Test.LightweightIocContainer [Test] public void TestFromAssemblyInstance() { - List types = new List + List types = new() { typeof(object), typeof(TestInstaller) }; - Mock assemblyMock = new Mock(); + Mock assemblyMock = new(); assemblyMock.Setup(a => a.GetTypes()).Returns(types.ToArray); IIocContainer iocContainer = new IocContainer(); diff --git a/Test.LightweightIocContainer/EnumerableExtensionTest.cs b/Test.LightweightIocContainer/EnumerableExtensionTest.cs index 29dca2b..c4d8578 100644 --- a/Test.LightweightIocContainer/EnumerableExtensionTest.cs +++ b/Test.LightweightIocContainer/EnumerableExtensionTest.cs @@ -27,19 +27,19 @@ namespace Test.LightweightIocContainer [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] public void TestFirstOrGivenNoPredicateEmpty() { - List list = new List(); + List list = new(); Assert.IsInstanceOf(list.FirstOrGiven()); } [Test] public void TestFirstOrGivenNoPredicate() { - List list = new List() + List 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(); @@ -52,19 +52,19 @@ namespace Test.LightweightIocContainer [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")] public void TestFirstOrGivenPredicateEmpty() { - List list = new List(); + List list = new(); Assert.IsInstanceOf(list.FirstOrGiven(o => o.Index == 2)); } [Test] public void TestFirstOrGivenPredicate() { - List list = new List() + List 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(o => o.Index == 2); diff --git a/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs b/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs index 8470dd6..b6236af 100644 --- a/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs +++ b/Test.LightweightIocContainer/FluentFactoryRegistrationTest.cs @@ -185,8 +185,8 @@ namespace Test.LightweightIocContainer { _iocContainer.RegisterMultiton().WithFactory(); - MultitonScope scope1 = new MultitonScope(); - MultitonScope scope2 = new MultitonScope(); + MultitonScope scope1 = new(); + MultitonScope scope2 = new(); ITestFactory testFactory = _iocContainer.Resolve(); @@ -204,8 +204,8 @@ namespace Test.LightweightIocContainer { _iocContainer.RegisterMultiton().WithFactory(); - MultitonScope scope1 = new MultitonScope(); - MultitonScope scope2 = new MultitonScope(); + MultitonScope scope1 = new(); + MultitonScope scope2 = new(); ITestFactory testFactory = _iocContainer.Resolve(); diff --git a/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs b/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs index 485002a..492606a 100644 --- a/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs +++ b/Test.LightweightIocContainer/IocContainerInterfaceSegregationTest.cs @@ -58,10 +58,10 @@ namespace Test.LightweightIocContainer _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", fooException.Message); + Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", barException.Message); + Assert.AreEqual("Foo", barException?.Message); } [Test] @@ -70,13 +70,13 @@ namespace Test.LightweightIocContainer _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", fooException.Message); + Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", barException.Message); + Assert.AreEqual("Foo", barException?.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", anotherFooException.Message); + Assert.AreEqual("Foo", anotherFooException?.Message); } [Test] @@ -85,16 +85,16 @@ namespace Test.LightweightIocContainer _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", fooException.Message); + Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", barException.Message); + Assert.AreEqual("Foo", barException?.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", anotherFooException.Message); + Assert.AreEqual("Foo", anotherFooException?.Message); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", anotherBarException.Message); + Assert.AreEqual("Foo", anotherBarException?.Message); } [Test] @@ -103,19 +103,19 @@ namespace Test.LightweightIocContainer _container.Register().OnCreate(foo => foo.ThrowFoo()); Exception fooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", fooException.Message); + Assert.AreEqual("Foo", fooException?.Message); Exception barException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", barException.Message); + Assert.AreEqual("Foo", barException?.Message); Exception anotherFooException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", anotherFooException.Message); + Assert.AreEqual("Foo", anotherFooException?.Message); Exception anotherBarException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", anotherBarException.Message); + Assert.AreEqual("Foo", anotherBarException?.Message); Exception anotherOneException = Assert.Throws(() => _container.Resolve()); - Assert.AreEqual("Foo", anotherOneException.Message); + Assert.AreEqual("Foo", anotherOneException?.Message); } [Test] diff --git a/Test.LightweightIocContainer/IocContainerRecursionTest.cs b/Test.LightweightIocContainer/IocContainerRecursionTest.cs index d34d3b4..5a5ba61 100644 --- a/Test.LightweightIocContainer/IocContainerRecursionTest.cs +++ b/Test.LightweightIocContainer/IocContainerRecursionTest.cs @@ -100,7 +100,7 @@ namespace Test.LightweightIocContainer _iocContainer.Register(); CircularDependencyException exception = Assert.Throws(() => _iocContainer.Resolve()); - 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); } diff --git a/Test.LightweightIocContainer/IocContainerTest.cs b/Test.LightweightIocContainer/IocContainerTest.cs index dfcbedd..d895855 100644 --- a/Test.LightweightIocContainer/IocContainerTest.cs +++ b/Test.LightweightIocContainer/IocContainerTest.cs @@ -92,7 +92,7 @@ namespace Test.LightweightIocContainer [Test] public void TestInstall() { - Mock installerMock = new Mock(); + Mock installerMock = new(); IIocContainer returnedContainer = _iocContainer.Install(installerMock.Object); installerMock.Verify(m => m.Install(It.IsAny()), Times.Once); @@ -103,9 +103,9 @@ namespace Test.LightweightIocContainer [Test] public void TestInstallMultiple() { - Mock installer1Mock = new Mock(); - Mock installer2Mock = new Mock(); - Mock installer3Mock = new Mock(); + Mock installer1Mock = new(); + Mock installer2Mock = new(); + Mock installer3Mock = new(); IIocContainer returnedContainer = _iocContainer.Install(installer1Mock.Object, installer2Mock.Object, installer3Mock.Object); @@ -133,14 +133,14 @@ namespace Test.LightweightIocContainer { _iocContainer.Register(); MultipleRegistrationException exception = Assert.Throws(() => _iocContainer.Register()); - Assert.AreEqual(typeof(ITest), exception.Type); + Assert.AreEqual(typeof(ITest), exception?.Type); } [Test] public void TestResolveNotRegistered() { TypeNotRegisteredException exception = Assert.Throws(() => _iocContainer.Resolve()); - Assert.AreEqual(typeof(ITest), exception.Type); + Assert.AreEqual(typeof(ITest), exception?.Type); } [Test] @@ -217,8 +217,8 @@ namespace Test.LightweightIocContainer { _iocContainer.RegisterMultiton(); - MultitonScope scope1 = new MultitonScope(); - MultitonScope scope2 = new MultitonScope(); + MultitonScope scope1 = new(); + MultitonScope scope2 = new(); ITest resolvedTest1 = _iocContainer.Resolve(scope1); ITest resolvedTest2 = _iocContainer.Resolve(scope1); @@ -235,7 +235,7 @@ namespace Test.LightweightIocContainer _iocContainer.RegisterMultiton(); MultitonResolveException exception = Assert.Throws(() => _iocContainer.Resolve()); - Assert.AreEqual(typeof(ITest), exception.Type); + Assert.AreEqual(typeof(ITest), exception?.Type); } [Test] @@ -244,7 +244,7 @@ namespace Test.LightweightIocContainer _iocContainer.RegisterMultiton(); MultitonResolveException exception = Assert.Throws(() => _iocContainer.Resolve(new object())); - Assert.AreEqual(typeof(ITest), exception.Type); + Assert.AreEqual(typeof(ITest), exception?.Type); } [Test] @@ -263,7 +263,7 @@ namespace Test.LightweightIocContainer { _iocContainer.Register(); NoMatchingConstructorFoundException exception = Assert.Throws(() => _iocContainer.Resolve()); - Assert.AreEqual(typeof(TestConstructor), exception.Type); + Assert.AreEqual(typeof(TestConstructor), exception?.Type); } [Test] @@ -278,7 +278,7 @@ namespace Test.LightweightIocContainer { _iocContainer.Register(); NoPublicConstructorFoundException exception = Assert.Throws(() => _iocContainer.Resolve()); - Assert.AreEqual(typeof(TestPrivateConstructor), exception.Type); + Assert.AreEqual(typeof(TestPrivateConstructor), exception?.Type); } [Test] diff --git a/Test.LightweightIocContainer/MultipleMultitonRegistrationTest.cs b/Test.LightweightIocContainer/MultipleMultitonRegistrationTest.cs index 59d1188..86b4d56 100644 --- a/Test.LightweightIocContainer/MultipleMultitonRegistrationTest.cs +++ b/Test.LightweightIocContainer/MultipleMultitonRegistrationTest.cs @@ -51,7 +51,7 @@ namespace Test.LightweightIocContainer { _iocContainer.RegisterMultiton(); - MultitonScope scope = new MultitonScope(); + MultitonScope scope = new(); ITest test = _iocContainer.Resolve(scope); Assert.NotNull(test); @@ -67,8 +67,8 @@ namespace Test.LightweightIocContainer { _iocContainer.RegisterMultiton(); - MultitonScope scope = new MultitonScope(); - MultitonScope differentScope = new MultitonScope(); + MultitonScope scope = new(); + MultitonScope differentScope = new(); ITest test = _iocContainer.Resolve(scope); Assert.NotNull(test); @@ -85,7 +85,7 @@ namespace Test.LightweightIocContainer { _iocContainer.RegisterMultiton().OnCreate(t => t.DoSomething(1)); - MultitonScope scope = new MultitonScope(); + MultitonScope scope = new(); ITest test = _iocContainer.Resolve(scope); Assert.NotNull(test); diff --git a/Test.LightweightIocContainer/OnCreateTest.cs b/Test.LightweightIocContainer/OnCreateTest.cs index bc42d49..ef7816c 100644 --- a/Test.LightweightIocContainer/OnCreateTest.cs +++ b/Test.LightweightIocContainer/OnCreateTest.cs @@ -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().Object); + RegistrationFactory registrationFactory = new(new Mock().Object); ITypedRegistration testRegistration = registrationFactory.Register(Lifestyle.Transient).OnCreate(t => t.DoSomething()); Test test = new Test(); diff --git a/Test.LightweightIocContainer/RegistrationBaseTest.cs b/Test.LightweightIocContainer/RegistrationBaseTest.cs index ac2f4cd..cbb7a4f 100644 --- a/Test.LightweightIocContainer/RegistrationBaseTest.cs +++ b/Test.LightweightIocContainer/RegistrationBaseTest.cs @@ -53,7 +53,7 @@ namespace Test.LightweightIocContainer [Test] public void TestWithParameters() { - RegistrationFactory registrationFactory = new RegistrationFactory(new Mock().Object); + RegistrationFactory registrationFactory = new(new Mock().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().Object); + RegistrationFactory registrationFactory = new(new Mock().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().Object); + RegistrationFactory registrationFactory = new(new Mock().Object); Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters(new Bar()).WithParameters(new Test())); Assert.Throws(() => registrationFactory.Register(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().Object); + RegistrationFactory registrationFactory = new(new Mock().Object); Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters((object[])null)); Assert.Throws(() => registrationFactory.Register(Lifestyle.Transient).WithParameters(((int index, object parameter)[])null)); } diff --git a/Test.LightweightIocContainer/SingleTypeRegistrationTest.cs b/Test.LightweightIocContainer/SingleTypeRegistrationTest.cs index da8e87e..907a17f 100644 --- a/Test.LightweightIocContainer/SingleTypeRegistrationTest.cs +++ b/Test.LightweightIocContainer/SingleTypeRegistrationTest.cs @@ -48,7 +48,7 @@ namespace Test.LightweightIocContainer Mock iocContainerMock = new(); iocContainerMock.Setup(c => c.Resolve()).Returns(bar); - RegistrationFactory registrationFactory = new RegistrationFactory(iocContainerMock.Object); + RegistrationFactory registrationFactory = new(iocContainerMock.Object); ISingleTypeRegistration registration = registrationFactory.Register(Lifestyle.Transient).WithFactoryMethod(c => new Foo(c.Resolve())); IFoo foo = registration.FactoryMethod(iocContainerMock.Object); @@ -61,7 +61,7 @@ namespace Test.LightweightIocContainer IIocContainer container = new IocContainer(); IBar bar = new Bar(); - container.Register(Lifestyle.Singleton).WithFactoryMethod(c => new Foo(bar)); + container.Register(Lifestyle.Singleton).WithFactoryMethod(_ => new Foo(bar)); IFoo foo = container.Resolve();