Skip to content

Commit

Permalink
fixed: #460
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Mar 16, 2022
1 parent a4e02bc commit c134ce1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4937,7 +4937,7 @@ public static class ResolverContext
/// Traverses the parent containers until the root or returns itself if it is already a root.</summary>
public static Expression GetRootOrSelfExpr(Request request) =>
request.Reuse is CurrentScopeReuse == false
&& (request.DirectParent.IsSingletonOrDependencyOfSingleton || request.IsDirectlyWrappedInFunc())
&& request.DirectParent.IsSingletonOrDependencyOfSingleton
&& !request.OpensResolutionScope
&& request.Rules.ThrowIfDependencyHasShorterReuseLifespan // see the #378
? RootOrSelfExpr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public int Run()
Should_use_data_from_the_scope_when_resolving_in_scope_with_asResolutionCall();
Should_use_data_from_the_scope_when_resolving_in_scope_with_original_Use();
Auto_dynamic_registration_for_the_concrete_types_should_work();
return 3;
Should_use_data_from_the_scope_when_resolving_in_scope();
Should_use_data_from_the_scope_when_resolving_in_scope_without_the_first_resolve();
return 4;
}

[Test]
Expand Down Expand Up @@ -75,6 +77,42 @@ public void Auto_dynamic_registration_for_the_concrete_types_should_work()
Assert.AreEqual("child", inScope.Dependent.Data1.Text);
}
}

[Test]
public void Should_use_data_from_the_scope_when_resolving_in_scope()
{
var container = new Container(rules => rules.WithFuncAndLazyWithoutRegistration());

container.RegisterInstance(new Data { Text = "parent" }, setup: Setup.With(asResolutionCall: true));
container.Register<IDataDependent, DataDependent>();
container.Register<DataDependentIndirectly2>();

// now it fails even if this is removed
//var outside = container.Resolve<DataDependentIndirectly2>();

using var scope = container.OpenScope();
scope.Use(new Data { Text = "child" });

var inScope = scope.Resolve<DataDependentIndirectly2>();
Assert.AreEqual("child", inScope.Dependent.Data1.Text); // fails, the value is "parent"
}

[Test]
public void Should_use_data_from_the_scope_when_resolving_in_scope_without_the_first_resolve()
{
var container = new Container(rules => rules.WithFuncAndLazyWithoutRegistration());

container.RegisterInstance(new Data { Text = "parent" }, setup: Setup.With(asResolutionCall: true));
container.Register<IDataDependent, DataDependent>();
container.Register<DataDependentIndirectly2>();

using var scope = container.OpenScope();
scope.Use(new Data { Text = "child" });

var inScope = scope.Resolve<DataDependentIndirectly2>();
Assert.AreEqual("child", inScope.Dependent.Data1.Text); // fails, the value is "parent"
}

public class Data
{
public string Text { get; set; }
Expand Down Expand Up @@ -104,5 +142,17 @@ public DataDependentIndirectly(IDataDependent dataDependent)
Dependent = dataDependent;
}
}

public class DataDependentIndirectly2
{
private Lazy<IDataDependent> _lazyDependent;

public IDataDependent Dependent => _lazyDependent.Value;

public DataDependentIndirectly2(Lazy<IDataDependent> dataDependent)
{
_lazyDependent = dataDependent;
}
}
}
}

0 comments on commit c134ce1

Please sign in to comment.