2

I'm building a multi-tenant MVC 4 application and using Autofac as the container.

I've been looking at the Autofac MultiTenant library, however unless I have missed something I don't think it really fits what I need. This is my situation:

  • I don't know all of the tenants up front and new ones will be created during execution
  • Tenants dont have differing dependencies, only the data stored within the instances
  • I have some instances that need to be per tenant, to ensure no leakage of data
  • I have some instances that will be single instance in the root container (not a problem)
  • I have some instances that will be per http request (not a problem)
  • I determine a tenant based off the URL they access the web site with

I haven't been able to find anything that fits my needs so, I think I need to look at a way to create my own lifetime scopes per tenant. How would I go about creating such as thing?

tereško
  • 58,060
  • 25
  • 98
  • 150
Glenn Morton
  • 444
  • 4
  • 15

1 Answers1

3

[I previously answered and indicated you couldn't register new tenants on the fly. That was incorrect and I'm updating accordingly.

I think you can do what you want with Autofac.Extras.Multitenant, though you'll want to test it thoroughly (and let us know if it's broken).

  • Tenants don't have differing dependencies, only the data stored within the instances: Register common dependencies at the container level, but for instances that have different data per tenant, register those as InstancePerTenant.
  • I have some instances that need to be per tenant, to ensure no leakage of data: Use the InstancePerTenant registration extension.
  • I determine a tenant based off the URL they access the web site with: Implement your own ITenantIdentificationStrategy that looks at the URL and converts to a tenant ID.

The new tenants need to be registered during execution item was something I previously was thinking wouldn't work but now I think it will.

When you create a tenant at app startup, it's like this:

// Configure application-level defaults.
var builder = new ContainerBuilder();
builder.RegisterType<Consumer>().As<IDependencyConsumer>().InstancePerDependency();
builder.RegisterType<BaseDependency>().As<IDependency>().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 => b.RegisterType<Tenant1Dependency>().As<IDependency>().InstancePerDependency());
mtc.ConfigureTenant('2', b => b.RegisterType<Tenant2Dependency>().As<IDependency>().SingleInstance());

// Set the MVC dependency resolver.
DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc));

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).

I think it'd work something like this:

// Get the current application container.
var mtc = AutofacDependencyResolver.Current.ApplicationContainer as MultitenantContainer;

// Configure the new tenant.
mtc.ConfigureTenant('3', b => b.RegisterType<Tenant3Dependency>().As<IDependency>().InstancePerDependency());

Actually, I think it's as simple as that. Again, as long as you don't try to reconfigure an existing tenant you should be OK.

Travis Illig
  • 23,195
  • 2
  • 62
  • 85
  • It might even be the case that @Glenn means dynamically creating tenant instances, not creating new kinds of tenants? Think that would make Autofac MultiTenant a home run :) – Nicholas Blumhardt Mar 15 '13 at 21:59
  • @NicholasBlumhardt I'm going to slightly amend my answer: Reinvestigating, I don't see why you wouldn't be able to add a tenant on the fly. As long as you don't duplicate tenant IDs... it should be OK. – Travis Illig Mar 15 '13 at 23:34
  • I'll give it a test when I have some time and update this questions with the results. @NicholasBlumhardt you were on the right track, I only require one type of tenant with lots of instances that are to be created on the fly. I haven't looked yet, but is there a way to confirm previously registered tenant ID's? – Glenn Morton Apr 02 '13 at 16:07