#What importing whole module does?

4 messages · Page 1 of 1 (latest)

teal warren
#

in the documentation about Feature Modules

there is CatsModule that imported to the root AppModule, what does it mean? Will AppModule have access to all providers,controllers and imports defined in the imported module? (and all these controllers, providers and imports of the imported module will be provided to AppModule as new instance of all them, because all module is Shared modules by default)

so in order to share a instance of controller/service between another module you need to include it into exports array, right? but importing module will still have all providers/controllers/servicies as new instances, except ones that included into exports array, them importing module will have like the same instance

jagged falcon
#

but importing module will still have all providers/controllers/servicies as new instances,

#

If it is the same module, Nest will know not to create new instances. Providers (as the default) are singletons. The only time other instances are created are in request or transient scoped providers.

grim turtle
#

In my experience, but please correct me if I'm wrong:

You can think of each module as a DI container that may have global or dynamic registrations. Exports control what you want to share to your importer, like exposing APIs but not your dependencies directly.

Now, as Scott mentioned, the dependencies usually singletons, but that one you can control in a few ways. For example, re-usable modules usually have .forRoot and .forFeature connector methods where they represent static vs dynamic configurations. The latter will have multiple instances per configurations, including the module classes, but each of those will be handled as singleton of that particular container-branch.

When importing a module, you adding to the container-tree, which in turn will resolve the dependencies, and instantiate all on boot. When importing the same module multiple times, its dependencies will be instantiated only once per configuration.