-
Notifications
You must be signed in to change notification settings - Fork 160
Extra help for shading‐only apps
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:
- Replaced the old services for handling sharding entries due to a limitation (see How AuthP handles sharding).
- The ability to define a sharding-only mode - see Configuring sharding > sharding-only mode
- 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. - A
IShardingOnlyTenantAddRemove
to simplify the create / delete of sharding-only tenants.
But what I want to focus on in this document is...
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.
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.
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.
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.
- Intro to multi-tenants (ASP.NET video)
- Articles in date order:
- 0. Improved Roles/Permissions
- 1. Setting up the database
- 2. Admin: adding users and tenants
- 3. Versioning your app
- 4. Hierarchical multi-tenant
- 5. Advanced technique with claims
- 6. Sharding multi-tenant setup
- 7. Three ways to add new users
- 8. The design of the sharding data
- 9. Down for maintenance article
- 10: Three ways to refresh claims
- 11. Features of Multilingual service
- 12. Custom databases - Part1
- Videos (old)
- Authentication explained
- Permissions explained
- Roles explained
- AuthUser explained
- Multi tenant explained
- Sharding explained
- How AuthP handles sharding
- How AuthP handles errors
- Languages & cultures explained
- JWT Token refresh explained
- Setup Permissions
- Setup Authentication
- Startup code
- Setup the custom database feature
- JWT Token configuration
- Multi tenant configuration
- Using Permissions
- Using JWT Tokens
- Creating a multi-tenant app
- Supporting multiple languages
- Unit Test your AuthP app