Note that there are some explanatory texts on larger screens.

plurals
  1. POInject different repository depending on a querystring / derive controller and inject the repository depending on the controller type / ASP.NET MVC
    text
    copied!<p>I have a search form that can search in different provider. I started out by having a base controller</p> <pre><code>public SearchController : Controller { protected readonly ISearchService _searchService public SearchController(ISearchService searchService) { _searchService= searchService; } public ActionResult Search(...) { // Use searchService to query and return a view. } } </code></pre> <p>And child controllers </p> <pre><code>TwitterController : SearchController { ... } NewsController : SearchController { ... } </code></pre> <p>I use StructureMap to insert all my dependencies in the controller. With this setup, I was able to change the SearchService depending on the type of the controller being instanciated.</p> <pre><code>x.For&lt;ISearchService&gt;().ConditionallyUse(o =&gt; { o.TheDefault.Is.OfConcreteType&lt;NewsSearchService&gt;(); o.If(c =&gt; c.ParentType == typeof(TwitterController)) .ThenIt.Is.OfConcreteType&lt;TwitterSearchService&gt;(); ... }); </code></pre> <p>That even allowed me to set different Views for each controller, (just putting the corresponding folder (Twitter, News...) and the Parent controller is still handling all the Search, with a simple </p> <pre><code>return View(results) </code></pre> <p>which is displaying the correct view specific to twitter, news, or other</p> <p>Now that was cool and looked great, I a single form and the different views are displayed in tabs on the same page. That's where it starts to get complicated with this approach. The form has to post to /Twitter to search in twitter, to /News to search in news... which means I should change the action parameter of the form depending on which tab I am and display the correct tab on when the form returns depending on.. the url? craziness follows.</p> <p>If you have built something like this already or know what's the best approach to this, please advices are welcome.</p> <p>Now I think I would have less pain using a parameter in the form and posting to a single controller. I am thinking of injecting the correct SearchService depending on this parameter. What would be the best approach? I thought of using a model binder,</p> <p>So I would have my ActionMethod that look like this:</p> <pre><code>public ActionResult Search(ISearchService service, Query query) { var results = service.Find(query); } </code></pre> <p>But I think would need to make a call like this in the ModelBinder</p> <pre><code>ObjectFactory.GetInstance(...); </code></pre> <p>Based on the querystring parameter that describe which provider to use, and that doesn't seem more elegant to me. I feel stuck, help :(.</p>
 

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