services-and-interfaces

star 217

Define and implement services that encapsulate business logic with proper constructor-based dependency injection.

Authula By Authula schedule Updated 4/25/2026

name: services-and-interfaces description: Define and implement services that encapsulate business logic with proper constructor-based dependency injection.

Services & Interface Design

When to use this skill

  • Create services that encapsulate business logic
  • Implement business operations that delegate to repositories for data access (Database)
  • Add authentication or domain-specific features as services

Key principles

  1. Concrete struct design: Services are defined as interfaces and implemented by concrete structs with the same name lowercase and with a constructor (e.g., TodoService and todoService with NewTodoService constructor)
  2. Constructor-based injection: Dependencies passed at construction
  3. Single responsibility: Each service handles one domain concern
  4. Repository delegation: Services delegate all data access to repositories
  5. Context-aware: All public methods accept context.Context
  6. Interface return types: Constructors return interfaces, not concrete types
  7. Single File Exports: Each service implementation is in its own file. However, all service interfaces are exported from interfaces.go under the services/ folder.

Pattern

Services handle business logic:

  • Define interface for service with methods representing business operations
  • Implement as concrete struct with repository dependencies
  • Constructor function injects all dependencies
  • Methods are thin wrappers that validate, transform, and delegate to repositories
  • Return interfaces from constructors

Example

See examples/todo_service.go for:

  • TodoService interface definition
  • todoService implementation with constructor
  • Validation and repository delegation patterns

Common mistakes

  1. Service-to-service direct calls without interfaces
  2. Putting queries in services (repositories own data access)
  3. Missing context parameter
  4. Exporting implementation struct instead of interface
  5. Not testing with mocks

References

Install via CLI
npx skills add https://github.com/Authula/authula --skill services-and-interfaces
Repository Details
star Stars 217
call_split Forks 22
navigation Branch main
article Path SKILL.md
More from Creator