Hazina Entity Types: Soft Delete, Multi-Tenant, and Ownership
Hazina Framework

Hazina Entity Types: Soft Delete, Multi-Tenant, and Ownership

Not every entity needs the same features. A simple lookup table doesn’t need soft delete. A user’s private document needs ownership enforcement. Hazina gives you four entity base classes to match your needs.

The Four Base Classes

Base Class Features Use Case
EntityBase ID, CreatedAt, UpdatedAt Simple entities, lookup tables
SoftDeleteEntityBase + IsDeleted, DeletedAt Entities that should never truly disappear
TenantEntityBase + TenantId, automatic filtering Multi-tenant SaaS applications
OwnedEntityBase + OwnerId, access control User-owned content

EntityBase – The Foundation

public class Category : EntityBase
{
    public string Name { get; set; } = string.Empty;
}
// Gives you: Id, CreatedAt, UpdatedAt

SoftDeleteEntityBase – Nothing Is Ever Gone

public class Invoice : SoftDeleteEntityBase
{
    public string Number { get; set; } = string.Empty;
    public decimal Amount { get; set; }
}
// DELETE /api/invoices/42 sets IsDeleted=true, DeletedAt=now
// GET /api/invoices automatically excludes deleted records
// GET /api/invoices?includeDeleted=true shows everything

TenantEntityBase – Automatic Data Isolation

public class Project : TenantEntityBase
{
    public string Name { get; set; } = string.Empty;
}
// Every query is automatically filtered by TenantId
// Tenant A never sees Tenant B's data
// No manual WHERE clauses needed

OwnedEntityBase – User-Level Access Control

public class Document : OwnedEntityBase
{
    public string Title { get; set; } = string.Empty;
    public string Content { get; set; } = string.Empty;
}
// Only the owner can see/edit their documents
// OwnerId is set automatically from the authenticated user

Combining Features

Need soft delete AND multi-tenancy? Create your own base:

public abstract class TenantSoftDeleteEntity : TenantEntityBase
{
    public bool IsDeleted { get; set; }
    public DateTime? DeletedAt { get; set; }
}

The framework respects all configured behaviors automatically.

In the next post, we’ll deep dive into multi-tenancy – how Hazina handles data isolation in SaaS applications.

Frequently Asked Questions

What are the different entity types in Hazina?

Hazina provides four base classes: EntityBase for simple entities, SoftDeleteEntityBase for entities that can be soft deleted, TenantEntityBase for multi-tenant applications, and OwnedEntityBase for user-owned content. Each class is designed to meet specific use cases and requirements.

How does the SoftDeleteEntityBase work in Hazina?

The SoftDeleteEntityBase allows entities to be marked as deleted without being permanently removed from the database. When an entity is deleted, the IsDeleted flag is set to true, and the DeletedAt timestamp is recorded, enabling the option to retrieve deleted records if needed.

What features does TenantEntityBase provide for multi-tenant applications?

TenantEntityBase ensures automatic data isolation by filtering queries based on the TenantId. This means that each tenant can only access their own data without needing to manually add WHERE clauses, simplifying the development of multi-tenant SaaS applications.

Can I combine features like soft delete and multi-tenancy in Hazina?

Yes, you can create a custom base class, such as TenantSoftDeleteEntity, that incorporates both soft delete and multi-tenancy features. This allows you to manage entities that need to be soft deleted while also maintaining data isolation across tenants.

Terug naar overzicht

Frequently Asked Questions

What are the four base classes provided by Hazina for entity types? +

Hazina provides four base classes: EntityBase, SoftDeleteEntityBase, TenantEntityBase, and OwnedEntityBase. Each class is tailored for specific use cases, such as simple lookup tables, multi-tenant applications, and user-owned content.

How does SoftDeleteEntityBase handle deleted records? +

SoftDeleteEntityBase introduces IsDeleted and DeletedAt properties, allowing entities to be marked as deleted without being permanently removed. For instance, when an invoice is deleted, IsDeleted is set to true and DeletedAt records the timestamp.

What functionality does TenantEntityBase offer for multi-tenant applications? +

TenantEntityBase includes a TenantId property that automatically filters data based on the tenant, ensuring that one tenant cannot access another tenant's data. This eliminates the need for manual WHERE clauses in queries.

How does OwnedEntityBase enforce user-level access control? +

OwnedEntityBase incorporates an OwnerId property that is automatically set to the authenticated user's ID, ensuring that only the document's owner can view or edit it. This helps maintain privacy and access control for user-owned content.

Can Hazina handle a scenario requiring both soft delete and multi-tenancy features? +

Yes, Hazina allows you to create a custom base class, such as TenantSoftDeleteEntity, which combines soft delete and multi-tenancy features. This class can include both IsDeleted and DeletedAt properties alongside TenantId for automatic data isolation.

ENNL