API Reference
Complete reference for all APIs provided by the DI framework.
Decorators
@Container(options?)
Marks a class as injectable and automatically registers it with the DI container.
Options:
singleton?: boolean(default:true) - Create a new instance each time or reuse the same instancecontainer?: DIContainer- Specify a custom container (defaults to global container)
Example:
@Component(target)
Marks a constructor parameter or property for dependency injection.
Parameters:
target- The class to inject or a string identifier for factory-registered services
Example - Constructor Parameter:
Example - Property Injection:
Example - Factory Service:
Container API
useContainer()
Returns the global DI container instance.
Returns: Container
Example:
container.register(serviceClass, options?)
Manually register a service class.
Parameters:
serviceClass- The class to registeroptions?- Registration optionssingleton?: boolean(default:true)
Example:
container.registerFactory(name, factory, options?)
Register a service using a factory function.
Parameters:
name: string- Identifier for the servicefactory: () => T- Factory function that creates the service instanceoptions?- Registration optionssingleton?: boolean(default:true)
Example:
container.resolve(serviceClass)
Resolve and get an instance of a service.
Parameters:
serviceClass- The class or string identifier to resolve
Returns: Instance of the service with all dependencies injected
Example:
container.has(serviceClass)
Check if a service is registered in the container.
Parameters:
serviceClass- The class or string identifier to check
Returns: boolean
Example:
container.getServiceNames()
Get all registered service names.
Returns: string[] - Array of all registered service identifiers
Example:
container.on(event, listener) / container.off(event, listener)
Subscribe to container lifecycle events (observer pattern). Returns an unsubscribe function from on.
Events:
registered-{ key, singleton, kind }resolved-{ key, instance, singleton, fromCache }constructed-{ key, instance, overrides }cleared-{ count }
Example:
container.construct(serviceClass, overrides?)
Create a fresh instance without registering it, while still resolving dependencies. Use overrides to supply specific constructor arguments (by index) such as primitives or config values.
Example:
container.fork(options?)
Clone all registrations into a new container (prototype pattern). Pass { carrySingletons: true } to reuse existing singleton instances; default is to start with fresh instances.
Example:
Lifecycle Methods
Services can optionally implement lifecycle methods that are called by the framework or your application code.
setEnv(env: Record<string, any>)
Called to initialize environment-specific configuration.
Example:
setCtx(context: any)
Called to set execution context (e.g., request context).
Example:
Types
Container
The DI container class.
Next Steps
Advanced Usage - Learn advanced patterns and techniques
Error Handling - Understand error scenarios and how to handle them
Testing - Learn how to test services with the DI framework