Skip to content

Extra help for shading‐only apps

Jon P Smith edited this page Dec 20, 2023 · 8 revisions

In my article "The (long) journey to a better sharding multi-tenant application" 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 building a sharding-only multi-tenant appthis 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 the create / delete of sharding-only tenants.

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.

Create a new shard-only 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);
}

If you are creating a hierarchical tenant, then you also need to call the ShardingOnlyTenantAddDto's FillListOfHierarchicalTenants method in the action shown above. This call will set up the ListOfHierarchicalTenants property in the ShardingOnlyTenantAddDto which contains all the possible hierarchical tenants. This lets the admin user to either add the new tenant to an existing hierarchical tenant (known as the ParentTenantId) or creating a new, top-level entry. See Creating a new hierarchical Tenant for more on this.

NOTE: In the cases where a new, top-level tenant, then the name of the database and the sharding entry is created by the ShardingOnlyTenantAddDto's FormDatabaseInformation method. This method creates time-based 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.

Delete a tenant

This is very easy to use because you only have to provide the TenantId and it work out what to do. The code to this method is shown below:

var status = await service.DeleteTenantAsync(input.TenantId);

This method will work on any type of tenant (shared or shard-only tenants and SingleLevel and Hierarchical tenants, including hierarchical child tenants). The method reads in the Tenant which contains all the information the method needs to delete tenant, and if the tenant is a shard-only and not a hierarchical child tenant it will also delete its sharding entry.

Additional resources

Articles / Videos

Concepts

Setup

Usage

Admin

SupportCode

Clone this wiki locally