Uncategorized

Hazina YAML Entities: Let Non-Developers Define Your Data Model

Not everyone who needs to define a data structure is a C# developer. Hazina supports YAML and JSON entity definitions, making it possible for business analysts, product managers, or even AI to define entities that become fully functional APIs.

YAML Entity Definition

# entities/product.yaml
name: Product
baseClass: SoftDeleteEntityBase
properties:
  - name: Name
    type: string
    required: true
    maxLength: 200
  - name: Price
    type: decimal
    required: true
    min: 0
  - name: Description
    type: string
    maxLength: 2000
  - name: Category
    type: string
    required: true
  - name: IsActive
    type: bool
    default: true

What This Generates

From this YAML, Hazina generates:

  • A C# entity class with all properties
  • EF Core configuration (column types, constraints)
  • A full CRUD API endpoint (/api/products)
  • Validation rules based on constraints
  • Database migration

JSON Format

{
  "name": "Customer",
  "baseClass": "TenantEntityBase",
  "properties": [
    { "name": "Name", "type": "string", "required": true },
    { "name": "Email", "type": "string", "required": true, "unique": true },
    { "name": "Phone", "type": "string" },
    { "name": "Tier", "type": "enum", "values": ["Free", "Pro", "Enterprise"] }
  ]
}

Loading Definitions

builder.Services.AddGenericEntityApi<AppDbContext>(options => {
    options.LoadEntitiesFromYaml("./entities");
    // Or: options.LoadEntitiesFromJson("./entities");
});

Relationships

# entities/order.yaml
name: Order
baseClass: TenantEntityBase
properties:
  - name: CustomerId
    type: int
    relation: Customer
  - name: OrderDate
    type: datetime
    default: now
  - name: Total
    type: decimal
    computed: "Items.Sum(i => i.Price * i.Quantity)"
relations:
  - name: Items
    type: hasMany
    target: OrderItem

Validation

Hazina validates YAML definitions at startup and provides clear errors:

// Error: Entity 'Order' references 'Customer' but no entity named 'Customer' was found.
// Did you mean 'Customers'?

In the next post, we’ll explore the Developer CLI tools that make working with Hazina a breeze.

Terug naar overzicht
ENNL