-
Notifications
You must be signed in to change notification settings - Fork 726
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
How to unbind & rebind named/tagged? #1359
Comments
Same! Is that thing? |
Any progress? |
it was 2023... no reaction |
This might not solve your problem 100%, but here is a method I have added to my container.ts import { Container } from "inversify"
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>);
declare module 'inversify' {
interface Container {
unbindTagged: <T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: string) => void
}
}
const container = new Container()
container.unbindTagged = <T>(serviceIdentifier: ServiceIdentifier<T>, key: string, value: string) => {
let bindings = container['_bindingDictionary'].get(serviceIdentifier)
for (let binding of bindings) {
const metadata = binding.constraint.metaData
if (metadata && metadata.key === key && metadata.value === value) {
container['_deactivateSingletons'](binding)
let newBindings = bindings.filter((binding: any) => {
return !(binding.constraint.metaData.key === key && binding.constraint.metaData.value === value)
})
container['_bindingDictionary']['_map'].set(serviceIdentifier, newBindings)
}
}
} index.ts import { container } from "./container"
class Car {}
class BMW extends Car {}
class Honda extends Car {}
async function main() {
container.bind(Car).to(BMW).whenTargetTagged('make', 'bmw')
container.bind(Car).to(Honda).whenTargetTagged('make', 'honda')
container.unbindTagged(Car, 'make', 'honda')
let cars = container.getAll(Car)
console.log(cars)
// [[class BMW extends Car]]
}
main() You can then Calling private methods on the container is never recommended, but this library hasn't been published since 2021, and you can lock npm to a specific version. |
Sorry for the lack of response here, the project went through an unmaintained period but we have been publishing releases again recently ( -- I think this ability would be a good addition to the library. IMO we should aim to introduce this into an official release, @logikaljay has provided a nice API for it which will suit the main Inversify API well. @PodaruDragos what are your thoughts? |
Added to milestone |
Greetings and thank you for the wonderful library. Great job!
Is there a way to unbind and rebind specific named/tagged bindings?
The text was updated successfully, but these errors were encountered: