di
Wire classes into the Inversify DI container correctly. Use when adding new repositories, controllers, services, or middleware to the server. Use when wiring dependencies, configuring the DI container, or understanding how classes are resolved.
What this skill does
# Inversify Dependency Injection
The monotemplate uses **Inversify v7** with `reflect-metadata` for decorator-based dependency injection. All DI configuration lives in `apps/server/src/di/container.ts`, which exports a singleton `container` instance.
## Making a Class Injectable
Every Repository, Controller, and Service class must be decorated with `@injectable()`. Constructor dependencies use `@inject(ClassName)` parameter decorators. Import the class itself (not `type`-only) so the class token is available at runtime.
```typescript
import { injectable, inject } from "inversify";
import { SomeRepository } from "@server/repositories/SomeRepository";
@injectable()
export class SomeController {
constructor(@inject(SomeRepository) private someRepository: SomeRepository) {}
}
```
## Registering Bindings in `container.ts`
| Pattern | Use case | Example |
|---------|----------|---------|
| `toSelf().inSingletonScope()` | Classes that auto-resolve their own deps | Repositories, Controllers |
| `toConstantValue(value)` | External/constant values | PrismaClient, env-gated services |
| `to(ConcreteClass).inSingletonScope()` | Abstract to concrete | Auth factories |
| `bind(SYMBOL).toConstantValue(value)` (repeated) | Multi-bindings | Express middleware |
### Binding Order
Follow this order in `container.ts`:
```typescript
// Database
container.bind(PrismaClient).toConstantValue(prisma);
// Middleware (multi-binding via Symbol token)
container.bind(EXPRESS_MIDDLEWARE).toConstantValue(helmet());
container.bind(EXPRESS_MIDDLEWARE).toConstantValue(express.json());
// Auth (abstract → concrete, with simulated mode gating)
if (isSimulated()) {
container.bind(AuthContextFactory).to(SimulatedAuthContextFactory).inSingletonScope();
} else {
container.bind(AuthContextFactory).to(ClerkAuthContextFactory).inSingletonScope();
}
// Repositories
container.bind(FooRepository).toSelf().inSingletonScope();
// Controllers
container.bind(FooController).toSelf().inSingletonScope();
```
## Resolving Dependencies
- In routers/handlers: `container.get(ClassName)` — import both `container` and the class
- In `server.ts`: `container.get(AuthContextFactory)`, `container.getAll<T>(EXPRESS_MIDDLEWARE)`
- Never destructure from a container object; always use `container.get()`
```typescript
import { container } from "@server/di/container";
import { UsersController } from "@server/controllers/UsersController";
const result = await container.get(UsersController).getUserById(id);
```
## Symbol Tokens (`di/tokens.ts`)
Used for multi-bindings where multiple values share one key (e.g., `EXPRESS_MIDDLEWARE`).
```typescript
export const TOKEN_NAME = Symbol.for("TokenName");
```
## Simulated / E2E Auth Gating
- `__dev__/` directory code is dynamically imported inside `isSimulated()` checks (Bun macro)
- Never use static imports for `__dev__/` code in production paths
```typescript
if (isSimulated()) {
const { SimulatedAuthContextFactory } = await import("@server/__dev__/SimulatedAuthContextFactory");
container.bind(AuthContextFactory).to(SimulatedAuthContextFactory).inSingletonScope();
}
```
## Nullable / Env-Gated Services
When a service depends on an env variable that may not be set, bind it as `ServiceClass | null`:
```typescript
container.bind<StorageService | null>(StorageService).toConstantValue(
env.SOME_KEY ? new StorageService({ ... }) : null,
);
// Resolve with explicit type annotation:
const svc = container.get<StorageService | null>(StorageService);
```
## tsconfig Requirements
- `experimentalDecorators: true` and `emitDecoratorMetadata: true` must be set
- `import "reflect-metadata"` must be the first import in the entry point (`index.ts`)
## New Entity Checklist
When adding a new Repository, Controller, or Service:
1. Add `@injectable()` to the class
2. Add `@inject(Dep)` to each constructor parameter
3. Import classes (not `type`-only) for injected dependencies
4. Register binding in `container.ts` under the appropriate section
5. In routers, resolve via `container.get(ClassName)`
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.