Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A 'module' feature is deliberately left out of the Simple Injector core library, but there is a <a href="https://nuget.org/packages/SimpleInjector.Packaging" rel="nofollow noreferrer">SimpleInjector.Packaging NuGet package</a> that allows you to do this. 'Package' is the term Simple Injector uses. This library however, is nothing more than one <code>IPackage</code> interface and two extension methods. You can achieve the same by writing code like this:</p> <p>A package:</p> <pre class="lang-cs prettyprint-override"><code>public static class BootstrapperPackage { public static void RegisterServices(Container container) { container.Register&lt;IBar, Bar&gt;(Lifestyle.Scoped); container.Register&lt;IFoo, Foo&gt;(Lifestyle.Singleton); } } </code></pre> <p>In your composition root:</p> <pre class="lang-cs prettyprint-override"><code>public static void Main(string[] args) { var container = new SimpleInjector.Container(); BootstrapperPackage.RegisterServices(container); ... } </code></pre> <p>The difference with the <a href="https://nuget.org/packages/SimpleInjector.Packaging" rel="nofollow noreferrer">SimpleInjector.Packaging NuGet package</a> is that this package defines an interface for you, and allows you to dynamically load multiple packages in one single line:</p> <pre><code>public class BusinessLayerPackage : IPackage { public void RegisterServices(Container container) { container.Register&lt;IBar, Bar&gt;(Lifestyle.Scoped); container.Register&lt;IFoo, Foo&gt;(Lifestyle.Singleton); } } public static void Main(string[] args) { var container = new SimpleInjector.Container(); container.RegisterPackages(AppDomain.CurrentDomain.GetAssemblies()); } </code></pre> <p>However, if you don't really need dynamic loading, using static methods (as shown above) is preferred, because it has the following advantages:</p> <ul> <li>Makes loading modules very explicit and discoverable.</li> <li>Makes it easy to select which modules to load and which not.</li> <li>Makes it easy to pass along extra values to the <code>RegisterServices</code> methods, such as configuration values that such module requires. This prevents the module from taking a hard dependency on the configuration system.</li> </ul> <p>For more information, please read <a href="https://simpleinjector.org/howto#package-registrations" rel="nofollow noreferrer">this</a>.</p>
 

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