Note that there are some explanatory texts on larger screens.

plurals
  1. POCastle Windsor IoC Container Instantiation for multiple concrete types
    primarykey
    data
    text
    <p>I am new to IoC and Castle Windsor.</p> <p>The question is more related to IoC, but i just chose Castle as my weapon of choice.</p> <p>I immediately ran into a problem that it seems impossible to construct the container from a config file or from a singleton.</p> <p>The only way seeming to work for me is to use a Builder function that will construct the whole container each time and then let me specify the types, look at this example:</p> <p>I have a view:</p> <pre><code>public interface IView { } </code></pre> <p>There are 2 concrete implementations of this view:</p> <pre><code>public class ConcreteViewA : IView { } public class ConcreteViewB : IView { } </code></pre> <p>I have a controller that operates on the view:</p> <pre><code>public class Controller { public Controller(IView view) { } public void Load() { } } </code></pre> <p>I create a function that constructs my IoC container and register the common types, the only type i can register is my controller, as this is all that stays the same:</p> <pre><code>WindsorContainer BuildContainer() { var container = new WindsorContainer(); container.Register(Component.For&lt;Controller&gt;().ImplementedBy&lt;Controller&gt;()); return container; } </code></pre> <p>I have 2 (Root) entry points:</p> <pre><code>void RootMethod1() { var container = BuildContainer(); container.Register(Component.For&lt;IView&gt;().ImplementedBy&lt;ConcreteViewA&gt;()); var controller = container.Resolve&lt;Controller&gt;(); controller.Load(); } void RootMethod2() { var container = BuildContainer(); container.Register(Component.For&lt;IView&gt;().ImplementedBy&lt;ConcreteViewB&gt;()); var controller = container.Resolve&lt;Controller&gt;(); controller.Load(); } </code></pre> <p>As you can see I have to rebuild the container each time as i need to tell it what to use for IView for each of the context i am in. What if the Container is expensive to rebuild (Lots of types etc), how can I design this?</p>
    singulars
    1. This table or related slice is empty.
    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