The worst bug a SaaS can ship is one customer seeing another customer's data. If your isolation depends on every developer remembering .Where(x => x.TenantId == tenantId) on every query, it's not a matter of if .
Here's the pattern I use in ASP.NET Core and EF Core, taken from a production point of sale and now the foundation of my SaaS Starter Kit .
1. One base class for everything a tenant owns
public interface ITenantOwned { Guid TenantId { get ; } }
public abstract class Entity { public Guid Id { get ; set ; } public DateTimeOffset CreatedAt { get ; set ; } public DateTimeOffset UpdatedAt { get ; set ; } }
public abstract class TenantEntity : Entity , ITenantOwned { public Guid TenantId { get ; set ; } }
Enter fullscreen mode
Exit fullscreen mode
2.β¦