Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given your use case description, I'd say you have a few of options.</p> <p>First, <strong>you could just have each application register their own set of dependencies</strong> including lifetime scope. Having one or two "duplicate" pieces of code in this respect isn't that big of a deal considering the differences between the application and the fact that the registrations appear fairly small.</p> <p>Second, <strong>you could wrap the common part (minus lifetime scope) into a ContainerBuilder extension method</strong> that could be used in each application. It would still mean each app has a little "duplicate code" but the common logic would be wrapped in a simple extension.</p> <pre><code>public static IRegistrationBuilder&lt;TLimit, ScanningActivatorData, DynamicRegistrationStyle&gt; RegisterConnection&lt;TLimit, ScanningActivatorData, DynamicRegistrationStyle&gt;(this ContainerBuilder builder) { // Put the common logic here: builder.Register(...).AsImplementedInterfaces(); } </code></pre> <p>Consuming such an extension in each app would look like:</p> <pre><code>builder.RegisterConnection().InstancePerHttpRequest(); // or builder.RegisterConnection().InstancePerLifetimeScope(); </code></pre> <p>Finally, if you know it's either web or non-web, <strong>you could make a custom module that handles the switch</strong>:</p> <pre><code>public class ConnectionModule : Autofac.Module { bool _isWeb; public ConnectionModule(bool isWeb) { this._isWeb = isWeb; } protected override void Load(ContainerBuilder builder) { var reg = builder.Register(...).AsImplementedInterfaces(); if(this._isWeb) { reg.InstancePerHttpRequest(); } else { reg.InstancePerLifetimeScope(); } } } </code></pre> <p>In each application, you could then register the module:</p> <pre><code>// Web application: builder.RegisterModule(new ConnectionModule(true)); // Non-web application: builder.RegisterModule(new ConnectionModule(false)); </code></pre> <p>Alternatively, you mentioned your lifetime scope in your other apps has a name. <strong>You could make your module take the name</strong>:</p> <pre><code>public class ConnectionModule : Autofac.Module { object _scopeTag; public ConnectionModule(object scopeTag) { this._scopeTag = scopeTag; } protected override void Load(ContainerBuilder builder) { var reg = builder.Register(...) .AsImplementedInterfaces() .InstancePerMatchingLifetimeScope(this._scopeTag); } } </code></pre> <p>Consumption is similar:</p> <pre><code>// Web application (using the standard tag normally provided): builder.RegisterModule(new ConnectionModule("httpRequest")); // Non-web application (using your custom scope name): builder.RegisterModule(new ConnectionModule("yourOtherScopeName")); </code></pre> <p><strong>I would recommend against simply using <code>InstancePerLifetimeScope</code> in a web application unless that's actually what you intend.</strong> As noted in other answers/comments, <code>InstancePerHttpRequest</code> uses a specific named lifetime scope so that it's safe to create child lifetime scopes; using <code>InstancePerLifetimeScope</code> doesn't have such a restriction so <em>you'll actually get one connection per child scope rather than one connection for the request</em>. I, personally, don't assume that other developers won't make use of child lifetime scopes (<a href="http://nblumhardt.com/2011/01/an-autofac-lifetime-primer/" rel="noreferrer">which is a recommended practice</a>), so in my applications I'm very specific. If you're in total control of your application and you can assure that you aren't creating additional child scopes or that you actually do want one connection per scope, then maybe <code>InstancePerLifetimeScope</code> will solve your problem.</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.
 

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