Skip to content

Commit

Permalink
fixed: #559
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Mar 13, 2023
1 parent e6a4e33 commit 16b1084
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
12 changes: 3 additions & 9 deletions src/DryIoc/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4724,7 +4724,6 @@ public static Expression GetRootOrSelfExpr(Request request) =>
request.Reuse is CurrentScopeReuse == false
&& request.DirectParent.IsSingletonOrDependencyOfSingleton
&& request.Rules.ThrowIfDependencyHasShorterReuseLifespan // see the #378
// && !request.OpensResolutionScope
&& !request.OpensResolutionScopeUpToResolutionCall()
? RootOrSelfExpr
: FactoryDelegateCompiler.ResolverContextParamExpr;
Expand Down Expand Up @@ -4822,11 +4821,7 @@ public static IScope GetNamedScope(this IResolverContext r, object name, bool th
}

if (s == null)
{
if (throwIfNotFound)
Throw.It(Error.NoMatchedScopeFound, name, currentScope);
return null;
}
return throwIfNotFound ? Throw.For<Scope>(Error.NoMatchedScopeFound, name, currentScope) : null;

if (s.IsDisposed)
{
Expand Down Expand Up @@ -5131,7 +5126,7 @@ private static Expression GetArrayExpression(Request request)
var itemRequest = request.Push(itemInfo);

// For the required service type (not a wrapper) we at least looking at the unwrapped type, so we may check that type factory condition,
// or going to resolve the nested wrapper and Store the unwrapped factory in the request but did not check it until we down the wrappers chain with all available information
// or going to resolve the nested wrapper and Store the unwrapped factory in the request but did not check it until we down the wrappers chain with all the available information
var factory = requiredItemType == itemType
? itemRequest.MatchGeneratedFactoryByReuseAndConditionOrNull(item.Factory)
: container.ResolveFactory(itemRequest.WithWrappedServiceFactory(item.Factory));
Expand Down Expand Up @@ -9380,8 +9375,7 @@ public static bool MatchGeneratedFactory(this Request r, Factory f) =>
/// <summary>Matching things</summary>
public static Factory MatchGeneratedFactoryByReuseAndConditionOrNull(this Request r, Factory f)
{
var reuse = f.Reuse;
if (reuse != null && !reuse.CanApply(r))
if (!f.Setup.OpenResolutionScope && !r.MatchFactoryReuse(f))
return null;

var condition = f.Setup.Condition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public class GHIssue559_Possible_inconsistent_behaviour : ITest
{
public int Run()
{
Test1();
return 1;
Test_scoped_opening_scope();
Test_singleton_opening_scope();
return 2;
}

[Test]
public void Test1()
public void Test_scoped_opening_scope()
{
var container = new Container();
container.Register<Foo>(Reuse.Scoped, setup: Setup.With(openResolutionScope: true));
Expand All @@ -23,7 +24,20 @@ public void Test1()
Assert.IsNotNull(foo);

var actual = container.Resolve<IEnumerable<Foo>>();
Assert.AreEqual(0, actual.Count()); // todo: @fixme
Assert.AreEqual(1, actual.Count());
}

[Test]
public void Test_singleton_opening_scope()
{
var container = new Container();
container.Register<Foo>(Reuse.Singleton, setup: Setup.With(openResolutionScope: true));

var foo1 = container.Resolve<Foo>();
Assert.IsNotNull(foo1);

var foo2 = container.Resolve<Foo>();
Assert.AreSame(foo1, foo2);
}

public class Foo {}
Expand Down
2 changes: 1 addition & 1 deletion test/DryIoc.TestRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static void Main()
RunAllTests();

// new GHIssue559_Possible_inconsistent_behaviour().Run();

// new GHIssue557_WithFactorySelector_allows_to_Resolve_the_keyed_service_as_non_keyed().Run();
// new GHIssue555_ConcreteTypeDynamicRegistrations_is_not_working_with_MicrosoftDependencyInjectionRules().Run();
// new GHIssue554_System_NullReferenceException_Object_reference_not_set_to_an_instance_of_an_object().Run();
Expand Down Expand Up @@ -74,6 +73,7 @@ void Run(Func<int> run, string name = null)
new GHIssue554_System_NullReferenceException_Object_reference_not_set_to_an_instance_of_an_object(),
new GHIssue555_ConcreteTypeDynamicRegistrations_is_not_working_with_MicrosoftDependencyInjectionRules(),
new GHIssue557_WithFactorySelector_allows_to_Resolve_the_keyed_service_as_non_keyed(),
new GHIssue559_Possible_inconsistent_behaviour(),
};

// Parallel.ForEach(tests, x => Run(x.Run)); // todo: @perf enable and test when more tests are added
Expand Down

0 comments on commit 16b1084

Please sign in to comment.