- fix multiton resolve with int as scope

master
Simon G. 1 year ago
parent b8541e3ae5
commit 9a5abca32b
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 5
      LightweightIocContainer/IocContainer.cs
  2. 19
      Test.LightweightIocContainer/IocContainerTest.cs

@ -3,7 +3,6 @@
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using LightweightIocContainer.Exceptions; using LightweightIocContainer.Exceptions;
using LightweightIocContainer.Interfaces; using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Factories; using LightweightIocContainer.Interfaces.Factories;
@ -23,7 +22,7 @@ public class IocContainer : IIocContainer, IIocResolver
private readonly RegistrationFactory _registrationFactory; private readonly RegistrationFactory _registrationFactory;
private readonly List<(Type type, object? instance)> _singletons = new(); private readonly List<(Type type, object? instance)> _singletons = new();
private readonly List<(Type type, Type scope, ConditionalWeakTable<object, object?> instances)> _multitons = new(); private readonly List<(Type type, Type scope, Dictionary<object, object?> instances)> _multitons = new();
private readonly List<Type> _ignoreConstructorAttributes; private readonly List<Type> _ignoreConstructorAttributes;
@ -421,7 +420,7 @@ public class IocContainer : IIocContainer, IIocResolver
} }
T newInstance = Creator.CreateInstance<T>(registration.ImplementationType, arguments[1..]); T newInstance = Creator.CreateInstance<T>(registration.ImplementationType, arguments[1..]);
_multitons.Add((registration.ImplementationType, registration.Scope, new ConditionalWeakTable<object, object?> { { scopeArgument, newInstance } })); _multitons.Add((registration.ImplementationType, registration.Scope, new Dictionary<object, object?> { { scopeArgument, newInstance } }));
return newInstance; return newInstance;
} }

@ -35,6 +35,11 @@ public class IocContainerTest
} }
} }
private class TestMultitonIntScope(int scope) : ITest
{
}
[UsedImplicitly] [UsedImplicitly]
private class TestConstructor : ITest private class TestConstructor : ITest
{ {
@ -270,6 +275,20 @@ public class IocContainerTest
Assert.AreNotSame(resolvedTest2, resolvedTest3); Assert.AreNotSame(resolvedTest2, resolvedTest3);
} }
[Test]
public void TestResolveMultitonIntScope()
{
_iocContainer.Register(r => r.AddMultiton<ITest, TestMultitonIntScope, int>());
ITest resolvedTest1 = _iocContainer.Resolve<ITest>(1);
ITest resolvedTest2 = _iocContainer.Resolve<ITest>(1);
ITest resolvedTest3 = _iocContainer.Resolve<ITest>(2);
Assert.AreSame(resolvedTest1, resolvedTest2);
Assert.AreNotSame(resolvedTest1, resolvedTest3);
Assert.AreNotSame(resolvedTest2, resolvedTest3);
}
[Test] [Test]
public void TestResolveMultitonNoArgs() public void TestResolveMultitonNoArgs()
{ {

Loading…
Cancel
Save