From b3e9e8127cdb741126222c057e9d496f7d4f32c0 Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 17 Dec 2021 14:18:50 +0100 Subject: [PATCH] - fix bug where singletons could be created multiple times --- LightweightIocContainer/IocContainer.cs | 4 ++ .../LightweightIocContainer.csproj | 2 +- README.md | 4 +- .../MultiLayerResolveTest.cs | 41 ++++++++++++++----- .../Test.LightweightIocContainer.csproj | 2 +- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/LightweightIocContainer/IocContainer.cs b/LightweightIocContainer/IocContainer.cs index 87a260c..7619dd1 100644 --- a/LightweightIocContainer/IocContainer.cs +++ b/LightweightIocContainer/IocContainer.cs @@ -203,6 +203,10 @@ namespace LightweightIocContainer /// A recursively resolved instance of the given private T ResolvePlaceholder(InternalToBeResolvedPlaceholder toBeResolvedPlaceholder) { + object? existingInstance = TryGetExistingInstance(toBeResolvedPlaceholder.ResolvedRegistration, toBeResolvedPlaceholder.Parameters); + if (existingInstance is T instance) + return instance; + if (toBeResolvedPlaceholder.Parameters == null) return CreateInstance(toBeResolvedPlaceholder.ResolvedRegistration, null); diff --git a/LightweightIocContainer/LightweightIocContainer.csproj b/LightweightIocContainer/LightweightIocContainer.csproj index 971d98b..f65b55b 100644 --- a/LightweightIocContainer/LightweightIocContainer.csproj +++ b/LightweightIocContainer/LightweightIocContainer.csproj @@ -9,7 +9,7 @@ default enable LightweightIocContainer.xml - 3.0.0 + 3.0.1 diff --git a/README.md b/README.md index 07f6d2e..1fcb349 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,13 @@ The easiest way to [install](https://github.com/SimonG96/LightweightIocContainer You can either use the [`PackageManager`](https://github.com/SimonG96/LightweightIocContainer/wiki/Install-Lightweight-IOC-Container#packagemanager) in VisualStudio: ```PM -PM> Install-Package LightweightIocContainer -Version 3.0.0 +PM> Install-Package LightweightIocContainer -Version 3.0.1 ``` or you can use the [`.NET CLI`](https://github.com/SimonG96/LightweightIocContainer/wiki/Install-Lightweight-IOC-Container#net-cli): ```.net -> dotnet add package LightweightIocContainer --version 3.0.0 +> dotnet add package LightweightIocContainer --version 3.0.1 ``` ### Example usage diff --git a/Test.LightweightIocContainer/MultiLayerResolveTest.cs b/Test.LightweightIocContainer/MultiLayerResolveTest.cs index 3c7d857..1e7181c 100644 --- a/Test.LightweightIocContainer/MultiLayerResolveTest.cs +++ b/Test.LightweightIocContainer/MultiLayerResolveTest.cs @@ -13,12 +13,12 @@ public class MultiLayerResolveTest { public interface IA { - + IB BProperty { get; } } public interface IB { - + C C { get; } } [UsedImplicitly] @@ -36,19 +36,28 @@ public class MultiLayerResolveTest [UsedImplicitly] private class A : IA { - [UsedImplicitly] - private readonly IB _b; - - public A(IBFactory bFactory) => _b = bFactory.Create(new C("from A")); + public A(IBFactory bFactory) => BProperty = bFactory.Create(new C("from A")); + public IB BProperty { get; } + } + + private class OtherA : IA + { + public OtherA(IB bProperty, IB secondB) + { + BProperty = bProperty; + SecondB = secondB; + } + + public IB BProperty { get; } + public IB SecondB { get; } } [UsedImplicitly] private class B : IB { - public B(C c) - { - - } + public B(C c) => C = c; + + public C C { get; } } [UsedImplicitly] @@ -82,4 +91,16 @@ public class MultiLayerResolveTest IB b = container.Resolve(); Assert.IsInstanceOf(b); } + + [Test] + public void TestResolveSingletonTwiceAsCtorParameterInSameCtor() + { + IocContainer container = new(); + container.Register(r => r.Add()); + container.Register(r => r.Add()); + container.Register(r => r.Add(Lifestyle.Singleton).WithParameters("test")); + + OtherA a = container.Resolve(); + Assert.AreEqual(a.BProperty.C, a.SecondB.C); + } } \ No newline at end of file diff --git a/Test.LightweightIocContainer/Test.LightweightIocContainer.csproj b/Test.LightweightIocContainer/Test.LightweightIocContainer.csproj index 247e803..1a7b1cd 100644 --- a/Test.LightweightIocContainer/Test.LightweightIocContainer.csproj +++ b/Test.LightweightIocContainer/Test.LightweightIocContainer.csproj @@ -5,7 +5,7 @@ false SimonG default - 3.0.0 + 3.0.1