You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Additional provider registrations can be performed here
185
-
pass
186
180
```
187
181
188
182
In this example, the `CarModule` class accepts a `Config` object in its constructor.
189
183
This allows us to access configuration parameters within the module.
190
-
Additionally, the `register_providers` method can be utilized for more advanced provider registrations within the container.
191
184
192
185
## **Module Middleware**
193
186
Middleware functions can be defined at the module level using the `@middleware()` function decorator. Let's illustrate this with an example:
@@ -264,9 +257,42 @@ you can refer to the documentation [here](https://injector.readthedocs.io/en/lat
264
257
## **ForwardRefModule**
265
258
`ForwardRefModule` is a powerful feature that allows you to reference a `@Module()` class in your application
266
259
without needing to instantiate or configure it directly at the point of reference.
260
+
267
261
This is particularly useful in scenarios where you have circular dependencies between modules
268
262
or when you want to declare dependencies without tightly coupling your modules.
269
263
264
+
#### What problem does it solve?
265
+
Let's consider an example where we have two modules, `ModuleA` and `ModuleB`. `ModuleB` depends on a service/provider, which is exported, from `ModuleA` and `ModuleB` does not want to instantiate `ModuleA` directly but rather reference its existing instance from the application scope. `ModuleB` can use `ForwardRefModule` to declare the `ModuleA` as a dependency. And `ModuleA` can be setup differently in any random order.
266
+
267
+
A typical example is a `CustomModule` that depends on `JWTModule` to sign the JWT tokens. The `CustomModule` would declare `JWTModule` as a dependency using `ForwardRefModule` and `JWTModule` can be configured separately in `ApplicationModule`.
268
+
269
+
```python
270
+
from ellar.common import Module
271
+
from ellar.core import ModuleBase, ForwardRefModule
The `JWTModule` provides `JWTService` which is injected into `CustomModule`. By declaring `ForwardRefModule(JWTModule)` in `CustomModule`, the `JWTService` will be properly resolved during instantiation of `CustomModule`, regardless of the order in which the modules are configured in the application.
290
+
291
+
This pattern is particularly useful when:
292
+
- You want to avoid direct module instantiation
293
+
- You need to configure a module differently in different parts of your application
294
+
- You want to maintain loose coupling between modules
295
+
270
296
### Forward Reference by Class
271
297
272
298
In the following example, we have two modules, `ModuleA` and `ModuleB`. `ModuleB`
In the illustration above, the `AModule``register_services` method was used to register `IFoo` and `IFooB`
246
-
with their respective concrete implementations.
185
+
In the above example, `ProviderConfig` is used as a value type for `IFooB` and as a concrete type for `IFoo`. Also, the `use_class` argument can be used to specify the path to the class to be used as the provider which is useful when you want to lazy load the provider class.
In the above example, we are tagging `Foo` as `first_foo` and `FooB` as `second_foo`. By doing this, we can resolve both services using their tag names, thus providing the possibility of resolving services by tag name or type.
278
223
279
-
Also, services can be injected as a dependency by using tags. To achieve this, the `InjectByTag` decorator is used.
280
-
281
-
For example:
282
-
283
-
```python
284
-
from ellar.di import EllarInjector, InjectByTag, scopes
0 commit comments