Replies: 2 comments 1 reply
-
This question appears to be out of context. The primary role of a DI (Dependency Injection) container is to inject dependencies into a constructor rather than injecting parameters into arbitrary functions. It is highly likely that you're attempting to achieve a goal that this library was not designed for. |
Beta Was this translation helpful? Give feedback.
-
Hi @skalinkin well said, you're correct. I will add to your answer few more things to make sure it covers all the aspect of IoC in general as well as exclusively for Inversify, so the next one to ask about this can refer to this content. Hi @imcm7 In the example provided, you are attempting to use Constructor Injection @injectable()
class A {
constructor(@inject("B") private b: B) {}
}
// Or
class B {
constructor(private c: C) {}
} Property Injection @injectable()
class A {
@inject("B") private b: B;
} Why Method Parameter Injection Is Problematic:
Another point that you have not asked but your code is trying to do is inject primitive types, and let me elaborate this a bit more for you: Inject Primitive types Injecting primitive types such as Inversify is so feature rich that also handles this use case, you can inject primitives in constructors or as properties as well, here is an example: Via Constructor import "reflect-metadata";
import { injectable, inject, Container } from "inversify";
const TYPES = {
apiKey: Symbol.for("apiKey"),
};
@injectable()
class ApiService {
constructor(@inject(TYPES.apiKey) private apiKey: string) {}
fetchData() {
console.log(`Fetching data using API key: ${this.apiKey}`);
}
}
const container = new Container();
container.bind<ApiService>(ApiService).toSelf();
container.bind<string>(TYPES.apiKey).toConstantValue("my-secret-api-key");
const apiService = container.get(ApiService);
apiService.fetchData(); Via property @injectable()
class ApiService {
@inject(TYPES.apiKey) private apiKey!: string;
fetchData() {
console.log(`Fetching data using API key: ${this.apiKey}`);
}
} With every super power comes some points that needs to be taken in consideration when using primitive injection. 1 - The overhead, using the DI system for a simple primitive that don't need management or change dynamically I hope you have a clear understanding about your request now, next time try to explain a bit better your goals, use cases, so we can be more efficient on resolving the matter. Regards |
Beta Was this translation helpful? Give feedback.
-
How to inject method parameter
Beta Was this translation helpful? Give feedback.
All reactions