-
Notifications
You must be signed in to change notification settings - Fork 162
Extra help for shading‐only apps
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:
- 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 tenant create / delete.
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, but the example below uses the SingleLevel approach.
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);
}
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.
????????????????????????????????????????????????????????
- 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