Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>For current versons of Autofac: ( 2.0+, so anything you should be using today)</strong></p> <p>You register multiple <code>ILoggers</code> (for example):</p> <pre><code>var builder = new ContainerBuilder(); builder.Register&lt;ConsoleLogger&gt;() .As&lt;ILogger&gt;(); builder.Register&lt;EmailLogger&gt;() .As&lt;ILogger&gt;() .PreserveExistingDefaults(); //keeps console logger as the default </code></pre> <p>Then get all <code>ILogger</code>s:</p> <pre><code>var loggers = container.Resolve&lt;IEnumerable&lt;ILogger&gt;&gt;(); </code></pre> <p>You don't need to do anything special, just ask for an <code>IEnumerable&lt;T&gt;</code> of the desired type. Autofac has collection support out of the box, along with other <a href="http://autofac.readthedocs.io/en/latest/advanced/adapters-decorators.html" rel="nofollow noreferrer">adapters</a> that can wrap your components with additional functionality.</p> <p>This is the same usage as the pre-2.x ImplicitCollectionSupportModule, but baked right in.</p> <p><strong>For old versions (0.X - 1.4)</strong> </p> <p>Two ways:</p> <p>1) Use the collection registration</p> <pre><code>var builder = new ContainerBuilder(); builder.RegisterCollection&lt;ILogger&gt;() .As&lt;IEnumerable&lt;ILogger&gt;&gt;(); builder.Register&lt;ConsoleLogger&gt;() .As&lt;ILogger&gt;() .MemberOf&lt;IEnumerable&lt;ILogger&gt;&gt;(); builder.Register&lt;EmailLogger&gt;() .As&lt;ILogger&gt;() .MemberOf&lt;IEnumerable&lt;ILogger&gt;&gt;(); </code></pre> <p>Then:</p> <pre><code>var loggers = container.Resolve&lt;IEnumerable&lt;ILogger&gt;&gt;(); </code></pre> <p>which gives you an IEnumerable.</p> <p>or 2) You can use the ImplicitCollectionSupport module, which will make the code work like newer versions of Autofac:</p> <pre><code>builder.RegisterModule(new ImplicitCollectionSupportModule()); builder.Register(component1).As&lt;ILogger&gt;; builder.Register(component2).As&lt;ILogger&gt;; </code></pre> <p>Then resolve a collection of ILogger rather than looking for resolving all.</p> <pre><code>var loggers = container.Resolve&lt;IEnumerable&lt;ILogger&gt;&gt;(); </code></pre> <p>which gives you an IEnumerable, again.</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. 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.
 

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