Note that there are some explanatory texts on larger screens.

plurals
  1. POCastle Windsor IOC in MVC Controller
    primarykey
    data
    text
    <p>I have set up Castle Windsor IoC in MVC. I've read through the tutorials, and have done this:</p> <pre><code>public class HomeController : Controller { ///////////////////////////////////////////////////////////// // Attributes public ISearchService SearchService { get; set; } public IFeedbackService FeedbackService { get; set; } </code></pre> <p>The HomeController is responsible for handling searches and storing feedback, so the HomeController has two service references which are created by the IoC.</p> <p>This isn't really what I want. I don't want to have to construct two service for each request on the Home controller, when most of the time they will not be needed. I want to construct them in the action function, when I know for sure they will be needed.</p> <p>Previously, I used Microsoft Unity and there was a lot of <code>ServiceLocator.Resolve&lt;thing&gt;()</code> in my action functions. This is a real pain when it comes to testing, and I have read it is unadvisable (and anti-pattern).</p> <p>My wish is for this:</p> <pre><code>[HttpPost] public ActionResult(FeedbackViewModel vm,IFeedbackService feedbackService) { </code></pre> <p>and have the IoC construct the second parameter but not the first. Is that possible?</p> <p>If not, I will have to find a way to create the members of the service constructor without using a static reference.</p> <p>Thanks for reading.</p> <p><strong>Update using Brent Mannering's answer:</strong></p> <p>I tried creating a ResolveThis parameter but it is impossible to check for the attribute in the BindModel function. So I have stuck with resolving all interfaces:</p> <pre><code>/// &lt;summary&gt; /// /// &lt;/summary&gt; public class DependencyModelBinder : DefaultModelBinder { ///////////////////////////////////////////////////////////// // Attributes private IKernel _kernel; ///////////////////////////////////////////////////////////// // Construction public DependencyModelBinder(IKernel kernel) { // Initialize member variables _kernel = kernel; } ///////////////////////////////////////////////////////////// // Implementation public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // If this is an interface... if(bindingContext.ModelType.IsInterface) { // Resolve the object and return return _kernel.Resolve(bindingContext.ModelType); } // Call base-class function return base.BindModel(controllerContext, bindingContext); } } </code></pre>
    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