Git Product home page Git Product logo

tenantcore's Introduction

NuGet version Build Status Analytics

TenantCore

TenantCore is an OWIN Middleware that it can help to resolve tenants, a multitenancy middleware, by request's hostname for example or by any resolver. You can create your own Tenant's resolver or use the default one. The tenant can have a database connection string or any property that you need.

To start you need to setup the Resolver and Tenants in your OWIN AppBuilder, for example:

public void ConfigureAuth(IAppBuilder app)
{
    // SetUp Multitenancy with TenantCore
    var tenants = new List<ITenant>
    {
        new Tenant("local", "localhost", @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\local.mdf;Initial Catalog=local;Integrated Security=True"),
        new Tenant("sample", "sample.local", @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\sample.mdf;Initial Catalog=sample;Integrated Security=True"),
        new Tenant("fake", "fake.local", @"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\fake.mdf;Initial Catalog=fake;Integrated Security=True")
    };
            
    app.UseTenantCore(new HostNameTenantResolver(tenants));

    // Configure the db context and user manager to use a single instance per request
    app.CreatePerOwinContext<ApplicationDbContext>(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

    // And continue with your middleware and settings
}

If you want to have a database per tenant, you need to change your DbContext or IdentityDbContext to have a Create method with parameters:

public static ApplicationDbContext Create(IdentityFactoryOptions<ApplicationDbContext> options,
    IOwinContext context)
{
    return context.GetDbContext<ApplicationDbContext>();
}

The GetDbContext is an extension method in TenantCore, it will find the current Tenant and get the ConnectionString. The ConnectionString will be used with your DbContext, so be sure to have a constructor with connectionstring param like this:

public ApplicationDbContext(string connectionString)
            : base(connectionString, throwIfV1Schema: false) {}

Now in your ApiController you can use the Tenant or DbContext with some extension methods, for example:

[HttpGet, Route("register/{id}")]
public async Task<IHttpActionResult> Register(string id)
{
    // Gets the DbContext related to current Tenant
    var database = HttpContext.Current.GetDbContext<ApplicationDbContext>();
    // Gets the current Tenant
    var tenant = HttpContext.Current.GetCurrentTenant();
}

tenantcore's People

Contributors

geoperez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.