-
Notifications
You must be signed in to change notification settings - Fork 716
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
How to inject method parameter #1540
Comments
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. |
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 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
How to inject method parameter
The text was updated successfully, but these errors were encountered: