Is your feature request related to a problem? Please describe.
Wondering if it were possible in this library to do parameter dependency injection into the constructor instead of property dependency injection. Various articles seem to be against the latter though I am learning DI at the moment so I have no idea what the correct answer is.
Describe the solution you'd like
I have not tested this, but I ended up writing a small wrapper to implement this.
// src/lib/runtime/injex.ts
export const injectParam = (shadowedKey: string, dependencyNameOrType?: any) => {
return function (target: any, _key: string | symbol, _index: number) {
// _key is `undefined` in the constructor, I think, and _index doesn't
// seem to be useful in guessing the value of `shadowedKey`
inject(dependencyNameOrType)(target.prototype, shadowedKey);
};
}
// Usage
import { Dependency } from "somewhere/in/my/project"
export class SomeService {
constructor(
// Here `dep` is required as I couldn't find a way to
// get the name of the paramter in the constructor using
// `reflect-metadata`
@injectParam("dep", Dependency) private dep: Dependency) {}
public someMethod() {
const someResult = this.dep.someResource();
// ...
}
}
It's not great as two function parameters in the decorator go unused, but I'm sure someone more qualified can come up with a solution to that - maybe even detect the parameter names in the constructor.
Describe alternatives you've considered
InversifyJS includes the desired functionality with the caveat that it requires explicit registration of dependencies which I am too lazy to consider (I am only building a small app at the moment, no need for overkill, methinks).
Additional context
What prompted this feature request: