InversifyJS: Idea: Exception-less failure to resolve from the container

I have some scenarios where I’d like to allow for optional bindings when doing lookups against the container.

Currently, to support this I have to put try { } catch { } blocks around my container.get(...) calls, which works but is obviously a little verbose.

Is there any merit in offering a default value when attempting to resolve from the container and when the resolution doesn’t find anything, the default is returned instead?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 17 (6 by maintainers)

Most upvoted comments

I like A more, there also can be getOptional:

/** 
 * return `undefined` if the binding is missing, 
 * a short cut for `get(serviceIdentifier, undefined)`
 */
getOptional<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T | undefined;
/** 
 * return an empty array if there is no bindings,
 * a short cut for `getAll(serviceIdentifier, [])`
 */
getAllOptional<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T[];