Permalink
Browse files

Added overload for CanResolve which allows ignoring implicit bindings

  • Loading branch information...
remogloor committed Oct 12, 2011
1 parent 3478fe1 commit bcd77c597c33c889b0c15a07325cab0201476666
@@ -1,19 +1,27 @@
namespace Ninject.Tests.Integration.StandardKernelTests
{
+ using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
+
+ using Ninject.Parameters;
using Ninject.Tests.Fakes;
using Xunit;
- public class StandardKernelContext
+ public class StandardKernelContext : IDisposable
{
protected StandardKernel kernel;
public StandardKernelContext()
{
this.kernel = new StandardKernel();
}
+
+ public void Dispose()
+ {
+ this.kernel.Dispose();
+ }
}
public class WhenGetIsCalledForInterfaceBoundService : StandardKernelContext
@@ -423,6 +431,20 @@ public void RemovesAllBindingsForServiceAndReplacesWithSpecifiedBinding()
}
}
+ public class WhenCanResolveIsCalled : StandardKernelContext
+ {
+ [Fact]
+ public void ForImplicitBindings()
+ {
+ this.kernel.Get<Sword>();
+ var request = this.kernel.CreateRequest(typeof(Sword), null, Enumerable.Empty<IParameter>(), false, true);
+
+ this.kernel.CanResolve(request, true).Should().BeFalse();
+ this.kernel.CanResolve(request, false).Should().BeTrue();
+ this.kernel.CanResolve(request).Should().BeTrue();
+ }
+ }
+
public class InitializableA : IInitializable
{
public static int Count = 0;
@@ -30,6 +30,14 @@ public void DelegatesCallToParent()
block.CanResolve(request);
parentMock.Verify(x => x.CanResolve(request));
}
+
+ [Fact]
+ public void DelegatesCallToParent2()
+ {
+ IRequest request = requestMock.Object;
+ block.CanResolve(request, true);
+ parentMock.Verify(x => x.CanResolve(request, true));
+ }
}
public class WhenResolveIsCalledWithRequestObject : ActivationBlockContext
@@ -71,7 +71,21 @@ public override void Dispose(bool disposing)
public bool CanResolve(IRequest request)
{
Ensure.ArgumentNotNull(request, "request");
- return Parent.CanResolve(request);
+ return this.Parent.CanResolve(request);
+ }
+
+ /// <summary>
+ /// Determines whether the specified request can be resolved.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ /// <param name="ignoreImplicitBindings">if set to <c>true</c> implicit bindings are ignored.</param>
+ /// <returns>
+ /// <c>True</c> if the request can be resolved; otherwise, <c>false</c>.
+ /// </returns>
+ public bool CanResolve(IRequest request, bool ignoreImplicitBindings)
+ {
+ Ensure.ArgumentNotNull(request, "request");
+ return this.Parent.CanResolve(request, ignoreImplicitBindings);
}
/// <summary>
View
@@ -318,6 +318,21 @@ public virtual bool CanResolve(IRequest request)
return this.GetBindings(request.Service).Any(this.SatifiesRequest(request));
}
+ /// <summary>
+ /// Determines whether the specified request can be resolved.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ /// <param name="ignoreImplicitBindings">if set to <c>true</c> implicit bindings are ignored.</param>
+ /// <returns>
+ /// <c>True</c> if the request can be resolved; otherwise, <c>false</c>.
+ /// </returns>
+ public virtual bool CanResolve(IRequest request, bool ignoreImplicitBindings)
+ {
+ Ensure.ArgumentNotNull(request, "request");
+ return this.GetBindings(request.Service)
+ .Any(binding => (!ignoreImplicitBindings || !binding.IsImplicit) && this.SatifiesRequest(request)(binding));
+ }
+
/// <summary>
/// Resolves instances for the specified request. The instances are not actually resolved
/// until a consumer iterates over the enumerator.
@@ -29,6 +29,16 @@ public interface IResolutionRoot
/// <returns><c>True</c> if the request can be resolved; otherwise, <c>false</c>.</returns>
bool CanResolve(IRequest request);
+ /// <summary>
+ /// Determines whether the specified request can be resolved.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ /// <param name="ignoreImplicitBindings">if set to <c>true</c> implicit bindings are ignored.</param>
+ /// <returns>
+ /// <c>True</c> if the request can be resolved; otherwise, <c>false</c>.
+ /// </returns>
+ bool CanResolve(IRequest request, bool ignoreImplicitBindings);
+
/// <summary>
/// Resolves instances for the specified request. The instances are not actually resolved
/// until a consumer iterates over the enumerator.

0 comments on commit bcd77c5

Please sign in to comment.