Skip to content
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

Closed
kibertoad opened this issue Dec 27, 2022 · 4 comments
Closed

Support for eager instantiation #310

kibertoad opened this issue Dec 27, 2022 · 4 comments

Comments

@kibertoad
Copy link
Contributor

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.

@jeffijoe
Copy link
Owner

jeffijoe commented Jan 7, 2023

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.

@kibertoad
Copy link
Contributor Author

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,
    }),

const eagerLoad = (container, modules) => approach requires having second DI configuration, which would define what modules need to be instantiated eagerly, which is not clean at all: you would have to go to two different places to change different aspects of DI configuration.

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.

@jeffijoe
Copy link
Owner

jeffijoe commented Jan 9, 2023

You would still need to call something on container in order to signal that you are finished registering dependencies, and whether or not that becomes required depends on whether you are using this feature, which makes the API more complicated to reason about.

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.

@kibertoad
Copy link
Contributor Author

I understand your position, so I'm closing this ticket, as it's unlikely to move forward. Thank you for sharing your perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants