Skip to content

Extra help for shading‐only apps

Jon P Smith edited this page Aug 13, 2023 · 8 revisions

In my article "The (long) journey to a better sharding multi-tenant application" ??LINK?? I talk about the various issues I encountered when trying to build a multi-tenant applications where every tenant's data that have their own database (referred to as sharding-only). The learning from this caused me to create the version 6 of AuthP library. This document lists the new features added by version 6. They are:

  1. Replaced the old services for handling sharding entries due to a limitation (see How AuthP handles sharding).
  2. The ability to define a sharding-only mode - see Configuring sharding > sharding-only mode
  3. The Sign up for a new tenant, with versioning now works with sharding-only tenants. See the use of the "Sign up now" in Example7's HomeController's CreateTenant actions.
  4. A IShardingOnlyTenantAddRemove to simplify tenant create / delete.

But what I want to focus on in this document is...

4. The IShardingOnlyTenantAddRemove service to simplify tenant create / delete

When you create a new tenant you also need a sharding entry (see How AuthP handles sharding about sharding entries) to link the tenant to the database where the tenant's data is stored. If you are in hybrid mode, then you have to two steps because its not a one-to-one between the tenant and its database. But if your sharding-only mode then there IS and one-to-one link between tenant and its database, which allows to simplify the create tenant into one step, and the IShardingOnlyTenantAddRemove service does that (and delete a tenant too).

NOTE: This service works for both SingleLevel and Hierarchical tenants, but the example below uses the SingleLevel approach.

Create a new tenant

To use the IShardingOnlyTenantAddRemove's CreateTenantAsync method will creates an new the tenant and its sharding entry at the same time. The CreateTenantAsync method needs information to create the tenant. They are

  • Tenant name – from the admin user's input
  • The connection string holding the database server (code or human – see diagram below)
  • Set the database provider used to create the tenant database (e.g. SqlServer) is set by your code

The diagram below shows two web pages that the app admin user to create a new tenant on one go. The two pages show the two types of app: whether you have a single database server or multiple database servers. NOTE: The Example7’s TenantController in the AuthP repo has an example of the multiple database servers option.

TwoWaysToCreateTenant

NOTE: The Example7’s TenantController in the AuthP repo has an example of the multiple database servers option.

The CreateTenantAsync method takes in a class called ShardingOnlyTenantAddDto which holds the create data, plus has methods to help you to set up the fixed data. This code (taken from the Example7’s TenantController) works for BOTH one server and multiple servers and is shown below

[HasPermission(Example7Permissions.TenantCreate)]
public IActionResult Create([FromServices] IGetSetShardingEntries shardingService)
{
    var dto = new ShardingOnlyTenantAddDto
    {
        //set which database provider that you tenant DbContext uses
        DbProviderShortName = AuthPDatabaseTypes.SqlServer.ToString()
    };
    dto.SetConnectionStringNames(shardingService.GetConnectionStringNames());
    return View(dto);
}

NOTE: The name of the database and the sharding entry is created by the ShardingOnlyTenantAddDto's FormDatabaseInformation method which uses time as the database name, e.g. 20230808101149-892. If you don't want to use a name taken out of time, then you can create a new class which inherits the ShardingOnlyTenantAddDto class which allows you to overridden the FormDatabaseInformation with your own version.

????????????????????????????????????????????????????????

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally