Skip to content

TenantScopedEntity

Base classes for multi-tenant entities with automatic tenant isolation.

TenantScopedEntity

For simple tenant-scoped entities without domain events.

Properties

PropertyTypeDescription
TenantIdGuidThe tenant this entity belongs to

Plus all properties from BaseEntity.

Constructor

ParameterTypeDescription
tenantIdGuidRequired. Cannot be empty.

Usage

csharp
public class Product : TenantScopedEntity
{
    public string Name { get; private set; }
    public decimal Price { get; private set; }

    private Product() { }

    public Product(Guid tenantId, string name, decimal price)
        : base(tenantId)
    {
        Name = name;
        Price = price;
    }
}

TenantScopedAggregateRoot

For tenant-scoped aggregate roots that need domain events.

Properties

PropertyTypeDescription
TenantIdGuidThe tenant this aggregate belongs to

Plus all from AggregateRoot: Id, DomainEvents, etc.

Usage

csharp
public class Order : TenantScopedAggregateRoot
{
    public string OrderNumber { get; private set; }

    private Order() { }

    public Order(Guid tenantId, string orderNumber)
        : base(tenantId)
    {
        OrderNumber = orderNumber;
        RaiseDomainEvent(new OrderCreatedEvent(Id, tenantId));
    }
}

When to Use

ScenarioUse
Simple tenant entityTenantScopedEntity
Aggregate with eventsTenantScopedAggregateRoot
No tenant isolationBaseEntity or AggregateRoot

Released under the MIT License.