Skip to content

Commit

Permalink
Creating nested containers from other nested containers. Closes GH-378
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Sep 7, 2023
1 parent 599c9ac commit c4b987d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Lamar.Testing.Bugs
{
public class Bug_378_singleton_scope_incorrect_multiple_nested_containers
{
public class SingletonType
{
public Guid Id;
public SingletonType() { Id = Guid.NewGuid(); }
}

public class ScopedA
{
public SingletonType SingletonType;
public ScopedA(SingletonType singletonType) { SingletonType = singletonType; }
}

public class ScopedB
{
public ScopedA A;
public ScopedB(ScopedA scopedA) { A = scopedA; }
}

[Fact]
public void SingletonScopeIsIncorrect_WhenNestedContainersAre3LevelsDeep()
{
var container = new Container(c =>
{
c.AddSingleton<SingletonType>();
c.AddScoped<ScopedA>(); // Depends on SingletonType
c.AddScoped<ScopedB>(); // Depends on ScopedA
});

var singleton = container.GetInstance<SingletonType>();

using var nested1 = container.GetNestedContainer();
using var nested2 = nested1.GetInstance<IContainer>().GetNestedContainer();
using var nested3 = nested2.GetInstance<IContainer>().GetNestedContainer();

var scopedB = nested3.GetInstance<ScopedB>();
singleton.ShouldBeTheSameAs(scopedB.A.SingletonType);
}

}

public class Bug_tbd_singleton_scope_incorrect_multiple_nested_containers_setters_test
{
public class SingletonType
{
public Guid Id;

[SetterProperty]
public SingletonOther SingletonOther { get; set; }
public SingletonType() { Id = Guid.NewGuid(); }
}

public class SingletonOther
{
public Guid Id;
public SingletonOther() { Id = Guid.NewGuid(); }
}

public class ScopedA
{
public SingletonType SingletonType;
public ScopedA(SingletonType singletonType) { SingletonType = singletonType; }
}

public class ScopedB
{
public ScopedA A;
public ScopedB(ScopedA scopedA) { A = scopedA; }
}

[Fact]
public void SingletonScopeIsIncorrect_WhenNestedContainersAre3LevelsDeep()
{
var container = new Container(c =>
{
c.AddSingleton<SingletonType>();
c.AddSingleton<SingletonOther>();
c.AddScoped<ScopedA>(); // Depends on SingletonType
c.AddScoped<ScopedB>(); // Depends on ScopedA
});

var singletonOther = container.GetInstance<SingletonOther>();

using var nested1 = container.GetNestedContainer();
using var nested2 = nested1.GetInstance<IContainer>().GetNestedContainer();
using var nested3 = nested2.GetInstance<IContainer>().GetNestedContainer();

var scopedB = nested3.GetInstance<ScopedB>();
singletonOther.ShouldBeTheSameAs(scopedB.A.SingletonType.SingletonOther);

}

}
}
40 changes: 20 additions & 20 deletions src/Lamar.Testing/Lamar.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@
<LangVersion>10.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0"/>
<PackageReference Include="System.Data.SqlClient" Version="4.8.2"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"/>
<PackageReference Include="Shouldly" Version="3.0.0"/>
<PackageReference Include="NSubstitute" Version="3.1.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="NSubstitute" Version="3.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Aspect.Logger\Widget.Aspect.Logger.csproj"/>
<ProjectReference Include="..\Lamar.Diagnostics\Lamar.Diagnostics.csproj"/>
<ProjectReference Include="..\Lamar\Lamar.csproj"/>
<ProjectReference Include="..\StructureMap.Testing.GenericWidgets\StructureMap.Testing.GenericWidgets.csproj"/>
<ProjectReference Include="..\StructureMap.Testing.Widget2\StructureMap.Testing.Widget2.csproj"/>
<ProjectReference Include="..\StructureMap.Testing.Widget3\StructureMap.Testing.Widget3.csproj"/>
<ProjectReference Include="..\StructureMap.Testing.Widget4\StructureMap.Testing.Widget4.csproj"/>
<ProjectReference Include="..\StructureMap.Testing.Widget5\StructureMap.Testing.Widget5.csproj"/>
<ProjectReference Include="..\StructureMap.Testing.Widget\StructureMap.Testing.Widget.csproj"/>
<ProjectReference Include="..\Widget.Core\Widget.Core.csproj"/>
<ProjectReference Include="..\Widget.Instance\Widget.Instance.csproj"/>
<ProjectReference Include="..\Widget.Registration\Widget.Registration.csproj"/>
<ProjectReference Include="..\Aspect.Logger\Widget.Aspect.Logger.csproj" />
<ProjectReference Include="..\Lamar.Diagnostics\Lamar.Diagnostics.csproj" />
<ProjectReference Include="..\Lamar\Lamar.csproj" />
<ProjectReference Include="..\StructureMap.Testing.GenericWidgets\StructureMap.Testing.GenericWidgets.csproj" />
<ProjectReference Include="..\StructureMap.Testing.Widget2\StructureMap.Testing.Widget2.csproj" />
<ProjectReference Include="..\StructureMap.Testing.Widget3\StructureMap.Testing.Widget3.csproj" />
<ProjectReference Include="..\StructureMap.Testing.Widget4\StructureMap.Testing.Widget4.csproj" />
<ProjectReference Include="..\StructureMap.Testing.Widget5\StructureMap.Testing.Widget5.csproj" />
<ProjectReference Include="..\StructureMap.Testing.Widget\StructureMap.Testing.Widget.csproj" />
<ProjectReference Include="..\Widget.Core\Widget.Core.csproj" />
<ProjectReference Include="..\Widget.Instance\Widget.Instance.csproj" />
<ProjectReference Include="..\Widget.Registration\Widget.Registration.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Codegen\StubGeneratedMethod.cs"/>
<Compile Remove="Codegen\StubGeneratedMethod.cs" />
</ItemGroup>
<ItemGroup>
<Compile Remove="IoC\Acceptance\generated_code.cs"/>
<Compile Remove="IoC\Acceptance\generated_code.cs" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Lamar/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ private Container(ServiceGraph serviceGraph, Container container) : base(service
public INestedContainer GetNestedContainer()
{
assertNotDisposed();

if (Root is Container root) return new Container(root.ServiceGraph, root);

return new Container(ServiceGraph, this);
}

Expand Down

0 comments on commit c4b987d

Please sign in to comment.