- fix bug where singletons could be created multiple times

pull/57/head
Simon G 4 years ago
parent 1072e62563
commit b3e9e8127c
  1. 4
      LightweightIocContainer/IocContainer.cs
  2. 2
      LightweightIocContainer/LightweightIocContainer.csproj
  3. 4
      README.md
  4. 41
      Test.LightweightIocContainer/MultiLayerResolveTest.cs
  5. 2
      Test.LightweightIocContainer/Test.LightweightIocContainer.csproj

@ -203,6 +203,10 @@ namespace LightweightIocContainer
/// <returns>A recursively resolved instance of the given <see cref="Type"/></returns>
private T ResolvePlaceholder<T>(InternalToBeResolvedPlaceholder toBeResolvedPlaceholder)
{
object? existingInstance = TryGetExistingInstance<T>(toBeResolvedPlaceholder.ResolvedRegistration, toBeResolvedPlaceholder.Parameters);
if (existingInstance is T instance)
return instance;
if (toBeResolvedPlaceholder.Parameters == null)
return CreateInstance<T>(toBeResolvedPlaceholder.ResolvedRegistration, null);

@ -9,7 +9,7 @@
<LangVersion>default</LangVersion>
<Nullable>enable</Nullable>
<DocumentationFile>LightweightIocContainer.xml</DocumentationFile>
<VersionPrefix>3.0.0</VersionPrefix>
<VersionPrefix>3.0.1</VersionPrefix>
</PropertyGroup>
<PropertyGroup>

@ -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

@ -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<IB>();
Assert.IsInstanceOf<B>(b);
}
[Test]
public void TestResolveSingletonTwiceAsCtorParameterInSameCtor()
{
IocContainer container = new();
container.Register(r => r.Add<IA, OtherA>());
container.Register(r => r.Add<IB, B>());
container.Register(r => r.Add<C>(Lifestyle.Singleton).WithParameters("test"));
OtherA a = container.Resolve<OtherA>();
Assert.AreEqual(a.BProperty.C, a.SecondB.C);
}
}

@ -5,7 +5,7 @@
<IsPackable>false</IsPackable>
<Authors>SimonG</Authors>
<LangVersion>default</LangVersion>
<VersionPrefix>3.0.0</VersionPrefix>
<VersionPrefix>3.0.1</VersionPrefix>
</PropertyGroup>
<ItemGroup>

Loading…
Cancel
Save