Skip to content

Commit

Permalink
more seamless aproach to #565
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Apr 24, 2023
1 parent a3bdb06 commit 7a8dffb
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public void TestScope_Zero()
});
}

public sealed class ToAnyScopeExceptResolutionScopeOf<T> : IScopeName
{
public static readonly IScopeName Instance = new ToAnyScopeExceptResolutionScopeOf<T>();
private ToAnyScopeExceptResolutionScopeOf() {}
public bool Match(object scopeName) =>
scopeName is not ResolutionScopeName rn || rn.ServiceType != typeof(T);
}

[Test]
public void TestScope_One()
{
Expand All @@ -70,7 +78,7 @@ public void TestScope_One()
/// even though it has opened a new scope internally.
var container = new Container();
container.Register<ICar, Car>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.Transient);
container.Register<IBar, Bar>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.ScopedTo<ICar>());
container.Register<IBar, Bar>(setup: Setup.With(openResolutionScope: true), reuse: Reuse.ScopedTo(ToAnyScopeExceptResolutionScopeOf<IBar>.Instance));
container.Register<IFoo, Foo>(Reuse.Singleton);
container.Resolve<ICar>().GetId();
var car = container.Resolve<ICar>();
Expand Down Expand Up @@ -241,7 +249,7 @@ public Car(IResolverContext container, IBar bar)
_guidArray[3] = (nameof(ICar), _id);
_guidArray[4] = (nameof(IBar), container.Resolve<IBar>().GetId());
_guidArray[5] = (nameof(IFoo), container.Resolve<IBar>().GetFooId());
using (var scope = container.OpenScope(ResolutionScopeName.Of<ICar>()))
using (var scope = container.OpenScope())
{
//// 3 in new scope. IBar should have new id and be the same instance for each resolution.
_guidArray[6] = (nameof(ICar), _id);
Expand Down

0 comments on commit 7a8dffb

Please sign in to comment.