Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>[I previously answered and indicated you couldn't register new tenants on the fly. That was incorrect and I'm updating accordingly.</p> <p>I think you can do what you want with Autofac.Extras.Multitenant, though you'll want to test it thoroughly (and <a href="https://code.google.com/p/autofac/issues/list" rel="nofollow">let us know if it's broken</a>).</p> <ul> <li><strong>Tenants don't have differing dependencies, only the data stored within the instances</strong>: Register common dependencies at the container level, but for instances that have different data per tenant, register those as <code>InstancePerTenant</code>.</li> <li><strong>I have some instances that need to be per tenant, to ensure no leakage of data</strong>: Use the <code>InstancePerTenant</code> registration extension.</li> <li><strong>I determine a tenant based off the URL they access the web site with</strong>: Implement your own <code>ITenantIdentificationStrategy</code> that looks at the URL and converts to a tenant ID.</li> </ul> <p>The <strong>new tenants need to be registered during execution</strong> item was something I previously was thinking wouldn't work but now I think it will.</p> <p>When you create a tenant at app startup, it's like this:</p> <pre><code>// Configure application-level defaults. var builder = new ContainerBuilder(); builder.RegisterType&lt;Consumer&gt;().As&lt;IDependencyConsumer&gt;().InstancePerDependency(); builder.RegisterType&lt;BaseDependency&gt;().As&lt;IDependency&gt;().SingleInstance(); var appContainer = builder.Build(); // Configure tenant identification and start the multitenant container. var tenantIdentifier = new MyTenantIdentificationStrategy(); var mtc = new MultitenantContainer(tenantIdentifier, appContainer); // Configure overrides for existing tenants. mtc.ConfigureTenant('1', b =&gt; b.RegisterType&lt;Tenant1Dependency&gt;().As&lt;IDependency&gt;().InstancePerDependency()); mtc.ConfigureTenant('2', b =&gt; b.RegisterType&lt;Tenant2Dependency&gt;().As&lt;IDependency&gt;().SingleInstance()); // Set the MVC dependency resolver. DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc)); </code></pre> <p>If you need to create a tenant during app runtime, you should be able to do that as long as you haven't previously configured the tenant (no duplicate tenant IDs).</p> <p>I think it'd work something like this:</p> <pre><code>// Get the current application container. var mtc = AutofacDependencyResolver.Current.ApplicationContainer as MultitenantContainer; // Configure the new tenant. mtc.ConfigureTenant('3', b =&gt; b.RegisterType&lt;Tenant3Dependency&gt;().As&lt;IDependency&gt;().InstancePerDependency()); </code></pre> <p>Actually, I think it's as simple as that. Again, as long as you don't try to <em>reconfigure an existing tenant</em> you should be OK.</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