Multi-Tenancy of Data Sources
Last updated
Last updated
Software-as-a-Service (SaaS) applications and Enterprise-level applications often need to segregate data between different tenants of the application, that could be different customers or different departments of the same company.
By default, the kernel library doesn't provides any set of abstractions and implementations to support multi-tenancy in your application, but the single drivers can provide it, accordingly to their specific capabilities.
In-Memory
MongoDB
Entity Framework Core
On a general basis, the tenant context is resolved through the identity of a user of the application, using mechanisms like claims or roles (see at how this is implemented in ASP.NET Core).
Some scenarios anyway require the access to those segregated information from a service or a background task, where the user identity is not available: for this reason the framework provides an abstraction named IRepositoryProvider<TEntity>
that will be used to resolve the repository to access the data, for the tenant identifier.
To learn more about the usage of the IRepositoryProvider<TEntity>
interface, you can read the documentation .
The preferred approach of the library is to use the framework to implement multi-tenant applications, and to use the ITenantInfo
interface to retrieve the current tenant information: this is obtained by scanning the current HTTP request, and retrieving the tenant information from the request.
In some cases, like in background services, where the identity of the tenant is not available through the user (eg. machine-to-machine communication), it is possible to obtain the repository for a specific tenant by using the IRepositoryProvider<TEntity>
interface: these are still drivers-specific, and produce instances of the repository for a specific tenant and specific driver.
IRepositoryProvider<TEntity>
interfaceThe IRepositoryProvider<TEntity>
exposes a single method that allows to obtain an instance of IRepository<TEntity>
for a specific tenant.
Every driver that supports multi-tenancy will implement this interface, and the IRepository<TEntity>
instance returned will be specific for the tenant identifier passed as parameter.