Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create a binding in Ninject that changes based on the requested controller?
    primarykey
    data
    text
    <p>I have an ASP.NET MVC 3 app, and have run into the following situation. On my page, I have a side bar, which can contain related links specific to that page, i.e., determined by controller type. The links will be determined by the current page's content. </p> <p>I have followed <a href="http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx" rel="nofollow">Phil Haack's blog post</a> on rendering dynamic side bars such as this using <code>Html.Action</code> and a separate controller. I like the separation of concerns this approach gives me: my controllers don't know anything about the side bar at all, which is the way it should be. </p> <p>I now want to inject an instance of a derived type of <code>SideBar</code> into my <code>SideBarController</code>, an action on which will be called to render the side bar itself. There is one derived type of <code>SideBar</code> per controller, and so I find myself wanting to write code similar to this:</p> <pre><code>kernel.Bind&lt;SideBar&gt;().ToMethod(_ =&gt; controllerName == "foo" ? new FooSideBar(kernel.Get&lt;UrlHelper&gt;()) : new BarSideBar(kernel.Get&lt;UrlHelper&gt;())); </code></pre> <p>but there's quite a lot that is wrong about that fragment of code, not least the fact that I can't get hold of the controller name in the lambda, and the question of what happens when a third type of controller comes along, and then a fourth, etc. </p> <p>Note that I can't use <code>WhenInjectedInto&lt;T&gt;()</code>, as the <code>SideBar</code> instance will always be injected into the <code>SideBarController</code>. </p> <p>For what it's worth, the instances of <code>SideBar</code> are currently being created via the Ninject Factory extension, so the relevant side bar bindings are as follows (I've hard-bound an implementation of SideBar just to prove the approach so far works):</p> <pre><code>kernel.Bind&lt;ISideBarFactory&gt;().ToFactory().InRequestScope(); kernel.Bind&lt;SideBar&gt;().To&lt;FooSideBar&gt;().InRequestScope(); </code></pre> <p>Finally, I essentially have a one-to-one mapping between the derived types of <code>SideBar</code> and the controller types. It feels a little bit like there might be a bit of duplication here, but also it represents the relationship between the components, so I think I'm ok with it. </p> <p>This all makes me think that my approach to this part of the problem is wrong, and so I would welcome suggestions on how to achieve a clean solution with Ninject. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload