Skip to main content
Domains are the core organizational unit in Platforma. Each domain represents a bounded context - a distinct area of your business logic with its own data models, business rules, and APIs.

Domain Interface

All domains must implement the simple interface from application/domain.go:4:
This allows the application to access the domain’s repository for database migration registration.
The GetRepository() method returns any because different domains have different repository types. The application only needs the repository reference to call its Migrations() method.

Domain Structure

A typical domain aggregates four components:

Component Breakdown

Handles all database operations for the domain:
  • CRUD operations
  • Complex queries
  • Database migrations
  • Transaction management
Dependencies: Only the database connection
Implements domain business rules:
  • Validation logic
  • Orchestrates repository calls
  • Coordinates with other domains
  • Contains no HTTP or database code
Dependencies: Repository and other domain services
Exposes HTTP endpoints for the domain:
  • Route definitions
  • Request/response handling
  • Input validation
  • Error mapping
Dependencies: Service layer
Intercepts HTTP requests:
  • Authentication/authorization
  • Request validation
  • Context enrichment
  • Response modification
Dependencies: Service layer (often)

Real Example: Auth Domain

The auth package provides a complete reference implementation. From auth/domain.go:7:

Auth Domain Constructor

From auth/domain.go:18, showing full domain initialization:
Notice how the constructor uses dependency injection - all external dependencies are passed as parameters, making the domain testable and flexible.

Creating a Domain

Follow these steps to create a new domain:
1

Define your domain struct

Create a domain.go file:
2

Implement the Domain interface

Add the required method:
3

Create a constructor

Initialize all components:
4

Register with application

In your main.go:

Repository Pattern

Repositories handle all database operations:
Repositories should only interact with the database. No business logic, HTTP handling, or external API calls.

Service Pattern

Services implement business logic and orchestrate repository calls:

Mounting Domain APIs

Mount your domain’s HTTP API to a server:
Now all auth routes are available under /auth:
  • POST /auth/register
  • POST /auth/login
  • GET /auth/me
  • etc.
Domains are self-contained. You can mount the same domain to multiple servers or use it in different applications.

Using Domain Middleware

Apply domain middleware to protect routes:
Or wrap individual handlers from auth/domain.go:27:

Domain Dependencies

Domains can depend on other domains via service interfaces:
Depend on interfaces, not concrete types. This keeps domains loosely coupled and makes testing easier.

Testing Domains

Test each layer independently:

Package Organization

Recommended file structure for a domain:

Next Steps

HTTP Routing

Learn how to define domain APIs with HandlerGroup

Middleware

Create middleware for authentication and validation

Database

Set up repositories and migrations

Architecture

Understand the complete framework architecture