Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getAll - Error: No matching bindings found for serviceIdentifier #1469

Closed
cts-tradeit opened this issue Jun 27, 2022 · 8 comments
Closed

getAll - Error: No matching bindings found for serviceIdentifier #1469

cts-tradeit opened this issue Jun 27, 2022 · 8 comments
Assignees
Milestone

Comments

@cts-tradeit
Copy link

cts-tradeit commented Jun 27, 2022

Expected Behavior

class StartupCallback {}

If I have container that contains 0 bindings of class StartupCallback and I do getAll I get empty array.

Current Behavior

class StartupCallback {}

If I have container that contains 0 bindings of class StartupCallback and I do getAll I get:

Error: No matching bindings found for serviceIdentifier

Possible Solution

Get all should return empty array if 0 items are bound. Usually when resolving by all (array) it is for components like lifecycle callbacks and initializes which count may vary across environments. It should be possible to have between 0 and n of these instead of between 1 and n, as in environment without single callback we have to provide noop binding.

Your Environment

version: 6.0.1

@Deathrage
Copy link

Are there aany news on thi sissue?

@logikaljay
Copy link

Old, but maybe this still helps you.

A nasty fix is to monkey patch container.getAll:

container.ts

import { Container } from "inversify"

export const container = new Container()

// types taken from `inversify/lib/interfaces.d.ts`
export type Newable<T> = new (...args: any[]) => T;
export interface Abstract<T> {
    prototype: T;
}
export type ServiceIdentifier<T = unknown> = (string | symbol | Newable<T> | Abstract<T>);

// monkey patching `container.getAll`
const getAll = container.getAll.bind(container)
container.getAll = <T>(serviceIdentifier: ServiceIdentifier<T>): T[] => {
  try {
    return getAll(serviceIdentifier)
  }
  catch (err) {
    return []
  }
}

Now, this will no longer throw an error, but instead return an empty array:

index.ts

import { container } from "./container"

// class that is not bound.
class MyService {
}


async function main() {
  let services = container.getAll(MyService)
  console.log('🎉');
}

main()

@Jameskmonger
Copy link
Contributor

Sorry for the lack of response here, the project went through an unmaintained period but we have been publishing releases again recently (6.0.2 at the end of October 2023)

--

Similar to #1359 I think this would be a great feature for the official library, imo we should aim to get this into an official release. cc @PodaruDragos

@Jameskmonger Jameskmonger added this to the 6.1.0 milestone Dec 17, 2023
@Jameskmonger
Copy link
Contributor

Added to milestone 6.1.0 pending agreement from other contributors.

@notaphplover
Copy link
Member

This is not an issue. The current behavior is correct. Having 0 injected services in a multi injection is perfectly fine and might be even desired in some cases.

@logikaljay
Copy link

@notaphplover OG post said that the current behaviour throws an error.

@notaphplover
Copy link
Member

@notaphplover OG post said that the current behaviour throws an error.

Oh, sry, my bad then. Reopening the issue

@notaphplover notaphplover reopened this Nov 28, 2024
@notaphplover
Copy link
Member

After having a look at the code, this is the expected behavior. inversify@6.2.0 will offer a container.tryGetAll method to provide the desired behavior. Meanwhile, inversify@6.2.0-beta.1 is released with this feature 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

5 participants