Layered Solution: Multi-Tenancy

Some of the features mentioned in this document may not be available in the free version. We're using the * symbol to indicate that a feature is available in the Team and Higher licenses.

Multi-tenancy is a software architecture where a single instance(codebase) of software runs on a server and serves multiple tenants. Tenants are isolated from each other and can have their own data, configurations, and users. This document explains how the multi-tenancy mechanism works in the layered solution template. You can learn more about multi-tenancy in the Multi-Tenancy, Tenant Management and SaaS * documents.

Multi-Tenancy in Layered Solutions

The layered solution templates use the Multi-Tenancy architecture only if you Enable Multi-Tenancy * option while creating the solution.

saas-module-selection

You can use different databases for each tenant or a shared database for all tenants. In the *SaaS * module, you can specify the database connection strings in the Connection Strings Management Modal. All cached data is isolated by tenant. Each event, background job, and other data is stored with the tenant id.

You can use the ICurrentTenant service to get the current tenant information in your application.

public class MyService : ITransientDependency
{
    private readonly ICurrentTenant _currentTenant;

    public MyService(ICurrentTenant currentTenant)
    {
        _currentTenant = currentTenant;
    }

    public void MyMethod()
    {
        var tenantId = _currentTenant.Id;
        var tenantName = _currentTenant.Name;
    }
}

Additionally, you can use the DataFilter system to disable the tenant filter and list all data in the same database.

public class MyBookService : ITransientDependency
{
    private readonly IDataFilter<IMultiTenant> _multiTenantFilter;
    private readonly IRepository<Book, Guid> _bookRepository;

    public MyBookService(
        IDataFilter<IMultiTenant> multiTenantFilter,
        IRepository<Book, Guid> bookRepository)
    {
        _multiTenantFilter = multiTenantFilter;
        _bookRepository = bookRepository;
    }

    public async Task<List<Book>> GetAllBooksIncludingDeletedAsync()
    {
        //Temporary disable the IMultiTenant filter
        using (_multiTenantFilter.Disable())
        {
            return await _bookRepository.GetListAsync();
        }
    }
}

Contributors


Last updated: December 16, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Watch the Event
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More