Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing parameters to constructors using Autofac
    primarykey
    data
    text
    <p>I'm very new to autofac so it's possible that I'm completely misusing it.</p> <p>Let's say I have a class that has this structure:</p> <pre><code>public class HelperClass : IHelperClass { public HelperClass(string a, string b) { this.A = a; this.B = b; } } </code></pre> <p>and I have two classes that use that class, but require different defaults for the constructor. The second constructor is JUST for testing purposes -- we will always want a HelperClass in the "real" app.:</p> <pre><code>public class DoesSomething: IDoesSomething { public DoesSomething() : this(new HelperClass("do", "something")); { } internal DoesSomething(IHelperClass helper) { this.Helper = helper; } } public class DoesSomethingElse : IDoesSomethingElse { public DoesSomethingElse() : this(new HelperClass("does", "somethingelse")); { } internal DoesSomethingElse(IHelperClass helper) { this.Helper = helper; } } </code></pre> <p>Here's my AutoFac module:</p> <pre><code>public class SomethingModule: Module { protected override void Load(ContainerBuilder builder) { builder.RegisterType&lt;DoesSomething&gt;().As&lt;IDoesSomething&gt;(); builder.RegisterType&lt;DoesSomethingElse&gt;().As&lt;IDoesSomethingElse(); } } </code></pre> <p>My question(s):</p> <ol> <li>When I call resolve on DoesSomething or DoesSomethignElse -- will it resolve the internal constructor instead of the public one? Do I need to leave IHelperClass unregistered?</li> <li>If yes, how do I make it pass different parameters to each instance of IHelperClass depending on whether it's used in DoesSomething or DoesSomethingElse?</li> </ol>
    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