Determining if a Conditional Binding is Resolvable With Ninject

An entry about inversion of control Publication date 12. September 2008 21:05

While working on an ASP.NET MVC application today which uses the Ninject IOC container, I came across a scenario in which I needed a way to figure out whether a given conditional binding would be resolvable or not. Out of the box, the only way to really do this would be to call Get<T>() and catch the ActivationException thrown. Not a very elegant solution, especially since it has the potentially nasty side-effect of actually activating the instance of the binding if it was found.

So I set out to find a better way, and with a few pointers from Nate (the author of Ninject), I ended up with the following extension method for IKernel:

public static class KernelExtensions
{ 
    /// <summary>
    /// Determines whether a given type has a resolvable binding
    /// </summary>
    public static bool HasBindingFor<T>(this IKernel kernel, IParameterCollection parameters)
    {
        Type service = typeof (T);
 
        // build a context with the parameters
        IContext context = kernel.Components.Get<IContextFactory>().Create(service);
        context.Parameters = parameters;
 
        // find all the bindings for the type
        var registry = kernel.Components.Get<IBindingRegistry>();
        ICollection<IBinding> bindings = registry.GetBindings(service);
 
        bool result = bindings.Has(binding => binding.Condition.Matches(context));
        return result;
    }
}

In my ASP.NET MVC application I can now use this to for example determine whether a given controller exists or not:

bool controllerExists = _kernel.HasBindingFor<IController>(
With.Parameters.Variable("controllerName", controllerName));

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Powered by BlogEngine.NET 1.4.5.0

Welcome!

My name is Fredrik Kalseth, and this is my blog - thanks for visiting! I am fortunate enough to work with what I love for a living, and this blog is essentially the biproduct of that.

I work as a senior consultant for Capgemini, and am also an active participant in the Norwegian .NET community, as an avid attendee but also as a speaker (most recently at NNUG and MSDN Live).

As a developer, I have a wide circle of interest. My primary passion is for agile, test-driven development, with focus on best practices and clean code. That said, I also love to work on the frontend, especially with web development.

On Twitter? My handle is fkalseth. On LinkedIn? I`m there too.

NDC 2010

The conference to attend this summer happens June 16th-18th in Oslo, Norway. Are you going? Be sure to catch my talk on AOP while you're there!

 

Disclaimer

This is a personal blog; any opinions expressed here are my own and do not necessarily reflect those of my employer. All content herein is my own original creation, and as such is protected by copyright law. Unless otherwise stated, all source code posted on this blog is freely usable under the Microsoft Permissive License.

What Readers Talk About

Comment RSS