Binder
Binder
Binder<TValue> is obtained from di.bind(key) or di.rebind(key). Its
methods select how the key is resolved. Each method returns a
BinderOptions<TValue> for further configuration.
toClass
toClass(constructor, injections?)
Resolves the key by instantiating constructor. Dependencies in injections
are resolved from the container and passed to the constructor in order.
di.bind(UserService).toClass(UserService, [Logger, Database])
When injections is omitted the container injects nothing. Use decorator
metadata or an explicit injection array.
toSelf
toSelf(injections?)
Shorthand for toClass(key, injections) when the key is a class reference.
di.bind(UserService).toSelf([Logger])
toValue
toValue(value)
Binds the key to a constant value. The value is returned as-is on every resolution.
di.bind('app.version').toValue('1.0.0')
di.bind(AppConfig).toValue(config)
toFactory
toFactory(factory)
Binds the key to a synchronous factory function. The factory receives a
ResolutionContext and must return the instance synchronously.
di.bind(Logger).toFactory(ctx => new ConsoleLogger(ctx.container.get(AppConfig)))
toAsyncFactory
toAsyncFactory(factory)
Binds the key to an async factory function that returns a Promise. The
container awaits the promise during init().
di.bind(DatabasePool).toAsyncFactory(async ctx => {
const cfg = ctx.container.get(AppConfig)
return connectToDatabase(cfg.databaseUrl)
})
toFunction
toFunction(fn, injections?)
Binds the key to the result of calling fn with resolved dependencies.
Unlike toFactory, the function does not receive a ResolutionContext — it
receives the resolved dependency values directly.
di.bind(Greeter).toFunction(
(name: string) => `Hello, ${name}!`,
['app.name'],
)
aliasOf
aliasOf(key)
Registers the key as an alias for another key. Resolving the key is identical to resolving the target.
di.bind(IUserService).aliasOf(UserServiceImpl)
BinderOptions
BinderOptions<TValue> is returned by every Binder terminal method. Its
methods are chainable and configure the binding's metadata. Calling any method
returns the same BinderOptions instance.
lifetime
lifetime(scopeId)
Sets the lifecycle scope for the binding. See Scopes for available scope identifiers.
di.bind(CacheService).toSelf().lifetime(Scopes.SINGLETON)
di.bind(RequestLogger).toSelf().lifetime(Scopes.REQUEST)
names
names(name, ...names)
Registers additional string or symbol keys for the binding. The binding is accessible under the primary key and all named keys.
di.bind(Logger)
.toClass(ConsoleLogger)
.names('default-logger', Symbol.for('logger'))
lazy
lazy(boolean?)
When true, defers instantiation until first resolution instead of during
init(). Default: false.
di.bind(HeavyService).toSelf().lazy()
primary
primary(boolean?)
Marks this binding as the preferred one when multiple bindings exist for the
same key. di.get() returns this binding instead of throwing
ErrNoUniqueInjectionForKey.
di.bind(Logger).toClass(FileLogger).primary()
fallback
fallback(boolean?)
Marks this binding as a fallback. It is only used when no non-fallback binding exists for the same key.
di.bind(Metrics).toClass(NoOpMetrics).fallback()
byPassPostProcessors
byPassPostProcessors()
Excludes this binding from all PostProcessor hooks.
injectProperty
injectProperty(property, injection)
Injects a dependency into a property of the resolved instance after construction.
di.bind(Service).toSelf().injectProperty('logger', Logger)
injectMethod
injectMethod(method, ...deps)
Calls method on the resolved instance after construction, passing resolved
dependencies as arguments.
di.bind(Service).toSelf().injectMethod('setLogger', Logger)
labels
labels(symbol, ...symbols)
Attaches symbol labels to the binding. Labels enable grouped retrieval via
di.getBindingsByLabel(label).
const kPlugin = Symbol.for('plugin')
di.bind(AuthPlugin).toSelf().labels(kPlugin)
tags
tags(symbol, value)
tags(map)
Attaches symbol-keyed metadata to the binding. Useful for runtime introspection.
di.bind(UserController).toSelf().tags(Symbol.for('route'), '/api/users')
postConstruct
postConstruct(fn)
Runs fn immediately after the instance is created. Must be synchronous; the container does not await a returned promise.
di.bind(DatabasePool)
.toSelf()
.postConstruct(pool => pool.connect())
preDestroy
preDestroy(fn)
Runs fn before the instance is destroyed during dispose().
di.bind(DatabasePool)
.toSelf()
.preDestroy(pool => pool.end())
intercept
intercept(interceptor)
Wraps every resolved instance with interceptor. The interceptor receives the
ResolutionContext and the instance and must return the (possibly wrapped)
instance.
di.bind(PaymentService)
.toSelf()
.intercept((ctx, instance) => withMetrics(instance))
conditional
conditional(fn)
Activates the binding only when all predicates in fn return true.
Predicates receive a ConditionContext with container.has().
di.bind(RedisCacheService)
.toSelf()
.conditional(ctx => ctx.container.has(RedisClient))
extends
extends(constructor?)
Registers the binding under an abstract base class in addition to its primary
key. When no constructor is given, the base is inferred from the class's
prototype chain.
di.bind(ConsoleLogger).toSelf().extends(Logger)
di.get(Logger) // ConsoleLogger
internal
internal()
Marks the binding as internal. Internal bindings are excluded from
getBindings(), getBindingsBy(), and getBindingsByLabel() results when
called from outside the container's own resolution logic.