4

I use inversify in a typescript project. I created a inversify.config.ts file and made some inversify factories. Here is an exemple of one of my factories :

InversifyConfig.bind<MyClass>(MyClass).toSelf();
InversifyConfig.bind<interfaces.Factory<MyClass>>("Factory<MyClass>")
               .toFactory<MyClass>((context: interfaces.Context) => {
     return (obj: object) => {
            let myClass = context.container.get<MyClass>(MyClass); // services are injected here
            myClass.initObject(obj);
            return myClass;
     }
});

This is working well so I can inject my factory easily. But I have around 10 classes like MyClass and they all extend MySuperClass (which has the "obj" property). So far I just copy/paste this code 10 times for all classes. I wonder if I can make a generic function to create all these factories. All I tried so far did not work because I don't know how to pass a Class parameter to my function. Can anyone have an idea on how to achieve what I want ? Thank you ;)

TCH
  • 421
  • 1
  • 6
  • 25

0 Answers0