IoC Container
IoC Container
The Container is LaraNode's dependency injection container. It manages class dependencies and performs automatic resolution.
Basic Usage
import { Container, container } from "@lara-node/core";
// Use the singleton
container.bind("service", () => new MyService());
const service = container.make("service");
// Or create your own
const myContainer = new Container();
Binding Methods
bind()
Register a transient binding (new instance each time):
container.bind(Logger, () => new Logger());
container.bind("mailer", () => new MailManager());
singleton()
Register a singleton (same instance always):
container.singleton(DatabaseService, () => {
return new DatabaseService(config("database"));
});
instance()
Bind an existing instance:
const expressApp = express();
container.instance("express", expressApp);
alias()
Create an alias for an existing binding:
container.bind("cache", () => new CacheManager());
container.alias("cache", CacheManager);
Resolving
make()
Resolve a service from the container:
const service = container.make(UserService);
const mailer = container.make("mailer");
app() Helper
Convenient helper for resolving:
import { app } from "@lara-node/core";
// Resolve a service
const service = app(UserService);
// Get the container
const container = app();
Automatic Resolution
The container can automatically resolve dependencies:
import { Injectable } from "@lara-node/core";
@Injectable()
class UserService {
constructor(
private db: DatabaseService,
private cache: CacheManager,
) {}
}
// Container resolves DatabaseService and CacheManager automatically
const service = container.make(UserService);
Resolving Callbacks
Register callbacks that fire when a type is resolved:
container.resolving(UserService, (service) => {
service.initialize();
});
container.resolving("*", (resolved) => {
console.log("Resolved:", resolved.constructor.name);
});
Checking Bindings
// Check if bound
container.bound("service"); // boolean
// Check if singleton
container.isSingleton(Logger); // boolean
Practical Example
// Register services
container.singleton(DatabaseService, () => {
return new DatabaseService(config("database"));
});
container.singleton(CacheManager, () => {
return new CacheManager(config("cache"));
});
container.bind(UserService, () => {
return new UserService(container.make(DatabaseService), container.make(CacheManager));
});
// Resolve
const users = container.make(UserService);
const allUsers = await users.all();
Next Steps
- Application -- Application bootstrap
- Service Providers -- Creating providers
- Dependency Injection -- DI guide
Configuration (Core)
LaraNode provides a dot-notation configuration system for managing application settings.
Form Request
The FormRequest is a Laravel-inspired request validation class that encapsulates validation logic, authorization, and input handling into a single, type-safe class. When type-hinted in a controller method, the router automatically instantiates it, runs validation, and injects it — returning a 422 response on failure.