Dependency Injection enhancement
See original GitHub issueIssue
Currently Foal resolves dependencies with property injection. Instance are injected with the dependency annotation. This scenario have some issues:
- Compiler is configured with “strictPropertyInitialization” to false. That’s a huge problem because this configuration defined properties to be optionally initialised by default. It’s better to allow developers to choose that (I personally always initialised my properties in constructor and mark them as readonly for immutability, so I like to have this option activated in order to give me errors if I forget something)
- Documentation give an example where the property is public. DI based on property injection is ok because everything is public in JavaScript and annotation don’t care about that, but to ensure that the dependency decorator will be the only one to manipulate the property in TypeScript, it will be better to show an example with private & readonly:
@dependency
private readonly logger: Logger
- Injection in constructor is far way better than property injection because it helps to implement immutability (that’s why developer must have the choice to use “strictPropertyInitialization”). Foal can do both. A quick example on ctor injection:
export class ApiController {
private readonly service: Service
constructor(@inject s: Service) {
this.service = s;
}
@Get('/')
index(ctx) {
return new HttpResponseOK(this.service.hello());
}
}
- Foal can’t integrate third part IoC container. It means that developers can’t use Inversify or tsyringe to manage their dependencies.
Possible solution
This is definitely not easy to implement. Foal can handle both scenarios:
- Property injection: Keep the current implementation
- Constructor injection: Need to be done with a Dependency Resolver. This one can be abstract with an interface, that’s allowed third part IoC container to be integrated by implementing a custom resolver. Foal have to provide a default one for this scenario.
I can probably help you on this enhancement because i’m working on a similar feature in my Mediator library.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Dependency injection in Android | Android Developers
What is dependency injection? · Constructor Injection. This is the way described above. You pass the dependencies of a class to its constructor....
Read more >Enhancement (Jakarta Context Dependency Injection API)
2nd phase of build compatible extension execution. Allows transforming annotations. In the following text, the term expected types denotes the set of types ......
Read more >Dependency injection and programmatic lookup
Dependency injection always occurs when the bean instance is first ... WeldInstance - an enhanced version of jakarta.enterprise.inject.
Read more >Cataloging dependency injection anti-patterns in software ...
Dependency injection improves software modularity by enabling less coupling among modules by refraining them from being aware of implementation details (i.e., ...
Read more >Building a Flexible Dependency Injection Library for Swift
Dependency Injection is a programming concept that improves testability and allows for easy decoupling of classes. It achieves this by requiring the ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah sure, don’t worry about that 😉
Definitely agree with that, Foal can’t remove this feature and some developers prefers property injection!
… I forgot that point for TypeORM 😢. So it seams near impossible to switch this configuration yet!
Yeah sure, I can do that 😉
I have the same feeling because currently private is just a “TypeScript” concept. Also we don’t know the impact of ECMAScript private fields on TypeScript and Decorators. So it’s better to just mark the property as readonly and that’s all!
😃
It helps to migrate to Foal. Application that uses Inversify don’t have to be refactor in order to switch to Foal. But it’s definitely not a huge priority.
I agree!
To be honest, it’s a really a complex topic because of the nature of JavaScript.
This feature is a must have, so I understand that injection support isn’t a priority. It can give me some time to work on my library, and after that I can probably help you to implement injection support in Foal 😉
Ok, I understand!
Also it’s really not an easy feature to add in Foal.