-
Notifications
You must be signed in to change notification settings - Fork 12
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
Constructor injection for factories with arguments #24
Comments
Hi! Actually I don't like this design myself, but here's the problem I can't solve: class MyChild {
constructor(someService: SomeService: otherThing: OtherThing) {
/* ... */
}
}
const TOKENS = {
someService: token<SomeService>('SomeService'),
myChildFactory: token<Factory<MyChild, [dep: OtherThing]>>('Factory<MyChild>')
};
injected(MyChild, TOKENS.someService, /* ??? */);
const container = new Container();
container.bind(TOKENS.myChildFactory)
.toFactory(MyChild, (dep: OtherThing): MyChild => new MyChild(/* ??? */, dep)); I mean, the problems start when you need to combine injected arguments and external ones. It might look like: container.bind(TOKENS.myChildFactory)
.toFactory(
MyChild,
(dep: OtherThing): MyChild => new MyChild(container.get(TOKENS.someService), dep),
); but I don't think it's the best design. What do you think? |
Sorry for the delayed response, I keep meaning to sit down and give this the attention it deserves. The bottom example is something that I'm actually using in some of my code currently, and while it sorta works there are several issues with it.
I think there are a few potential things that could help in all of this:
So numbers 2 and 3 might look something like this // [...]
injected(MyChild, TOKENS.someService, RuntimeToken('otherThing'), RunTimeToken('anotherOtherThing'));
const MyChildBindingResolver: BindingResolver<OtherThing, OtherThing> = (thing1: OtherThing, thing2: OtherThing) => { 'otherThing' : thing1, 'anotherOtherThing': thing2 };
// Potential binding syntax 1
container.bind(TOKENS.myChildFactory)
.withBindingSource(MyChildBindingResolver)
.toFactory(MyChild, Factory<MyChild>) ; // unsure if this signature would need to change
// Potential binding syntax 2
container.bind(TOKENS.myChildFactory)
.toFactory(MyChild, Factory<MyChild, [thing1: OtherThing, thing2: OtherThing]>, MyChildBindingResolver); The idea being that you'd call the factory with arguments, but it'd pass those arguments to the binding resolver... though looking at it, that resolver might not be necessary if we are using the factory tuple with a Runtime token? I'm reaching the edges of my TypeScript knowledge with this one, sorry... it's just a bit of functionality that is common in the DI systems I've used in other languages. Thanks regardless! |
just bumping, any movement on this? |
Current example of factories with arguments (and the related tests) seems to only allow for intializer injection into an already instantiated object, instead of allowing for a factory method that returns a new instance. This means it is impossible to inject the factory arguments as constructor arguments on the returned instance. I'm attempting to inject into a class who's parent I do not control, and need to inject dynamically created arguments into the constructor.
If there's a way to do this, can we update the documentation to reflect the method?
example:
The text was updated successfully, but these errors were encountered: