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
Support for eager instantiation #310
Comments
I don't think adding an API for this to Awilix is necessary since it's as simple as: const eagerLoad = (container, modules) =>
modules.forEach(m => container.resolve(m))
// ...
eagerLoad(container, ['serviceA', 'serviceB']) What API did you have in mind? I am trying to keep the scope of Awilix to be just dependency injection with whatever else is necessary to make that part easier. For example, disposers were necessary since Awilix may be responsible for constructing disposable modules, but this sounds like something that can be done outside Awilix. |
Implementing this outside of awilix as an additional library was my first thought, but it provides not the best developer experience, as it means the DI configuration is spread across multiple places. This is very concise and allows to store all the DI-related configuration in one place: rabbitMqDisposer: asClass(RabbitMqDisposer, {
dispose: (rabbitMqDisposer) => {
return rabbitMqDisposer.close()
},
eager: true,
}),
eager/lazy instantiation is very much a part of DI logic, and is traditionally handled by DI mechanism. See https://www.baeldung.com/spring-boot-lazy-initialization for an example. |
You would still need to call something on Re: the example you linked, lazy instantiation is more or less the standard for DI, and Spring is an application framework, not just a DI container so it makes sense for them to support these things. Awilix is just a container, it's not a design goal for Awilix to be an application framework, the idea is that people can build their application framework on top of Awilix if they wish. I should have stated the non-goals in the readme, that's my bad. |
I understand your position, so I'm closing this ticket, as it's unlikely to move forward. Thank you for sharing your perspective. |
It would be convenient to create some of the modules eagerly, in case no one really depends on them. One use-case example - smart disposers, which ensure a set of modules to be disposed in particular sequence, or background jobs, which can start themselves.
If this is approved, I can create a PR.
The text was updated successfully, but these errors were encountered: