-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating nested containers from other nested containers. Closes GH-378
- Loading branch information
1 parent
599c9ac
commit c4b987d
Showing
3 changed files
with
124 additions
and
20 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
src/Lamar.Testing/Bugs/Bug_378_singletons_being_created_from_multiple_nested_containers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters