0

I'm developing a multi-teant SaaS application in ASP.NET core mvc and I was wondering what the general approach is to applying tenant logic in a shared database scenario (TenantId for each entity). And more specificly, where this logic should be applied; In the Context, UnitOfWork, Repository or Service?

My current architecture looks like this:

EF DbContext -> UnitOfWork -> Repository -> Service

Right now, I'm applying all of my logic in the EF DbContext. For example applying the TenantId to a entity:

public override int SaveChanges( )
{
    foreach ( var entityEntry in ChangeTracker.Entries( ) )
    {
        if ( entityEntry.Entity is ITenantEntity entity )
        {
            if ( entityEntry.State == EntityState.Added ||
                 entityEntry.State == EntityState.Modified )
            {
                entity.TenantId = _tenantProvider.GetTenantId( );
            }
        }
    }

    return base.SaveChanges( );
}

And filtering out data based on tenant:

protected override void OnModelCreating( ModelBuilder builder )
{
    base.OnModelCreating( builder );

    ... omitted ....

    builder.Entity<Person>( )
        .HasQueryFilter( p => p.TenantId == _tenantProvider.GetTenantId( ) );

    builder.Entity<Address>( )
        .HasQueryFilter( p => p.TenantId == _tenantProvider.GetTenantId( ) );

    builder.Entity<Company>( )
        .HasQueryFilter( p => p.TenantId == _tenantProvider.GetTenantId( ) );

    ... omitted ....
}
0

Basically you only have two options

  1. Add tenantId onto your root objects and just have a universal repository

  2. Hide tenantId from the service by having a repository per tenant

1 is far easier, but it looks like you are doing 2.

The key thing is to work out what you persist between requests and make sure you can reuse connections and objects without running out of memory because you have an array of a million repositories

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.