|
| 1 | +# Layered Solution: Multi-Tenancy |
| 2 | + |
| 3 | +```json |
| 4 | +//[doc-nav] |
| 5 | +{ |
| 6 | + "Previous": { |
| 7 | + "Name": "Swagger integration", |
| 8 | + "Path": "solution-templates/layered-web-application/swagger-integration" |
| 9 | + }, |
| 10 | + "Next": { |
| 11 | + "Name": "BLOB storing", |
| 12 | + "Path": "solution-templates/layered-web-application/blob-storing" |
| 13 | + } |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +> 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](https://abp.io/pricing)** and **[Higher](https://abp.io/pricing)** licenses. |
| 18 | +
|
| 19 | +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](../../framework/architecture/multi-tenancy/index.md), [Tenant Management](../../modules/tenant-management.md) and [SaaS **\***](../../modules/saas.md) documents. |
| 20 | + |
| 21 | +## Multi-Tenancy in Layered Solutions |
| 22 | + |
| 23 | +The layered solution templates use the *Multi-Tenancy* architecture only if you *Enable Multi-Tenancy **\**** option while creating the solution. |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +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](../../modules/saas.md#connection-string). All cached data is isolated by tenant. Each event, background job, and other data is stored with the tenant id. |
| 28 | + |
| 29 | +You can use the `ICurrentTenant` service to get the current tenant information in your application. |
| 30 | + |
| 31 | +```csharp |
| 32 | +public class MyService : ITransientDependency |
| 33 | +{ |
| 34 | + private readonly ICurrentTenant _currentTenant; |
| 35 | + |
| 36 | + public MyService(ICurrentTenant currentTenant) |
| 37 | + { |
| 38 | + _currentTenant = currentTenant; |
| 39 | + } |
| 40 | + |
| 41 | + public void MyMethod() |
| 42 | + { |
| 43 | + var tenantId = _currentTenant.Id; |
| 44 | + var tenantName = _currentTenant.Name; |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Additionally, you can use the [DataFilter](../../framework/infrastructure/data-filtering.md#idatafilter-service-enabledisable-data-filters) system to disable the tenant filter and list all data in the same database. |
| 50 | + |
| 51 | +```csharp |
| 52 | +public class MyBookService : ITransientDependency |
| 53 | +{ |
| 54 | + private readonly IDataFilter<IMultiTenant> _multiTenantFilter; |
| 55 | + private readonly IRepository<Book, Guid> _bookRepository; |
| 56 | + |
| 57 | + public MyBookService( |
| 58 | + IDataFilter<IMultiTenant> multiTenantFilter, |
| 59 | + IRepository<Book, Guid> bookRepository) |
| 60 | + { |
| 61 | + _multiTenantFilter = multiTenantFilter; |
| 62 | + _bookRepository = bookRepository; |
| 63 | + } |
| 64 | + |
| 65 | + public async Task<List<Book>> GetAllBooksIncludingDeletedAsync() |
| 66 | + { |
| 67 | + //Temporary disable the IMultiTenant filter |
| 68 | + using (_multiTenantFilter.Disable()) |
| 69 | + { |
| 70 | + return await _bookRepository.GetListAsync(); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
0 commit comments