Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I inject run-time parameters along with a volatile dependency into a constructor with Castle Windsor?
    primarykey
    data
    text
    <p>Some of my classes have a constructor similar to the following:</p> <pre><code>public class MyComponent : BaseComponent, IMyComponent { public MyComponent(IPostRepository postsRepo, int postId, ICollection&lt;string&gt; fileNames) { // ... } } </code></pre> <p><code>IPostRepository</code> is a volatile dependency, but it can be initialized at application start. The postId and fileNames arguments are only known at run time.</p> <p><strong>How can I use Castle Windsor (3.2.0, if it matters) to handle the injection of the <code>IPostRepository</code> dependency while still allowing run-time constructor parameters?</strong></p> <p>(While one approach might be to refactor <code>MyComponent</code>, this would be a significant undertaking as many other portions of the code already refer to <code>MyComponent</code>.)</p> <p><strong>Here's where I've gotten so far</strong>: I think that I need to create a <code>MyComponentFactory</code>. The interface of <code>MyComponentFactory</code> would look like</p> <pre><code>public interface IMyComponentFactory { IMyComponent Create(params object[] args); } </code></pre> <p>This <code>IMyComponentFactory</code> would be injected into the layer above (the controller in my case) like so:</p> <pre><code>public class MyController : Controller { private IMyComponentFactory _myComponentFactory; public MyController(IMyComponentFactory myComponentFactory) { _myComponentFactory = myComponentFactory; } public ActionResult MyAction(int postId) { List&lt;string&gt; fileNames = new List&lt;string&gt;(); // ... // Creates a new instance of the resolved IMyComponent with the IPostRepository that was injected into IMyComponentFactory and the run time parameters. IMyComponent myComponent = _myComponentFactory.Create(postId, fileNames); // Now do stuff with myComponent return View(); } } </code></pre> <p>Finally, I have attempted to let Castle Windsor create the factory implementation by registering my <code>IMyComponentFactory</code> in the composition root like so:</p> <pre><code>// Add Factory facility container.AddFacility&lt;TypedFactoryFacility&gt;(); container.Register(Component.For&lt;IMyComponentFactory&gt;().AsFactory()); </code></pre> <p>Doing this results in a <code>DependencyResolverException</code> with a message of</p> <blockquote> <p>Could not resolve non-optional dependency for 'Playground.Examples.Components.MyComponent' (Playground.Examples.Components.MyComponent). Parameter 'postId' type 'System.Int32'</p> </blockquote> <p>The error makes sense, and I'm guessing that I need to create a custom implementation of <code>IMyComponentFactory</code>, but I'm not sure how to go about it.</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.
    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