Raw
1 export const metadata = {
2 title: `${pageNumber} Module Container`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you'll learn about the module container and how to resolve resources from it.
8
9 Since modules are [isolated](../isolation/page.mdx), each module has a local container used only by the resources of that module.
10
11 So, resources in the module, such as services or loaders, can only resolve other resources registered in the module's container, and some Framework tools that the Medusa application registers in the module's container.
12
13 ### List of Registered Resources
14
15 Find a list of resources or dependencies registered in a module's container in [the Container Resources reference](!resources!/medusa-container-resources).
16
17 ---
18
19 ## Resolve Resources from the Module Container
20
21 ### Resolve in Services
22
23 A service's constructor accepts as a first parameter an object used to resolve resources registered in the module's container. To resolve a resource, add the resource's registration name as a property of the object.
24
25 For example, to resolve the [Logger](../../../debugging-and-testing/logging/page.mdx) from the container:
26
27 ```ts highlights={[["4"], ["10"]]}
28 import { Logger } from "@medusajs/framework/types"
29
30 type InjectedDependencies = {
31 logger: Logger
32 }
33
34 export default class BlogModuleService {
35 protected logger_: Logger
36
37 constructor({ logger }: InjectedDependencies) {
38 this.logger_ = logger
39
40 this.logger_.info("[BlogModuleService]: Hello World!")
41 }
42
43 // ...
44 }
45 ```
46
47 You can then use the logger in the service's methods.
48
49 ### Resolve in Loaders
50
51 [Loaders](../loaders/page.mdx) accept an object parameter with the property `container`. Its value is the module's container that can be used to resolve resources using its `resolve` method.
52
53 For example, to resolve the [Logger](../../../debugging-and-testing/logging/page.mdx) in a loader:
54
55 ```ts highlights={[["9"]]}
56 import {
57 LoaderOptions,
58 } from "@medusajs/framework/types"
59 import {
60 ContainerRegistrationKeys,
61 } from "@medusajs/framework/utils"
62
63 export default async function helloWorldLoader({
64 container,
65 }: LoaderOptions) {
66 const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
67
68 logger.info("[helloWorldLoader]: Hello, World!")
69 }
70 ```
71
72 You can then use the logger in the loader's code.
73
74 ---
75
76 ## Caveat: Resolving Module Services in Loaders
77
78 Consider a module that has a main service `BrandModuleService`, and an internal service `CmsService`. Medusa will register both of these services in the module's container.
79
80 However, loaders are executed before any services are initialized and registered in the module's container. So, you can't resolve the `BrandModuleService` and `CmsService` in a loader.
81
82 Instead, if your main service extends the `MedusaService` [service factory](../service-factory/page.mdx), you can resolve the internal services generated for each data model passed to the `MedusaService` function.
83
84 For example, if the `BrandModuleService` is defined as follows:
85
86 ```ts
87 import { MedusaService } from "@medusajs/framework/utils"
88 import Brand from "./models/brand"
89
90 class BrandModuleService extends MedusaService({
91 Brand,
92 }) {
93 }
94
95 export default BrandModuleService
96 ```
97
98 Then, you can resolve the `brandService` that allows you to manage brands in the module's loader:
99
100 ```ts
101 import {
102 LoaderOptions,
103 } from "@medusajs/framework/types"
104
105 export default async function helloWorldLoader({
106 container,
107 }: LoaderOptions) {
108 const brandService = container.resolve("brandService")
109
110 const brands = await brandService.list()
111
112 console.log("[helloWorldLoader]: Brands:", brands)
113 }
114 ```
115
116 Refer to the [Service Factory reference](!resources!/service-factory-reference) for details on the available methods in the generated services.
117
118 ---
119
120 ## Alternative to Resolving Other Modules' Services
121
122 Since modules are [isolated](../isolation/page.mdx), you can't resolve resources that belong to other modules from the module's container. For example, you can't resolve the Product Module's service in the Blog Module's service.
123
124 Instead, to build commerce features that span multiple modules, you can create [workflows](../../workflows/page.mdx). In those workflows, you can resolve services of all modules registered in the Medusa application, including the services of the Product and Blog modules.
125
126 Then, you can execute the workflows in [API routes](../../api-routes/page.mdx), [subscribers](../../events-and-subscribers/page.mdx), or [scheduled jobs](../../scheduled-jobs/page.mdx).
127
128 Learn more and find examples in the [Module Isolation](../isolation/page.mdx) chapter.
129
130 ---
131
132 ## Avoid Circular Dependencies
133
134 When resolving resources in a module's services, make sure you don't create circular dependencies. For example, if `BlogModuleService` resolves `CmsService`, and `CmsService` resolves `BlogModuleService`, it will cause a circular dependency error.
135
136 Instead, you should generally only resolve services within the main service. For example, `BlogModuleService` can resolve `CmsService`, but `CmsService` should not resolve `BlogModuleService`.