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

How to unbind & rebind named/tagged? #1359

Open
geekox86 opened this issue Jul 8, 2021 · 6 comments
Open

How to unbind & rebind named/tagged? #1359

geekox86 opened this issue Jul 8, 2021 · 6 comments
Assignees
Milestone

Comments

@geekox86
Copy link

geekox86 commented Jul 8, 2021

Greetings and thank you for the wonderful library. Great job!

Is there a way to unbind and rebind specific named/tagged bindings?

@geekox86 geekox86 changed the title How to unbind named/tagged? How to unbind & rebind named/tagged? Jul 8, 2021
@wi-ski
Copy link

wi-ski commented Jan 15, 2022

Same! Is that thing?

@aztack
Copy link

aztack commented Feb 3, 2023

Any progress?

@safiullin-at
Copy link

it was 2023... no reaction

@logikaljay
Copy link

This might not solve your problem 100%, but here is a method I have added to my container.ts file that lets me unbindTagged:

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 container.bind(Car).to(Honda).whenTargetTagged('make', 'honda') to rebind.

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.

@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)

--

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?

@Jameskmonger
Copy link
Contributor

Added to milestone 6.1.0 pending agreement from other contributors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

6 participants