5.7 KiB
Code Organization Improvements
This document describes the recent reorganization of the codebase to improve maintainability, extensibility, and clarity.
Overview
The codebase has been restructured to follow better software engineering practices, making it more organized, maintainable, and easier to extend.
Key Improvements
1. Service Locator Pattern (ServiceLocator.kt)
Location: server/src/main/kotlin/at/mocode/services/ServiceLocator.kt
Purpose: Centralized dependency management for repository instances.
Benefits:
- Single point of access for all repositories
- Easy to switch implementations (e.g., for testing or different databases)
- Lazy initialization for better performance
- Simplified dependency injection
Usage:
val artikelRepository = ServiceLocator.artikelRepository
val vereinRepository = ServiceLocator.vereinRepository
2. Standardized API Responses (ApiResponse.kt)
Location: server/src/main/kotlin/at/mocode/utils/ApiResponse.kt
Purpose: Consistent response format across all API endpoints.
Benefits:
- Uniform error handling
- Standardized success/error response structure
- Reduced code duplication
- Better client-side error handling
Usage:
call.respondSuccess(data)
call.respondError("Error message")
call.respondNotFound("Resource")
3. Route Utilities (RouteUtils.kt)
Location: server/src/main/kotlin/at/mocode/utils/RouteUtils.kt
Purpose: Common route operations and parameter validation.
Benefits:
- Consistent parameter validation
- Reduced boilerplate code
- Standardized error responses
- Type-safe parameter extraction
Usage:
val uuid = call.getUuidParameter("id", "artikel") ?: return
val query = call.getQueryParameter("q") ?: return
val data = call.safeReceive<Artikel>() ?: return
4. Centralized Route Configuration (RouteConfiguration.kt)
Location: server/src/main/kotlin/at/mocode/routes/RouteConfiguration.kt
Purpose: Organized route registration by domain and functionality.
Benefits:
- Clear API structure
- Logical grouping of related endpoints
- Easy to understand and maintain
- Scalable for future additions
Structure:
/api
├── /artikel (core routes)
├── /personen
├── /vereine
├── /domain
│ ├── /lizenzen
│ ├── /pferde
│ └── /qualifikationen
└── /events
├── /veranstaltungen
├── /turniere
├── /bewerbe
└── /abteilungen
5. Configuration Management (AppConfig.kt)
Location: server/src/main/kotlin/at/mocode/config/AppConfig.kt
Purpose: Centralized application configuration management.
Benefits:
- Environment-specific settings
- Type-safe configuration
- Default values for development
- Easy to extend for new settings
Features:
- Application info (name, version, environment)
- Database configuration
- API settings
- Security configuration
Migration Guide
For Existing Routes
-
Update Repository Access:
// Before val repository = PostgresArtikelRepository() // After val repository = ServiceLocator.artikelRepository -
Update Route Paths:
// Before route("/api/artikel") { ... } // After route("/artikel") { ... } // /api prefix handled by RouteConfiguration -
Use Response Utilities:
// Before call.respond(HttpStatusCode.OK, data) // After call.respondSuccess(data) -
Use Route Utilities:
// Before val id = call.parameters["id"] ?: return@get call.respond(...) val uuid = uuidFrom(id) // After val uuid = call.getUuidParameter("id") ?: return
For New Routes
- Add repository interface to
ServiceLocator - Create route function using utilities
- Register in appropriate section of
RouteConfiguration - Update route paths to exclude
/apiprefix
Best Practices
Repository Pattern
- Always use interfaces for repositories
- Implement PostgreSQL versions for production
- Use ServiceLocator for dependency injection
Error Handling
- Use ResponseUtils for consistent error responses
- Handle common exceptions with
handleException() - Provide meaningful error messages
Route Organization
- Group related routes logically
- Use descriptive route names
- Follow RESTful conventions
- Document complex endpoints
Configuration
- Use AppConfig for all settings
- Provide sensible defaults
- Support environment-specific overrides
- Keep sensitive data in environment variables
Future Enhancements
Planned Improvements
-
Authentication & Authorization
- JWT token support
- Role-based access control
- Session management
-
API Documentation
- OpenAPI/Swagger integration
- Automatic documentation generation
- Interactive API explorer
-
Monitoring & Logging
- Structured logging
- Performance metrics
- Health checks
-
Testing Framework
- Unit test utilities
- Integration test helpers
- Mock repository implementations
Extension Points
- Add new repositories to ServiceLocator
- Extend RouteConfiguration for new domains
- Add configuration sections to AppConfig
- Create new utility functions as needed
Benefits Summary
- Maintainability: Clear separation of concerns and consistent patterns
- Extensibility: Easy to add new features and endpoints
- Testability: Dependency injection and clear interfaces
- Consistency: Standardized responses and error handling
- Documentation: Self-documenting code structure
- Performance: Lazy loading and efficient resource management
This reorganization provides a solid foundation for future development while maintaining backward compatibility and improving code quality.