Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ignore MEF for a moment and consider this:</p> <pre><code>public abstract class BaseClass { protected BaseClass(IService service) { } } public class SubClass : BaseClass { public SubClass(IService service) : base(service) { } } </code></pre> <p>Because the base class defines a single constructor, the subclass <strong>must</strong> initialise the base class, by providing an argument for the base class' constructor.</p> <p>Now, with MEF, what I think you are trying to do is this:</p> <pre><code>public abstract class BaseClass { [ImportingConstructor] protected BaseClass(IService service) { } } [Export] public class SubClass : BaseClass { public SubClass() { } } </code></pre> <p>It sounds like you are trying to inject something into the constructor of the base class, but you can't do that, because its the resposibility of the subclass to ensure the appropriate arguments are passed to the base class of the constructor. You need to make a modification to your subclass:</p> <pre><code>[Export] public class SubClass : BaseClass { [ImportingConstructor] public SubClass(IService service) : base(service) { } } </code></pre> <p>Now the subclass will have the service injected and will pass it to the base class.</p> <p>In terms of using Property Injection vs Constructor Injection: it is preferable to initialise a type into a usable state. That being any explicit dependencies should be required in the constructor. But, because MEF will satisfy all imports, by the time you've got your part back from the container, both constructor and property injections would have already occurred, so you could potentially use property imports instead. My preference has always been to go with constructor imports.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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