tags:

views:

51

answers:

2

I see methods for enumerating lists of bindings for a given service (type), but nowhere do I find a method returning a list of everything that's been bound in my loaded modules. I'm looking for something like Kernel::IEnumerable<IBinding> GetAllRegisteredBindings()

Does this exist? If not, might I be able to build an extension that could do it? I'd need to be able to get to the bindings w/out a service type...

+1  A: 

I looked through the code and didn't see a way to request all of the bindings. If you're comfortable with modifying it and using the modified code, here's what you can do:

to IKernel.cs, add:

    /// <summary>
    /// Gets all registered bindings
    /// </summary>
    IEnumerable<IBinding> GetBindings();

to KernelBase.cs, add:

    /// <summary>
    /// Gets all registered bindings
    /// </summary>
    public virtual IEnumerable<IBinding> GetBindings()
    {
        return _bindings.SelectMany( kvp => kvp.Value );
    }

and recompile.

to use:

    var bindings = Kernel.GetBindings();
    bindings.ForEach( b => logger.DebugFormat( "Binding: {0} -> {1}", b.Service, b.Target ) );
dave thieben
+1  A: 

While @dave thieben isnt far wrong, it would appear that your route in without requiring forkage may be to register a custom IBindingResolver Component in the Kernel and then concoct an IRequest that it will recognise, possibly via ResolutionExtensions.GetAll() (in general, most will require you to specify a service (though none Ensure.NotNull on it, some go party on it assuming NotNull).

But you forgot to say why you want it.

Thus I recommend:

  1. saying what you want
  2. asking on the ninject mailing list, including the answer to #1 - this is no beginner question!
Ruben Bartelink
ooh, that's cool. I hadn't considered that.
dave thieben
Thx Dave; thx Ruben. Sorry for the delay, I didn't have "Notify" checked. This was originally for the purpsose of providing a singular entry point into a framework, i.e. a FrameworkServices class that would provide a list of the IFrameworkService descendants that had been bound; thus calling code could check with the Manager what service bindings were available... I've since gone a different path, though I'd still like to be able to get a list of all bindings. ;-)
Robert Leahey
@Robert Leahey: There are a good few calls that do what you want (find the bindigns for a specific service) built in. The way you asked it suggested you wanted all bindings for all services, which is the tricky bit. There are calls to get bindings and there are calls for getting IEnumerable or arrays of implementations of a service. There are also a set of APIs for putting metadata on bindings in order to aid one in programmatically selecting an appropriate one at resolution time, either imperatively or declaratively
Ruben Bartelink
I'd also point out that @dave thieben and I spent not trivial amounts of time determining this. Hence a) we're learnt something new and are happy b) at least one of us deserves an upvote. But that's just me being a righteous smartarse beating up on noobs, so forget I said anything (having said that, I'm glad to hear a response at some stage - better late than never). (I personally would never ask a question like this and not either a) self-answer or delete when I've worked it out or b) react pretty immediiately to answers)
Ruben Bartelink
I'll give you an upvote, Ruben. ;)
dave thieben
@dave thieben: Backatcha. Now we'll be done for rigging :D
Ruben Bartelink
Dave and Ruben - I thank you for your time spent on this question; however, as Ruben points out, I am a StackOverflow noob, and I apparently do not have the "reputation" to so much as say that your answers are useful, much less The Answer.
Robert Leahey
Ruben, the way I asked the question was exactly the way I wanted it; you were right in the first case: I want a list of all bindings for all services, thus it appears the answer to the question is that I can't have it without modifying the source or jumping through hoops, thanks though. btw, something that works almost as well as being a righteous smartarse beating up on noobs is option c) asking clearly and directly for an "upvote", though it's not as much fun as taking offense. Either way, it's moot since I'm unable to vote. Thanks for everything.
Robert Leahey
@Robert Leahey: Never mind my whingeing. I was joking about wanting an upvote, though I thought @dave thieben who did the original work easily merited one. Whether someone going to double check and coming up with an unverified theory without a sample is another matter. NOt sure why you're not able to upvote. Also dont get what you mean by saying you dont have th rep to say if answers are useful - you're the most qualified person on the planet as far as I'm concerned. Still confusd by the mismatch between you saying you want a list of IFrameworkServices vs a list of everything.
Ruben Bartelink
Anyway a +1 on the question as you've spent time interacting with the questioners and people now more or less know what you want and why
Ruben Bartelink