1---2name: backend-code-organisation3description: Kotlin backend layering and packaging guidelines4license: MIT5---6
7## Mission
8- Keep backend services modular: resources handle transport, managers own business logic, and data access stays isolated.
9- Maintain clean dependency flow across modules (`service` → `core` → `models`) to encourage reuse and testability.
10
11## Layering Principles
12- **Resources**: Only translate HTTP/storage inputs to domain calls. No database or cross-service logic. Convert `SecurityContext` early.
13- **Managers**: Encapsulate business rules; coordinate helpers, other managers, workflows, and DAL components. Break cycles by separating read/write responsibilities or introducing controllers.
14- **Helpers/Services**: Reusable integrations (feature flags, external clients). Centralize external service calls to simplify retries and upgrades.
15- **Repos/DAL/DAO**: Keep database interactions single-purpose. Compose multi-step transactions in repos/DAL; keep DAO calls single query.
16- **Utils/Validators/Transformers**: Pure functions and extensions only; avoid hidden state.
17- **Cache Managers**: Inject Redis/Lettuce clients, expose typed `get`/`invalidate` helpers.
18
19## Package & Module Structure
20- Enforce lowercase package names without underscores; avoid versioned package hierarchies (`v2` folders). Prefer `managers.slots` over `managers.slots.v2`.
21- Organize by responsibility: `managers`, `helpers`, `utils`, `dao`, `workflows`, etc. Mirror structure in tests.
22- Module boundaries:
23 - `core`: managers, helpers, data access, transient models. May depend on `models`.
24 - `service`: Dropwizard resources, configuration, application wiring; no direct DB access.
25 - `console-service`: Console-specific resources/auth; depends on `core`.
26 - `client`: Outbound clients; depend only on `models`.
27 - `models`: Shared API/data contracts; no dependencies.
28
29## Dependency Injection & Config
30- Add new Redis/Cosmos/DB configs across all environments (`db-test`, `db-dev`, `db-warehouse-prod`) and run the service locally to validate.
31- Annotate injectable classes with `@Singleton` when appropriate.
32- Only use `@Named` when multiple bindings of the same type exist; otherwise default bindings suffice.
33- Centralize client construction in DI modules to enforce consistent timeouts and hosts.
34
35## Review Checklist
36- Check that new features land in the correct layer and module (e.g., resources calling managers, not DAOs).
37- Ensure helpers/managers do not introduce cyclic dependencies; suggest splitting read/write flows or using controller orchestrators.
38- Verify repos/DAL enforce validation and error translation before returning data.
39- Confirm new package or module names remain lowercase and idiomatic; flag attempts to create `v2` package forks.
40- Audit DI modules for duplicate provider methods and proper scoping.
41
42## Tooling Tips
43- `Glob` for `*.kt` within `service/` or `core/` to inspect layer usage.
44- `Read` DI modules when new bindings appear to ensure wiring matches guidelines.
45- `Grep` for `@Named` or direct DAO usage inside resources to catch misplaced logic.