Dependency Injection in Jetpath

Dependency Injection (DI) is a design pattern used to manage how components (like services, repositories, controllers) get instances of their dependencies (other components they rely on). While Jetpath does not include a built-in, dedicated Dependency Injection container like frameworks such as NestJS or Angular, it provides effective patterns for managing dependencies, primarily through its Plugin System.


Jetpath’s Approach to Dependencies

Instead of relying on a complex DI container, Jetpath encourages managing dependencies through:

  1. Plugins: Encapsulating related functionality and its dependencies within a plugin, making them accessible via the ctx.plugins object.
  2. Module Scope / Manual Instantiation: Creating and managing instances of services or clients within specific modules and importing them where needed.
  3. (Less Common) Attaching to App/Context: Sharing singleton instances by attaching them to the main app object or request-scoped instances via ctx.app.

1. Using the Plugin System (Recommended)

The Plugin System is the most idiomatic way to manage shared services and dependencies in Jetpath.


2. Module Scope / Manual Instantiation

This is the standard approach in JavaScript/TypeScript without a dedicated DI container.


3. Attaching to app or ctx.app


Conclusion

Jetpath provides flexibility in managing dependencies.