agentleFS
elidadutra187elidadutra187/claude-skills/skills/abp-multi-tenancy/SKILL.md

abp-multi-tenancy

Skill0 starsChanged 5 days agoMITView on GitHub

Use when working with multi-tenant features, tenant-specific data isolation, or switching tenant context. Covers IMultiTenant interface, CurrentTenant, CurrentTenant.Change(), DataFilter.Disable(IMultiTenant), tenant resolution order, and database-per-tenant.

1 other project uses a version of this, 1 of them edited.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use CurrentTenant property (available in base classes) or inject ICurrentTenant:

    line 41
  2. Very specific:

    Use CurrentTenant.Change() to temporarily switch tenant (useful in host context):

    line 65
  3. Distinctive:

    Test both host and tenant contexts: - ensure proper data isolation

    line 132
  4. Very specific:

    Check MultiTenancyConsts.IsEnabled in your solution for centralized control.

    line 144

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

ABP Multi-Tenancy

Docs: https://abp.io/docs/latest/framework/architecture/multi-tenancy

Making Entities Multi-Tenant

Implement IMultiTenant interface to make entities tenant-aware:

public class Product : AggregateRoot<Guid>, IMultiTenant
{
    public Guid? TenantId { get; set; } // Required by IMultiTenant

    public string Name { get; private set; }
    public decimal Price { get; private set; }

    protected Product() { }

    public Product(Guid id, string name, decimal price) : base(id)
    {
        Name = name;
        Price = price;
        // TenantId is automatically set from CurrentTenant.Id
    }
}

Key points:

  • TenantId is nullable - null means entity belongs to Host
  • ABP automatically filters queries by current tenant
  • ABP automatically sets TenantId when creating entities

Accessing Current Tenant

Use CurrentTenant property (available in base classes) or inject ICurrentTenant:

public class ProductAppService : ApplicationService
{
    public async Task DoSomethingAsync()
    {
        // Available from base class
        var tenantId = CurrentTenant.Id;        // Guid? - null for host
        var tenantName = CurrentTenant.Name;    // string?
        var isAvailable = CurrentTenant.IsAvailable; // true if Id is not null
    }
}

// In other services
public class MyService : ITransientDependency
{
    private readonly ICurrentTenant _currentTenant;
    public MyService(ICurrentTenant currentTenant) => _currentTenant = currentTenant;
}

Switching Tenant Context

Use CurrentTenant.Change() to temporarily switch tenant (useful in host context):

public class ProductManager : DomainService
{
    private readonly IRepository<Product, Guid> _productRepository;

    public async Task<long> GetProductCountAsync(Guid? tenantId)
    {
        // Switch to specific tenant
        using (CurrentTenant.Change(tenantId))
        {
            return await _productRepository.GetCountAsync();
        }
        // Automatically restored to previous tenant after using block
    }

    public async Task DoHostOperationAsync()
    {
        // Switch to host context
        using (CurrentTenant.Change(null))
        {
            // Operations here are in host context
        }
    }
}

Important: Always use Change() with a using statement.

Disabling Multi-Tenant Filter

To query all tenants' data (only works with single database):

public class ProductManager : DomainService
{
    public async Task<long> GetAllProductCountAsync()
    {
        // DataFilter is available from base class
        using (DataFilter.Disable<IMultiTenant>())
        {
            return await _productRepository.GetCountAsync();
            // Returns count from ALL tenants
        }
    }
}

Note: This doesn't work with separate databases per tenant.

Database Architecture Options

ApproachDescriptionUse Case
Single DatabaseAll tenants share one databaseSimple, cost-effective
Database per TenantEach tenant has dedicated databaseData isolation, compliance
HybridMix of shared and dedicatedFlexible, premium tenants

Connection strings are configured per tenant in Tenant Management module.

Best Practices

  1. Always implement IMultiTenant for tenant-specific entities
  2. Never manually filter by TenantId - ABP does it automatically
  3. Don't change TenantId after creation - it moves entity between tenants
  4. Use Change() scope carefully - nested scopes are supported
  5. Test both host and tenant contexts - ensure proper data isolation
  6. Consider nullable TenantId - entity may be host-only or shared

Enabling Multi-Tenancy

Configure<AbpMultiTenancyOptions>(options =>
{
    options.IsEnabled = true; // Enabled by default in ABP templates
});

Check MultiTenancyConsts.IsEnabled in your solution for centralized control.

Tenant Resolution

ABP resolves current tenant from (in order):

  1. Current user's claims
  2. Query string (?__tenant=...)
  3. Route (/{__tenant}/...)
  4. HTTP header (__tenant)
  5. Cookie (__tenant)
  6. Domain/subdomain (if configured)

For subdomain-based resolution:

Configure<AbpTenantResolveOptions>(options =>
{
    options.AddDomainTenantResolver("{0}.mydomain.com");
});

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.