microsoft / tsyringe

Lightweight dependency injection container for JavaScript/TypeScript
MIT License
5.18k stars 172 forks source link

resolve container passing reference parameter #215

Closed andrefelipeschulle closed 1 year ago

andrefelipeschulle commented 1 year ago

Guys, I would like some help from you, what is the best way to solve a container that has several possible providers, would there be a way to pass some parameter to solve an instance?

import {Foo1} from "./Foo1";
import {Foo2} from "./Foo2";

const containers = {
  Foo1: Foo1,
  Foo2: Foo2,
};

container.registerSingleton("FooContainer", (value) => {
  return containers[value];
});

and solve like this?

container.resolve("FooContainer", "foo1");

is there any way to work something like this in tsyringe ?