Skip to content

Commit

Permalink
fix #393
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-xu committed May 27, 2022
1 parent fa5b4af commit 179fc25
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public void EmptyEnumerableIsResolvedIfElementTypeIsMissingBinding()
[Fact]
public void EmptyEnumerableIsResolvedIfElementTypeIsMissingBindingEvenIsWasResoved()
{
var children1 = this.Kernel.Get<IEnumerable<ChildA>>();
var child = this.Kernel.Get<ChildA>();
var children = this.Kernel.Get<IEnumerable<ChildA>>();
var children2 = this.Kernel.Get<IEnumerable<ChildA>>();

children.Should().BeEmpty();
children1.Should().BeEmpty();
children2.Should().BeEmpty();
}
}
}
20 changes: 13 additions & 7 deletions src/Ninject/KernelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public virtual bool CanResolve(IRequest request, bool ignoreImplicitBindings)
/// <returns>An enumerator of instances that match the request.</returns>
public virtual IEnumerable<object> Resolve(IRequest request)
{
return this.Resolve(request, true);
return this.Resolve(request, true, false);
}

/// <summary>
Expand Down Expand Up @@ -478,7 +478,7 @@ protected virtual IContext CreateContext(IRequest request, IBinding binding)
return new Context(this, request, binding, this.Components.Get<ICache>(), this.Components.Get<IPlanner>(), this.Components.Get<IPipeline>());
}

private IEnumerable<object> Resolve(IRequest request, bool handleMissingBindings)
private IEnumerable<object> Resolve(IRequest request, bool handleMissingBindings, bool filterImplicitBindings)
{
void UpdateRequest(Type service)
{
Expand All @@ -499,7 +499,7 @@ void UpdateRequest(Type service)

UpdateRequest(service);

return new[] { this.Resolve(request, false).CastSlow(service).ToArraySlow(service) };
return new[] { this.Resolve(request, false, true).CastSlow(service).ToArraySlow(service) };
}

if (request.Service.IsGenericType)
Expand All @@ -512,7 +512,7 @@ void UpdateRequest(Type service)

UpdateRequest(service);

return new[] { this.Resolve(request, false).CastSlow(service).ToListSlow(service) };
return new[] { this.Resolve(request, false, true).CastSlow(service).ToListSlow(service) };
}

if (gtd == typeof(IEnumerable<>))
Expand All @@ -521,19 +521,25 @@ void UpdateRequest(Type service)

UpdateRequest(service);

return new[] { this.Resolve(request, false).CastSlow(service) };
return new[] { this.Resolve(request, false, true).CastSlow(service) };
}
}

var satisfiedBindings = this.GetBindings(request.Service)
.Where(this.SatifiesRequest(request));

if (filterImplicitBindings)
{
satisfiedBindings = satisfiedBindings.Where(binding => binding.IsImplicit == false);
}

var satisfiedBindingEnumerator = satisfiedBindings.GetEnumerator();

if (!satisfiedBindingEnumerator.MoveNext())
{
if (handleMissingBindings && this.HandleMissingBinding(request))
{
return this.Resolve(request);
return this.Resolve(request, false, false);
}

if (request.IsOptional)
Expand Down Expand Up @@ -570,7 +576,7 @@ from binding in satisfiedBindings
}
else
{
if (satisfiedBindings.Any(binding => !binding.IsImplicit) || !handleMissingBindings)
if (satisfiedBindings.Any(binding => !binding.IsImplicit))
{
satisfiedBindings = satisfiedBindings.Where(binding => !binding.IsImplicit);
}
Expand Down

0 comments on commit 179fc25

Please sign in to comment.