Sobes.tech
Senior

What does the repository layer see?

sobes.tech AI

Answer from AI

The repository layer in the application architecture is responsible for interacting with the data source (e.g., database). It should only see and work with domain entities (models) and provide methods for retrieving, saving, and updating data.

What the repository layer can see:

  • Domain models (Entities): structures reflecting business logic.
  • Data access interfaces: methods for CRUD operations.
  • Database transactions and contexts: if ORM or drivers are used.

What it should not see:

  • Business rules logic (this is the task of the service layer).
  • Implementation details of services.
  • HTTP requests, controllers, and other application layers.

Example of a repository interface in Go:

// User represents a domain model
type User struct {
    ID   int
    Name string
}

// UserRepository describes methods for accessing user data
type UserRepository interface {
    GetByID(id int) (*User, error)
    Save(user *User) error
    Delete(id int) error
}

// Implementation can use SQL, ORM, etc.

Thus, the repository layer encapsulates storage details and provides a clean API for data operations.