/Autofac.Multitenant

Multitenant application support for Autofac IoC

Primary LanguageC#MIT LicenseMIT

Autofac.Multitenant

Multitenant application support for Autofac IoC.

Build status codecov

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

BREAKING CHANGE: As of v4.0.0, the Autofac.Extras.Multitenant package is Autofac.Multitenant.

Quick Start

// First, create your application-level defaults using a standard
// ContainerBuilder, just as you are used to.
var builder = new ContainerBuilder();
builder.RegisterType<Consumer>()
  .As<IDependencyConsumer>()
  .InstancePerDependency();
builder.RegisterType<BaseDependency>()
  .As<IDependency>()
  .SingleInstance();
var appContainer = builder.Build();

// Once you've built the application-level default container, you
// need to create a tenant identification strategy that implements
// ITenantIdentificationStrategy. This is how the container will
// figure out which tenant is acting and needs dependencies resolved.
var tenantIdentifier = new MyTenantIdentificationStrategy();

// Now create the multitenant container using the application
// container and the tenant identification strategy.
var mtc = new MultitenantContainer(tenantIdentifier, appContainer);

// Configure the overrides for each tenant by passing in the tenant ID
// and a lambda that takes a ContainerBuilder.
mtc.ConfigureTenant(
  '1',
  b => b.RegisterType<Tenant1Dependency>()
    .As<IDependency>()
    .InstancePerDependency());
mtc.ConfigureTenant(
  '2',
  b => b.RegisterType<Tenant2Dependency>()
    .As<IDependency>()
    .SingleInstance());

// Now you can use the multitenant container to resolve instances.

Check out the documentation for more usage details.

Get Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.