1

Is there a way to use ninject's conventional binding to bind to self and all interfaces.

_container.Bind(x => x.From(_assemblyList)
                      .SelectAllClasses()
                      .BindAllInterfaces());

The code above does all interfaces but I can't seem to find a way to also bind to self in the same statement.

McDonnellDean
  • 1,097
  • 2
  • 7
  • 23
  • 1
    IIRC unless you turn off Implicit Self Binding with a NinjectSettings override, Self bindings are generated on the fly (which is ugly and AutoFac doesnt get into this mess...). IOW is this causing a problem for you (I could be mistaken but I believe it shouldnt be neccessary). The `BindToSelf` is only there as a way of saying 'Dont Bind any interfaces here'. ... If any of this pans out, the chances are the wiki could do with an edit to make this more explicit - or a PR for the xmldocs- ya know what to do if that's the case! – Ruben Bartelink May 15 '13 at 14:18
  • Nope, it is actually what I want to do, we have some older services in our app (we are bootstrapping ninject) that require bind to self. If it auto binds to self then all is good, I will do a quick test and come back to confirm. – McDonnellDean May 15 '13 at 14:29
  • @RubenBartelink Do you want to pop your comment down as the answer to receive the green tick? You are correct in that it is implicit. – McDonnellDean May 15 '13 at 14:32
  • 1
    +1 Good responsiveness and feedbackery. Now all you need to do is edit the wiki and/or do a PR for the Xmldocs (or both) to say 'Implicit Self Binding (unless switched off)' makes the class itself resolvable too. (IOW I believe it's pretty likely you're not te only one that hasnt been exposed to the intricacies of Self Binding and are likely to ask oneself this question when dotting up some code armed only with Common and Intelli sense) – Ruben Bartelink May 15 '13 at 14:42
  • I'm going to email Remo and see if it is ok to edit the docs to include this. Also the XMLDocs are an annoyance to be missing. I understand they are a pain to write but putting in at least the summary would be nice, that is how I do it myself! – McDonnellDean May 15 '13 at 14:57
  • @RubenBartelink I have actually asked if I can submit a pull with the XML docs included, I completely agree with you that for a lib of this nature it's important to have this resource available, and I don't mind doing it. – McDonnellDean May 15 '13 at 15:25
  • Are you using it from NuGet ? Ninject def can't be faulted for xmldocs; I was just suggesting that the `` might benefit from a slight rewording - you saying the .xml files are outright missing? – Ruben Bartelink May 15 '13 at 20:07
  • @RubenBartelink Yes I use Nuget, I find not everything is documented or it has the bare minimum which doesn't say very much more that I could gather by reading the method signature. – McDonnellDean May 16 '13 at 08:51

1 Answers1

2

Unless you explicitly turn offstub out Implicit Self Binding (by supplying a NinjectSettings override)as discussed in this answer by me from 2 years back I'd forgotten about, Self bindings are generated on the fly by Ninject as the need arises [which is ugly and AutoFac doesn't get into this mess... Also it leads to some implementation messiness and test cases but as far as I know there are no issues from the perspective of users of Ninject].

The BindToSelf() extension method is symmetric with the ToSelf in the normal Bind chain, i.e., it lets you say 'whatever - I'm going to get to the important bit...` and then override e.g. the Scoping.

Community
  • 1
  • 1
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249