Service Best Practices
Overview
In the code-arena project, the Service layer is responsible for business logic processing, data transformation, and external API integration. This skill provides specifications and best practice guidelines for writing Services to ensure code maintainability, testability, and consistency.
Project Data Flow
Frontend React components → tRPC Client → tRPC Router → Service → DAO → Drizzle DB → PostgreSQL
Core Specifications
1. File Structure
- Location:
- Naming:
- Export:
export const {Feature}Service = { ... }
2. Dependency Injection (Mandatory)
Services must not directly import
; they must use dependency injection via DAO.
3. Method Signatures
- CamelCase naming
- Return Promise type
- Use Zod for input validation
4. Error Handling
Use custom error classes, log records, and throw appropriate errors.
5. Transaction Management
Services can initiate
, and DAOs receive an optional
parameter.
Usage Guide
When creating or refactoring Service files:
- Follow the dependency injection pattern
- Use Zod for input validation
- Handle database operations via DAO
- Implement proper error handling and logging
- Write test cases
Detailed Reference
For the complete best practice guide, refer to service-best-practice-guide.md, which includes code examples, templates, and advanced usage.
Resources
- service-best-practice-guide.md: Complete Service writing guide