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
The `DiscoveryService`is a utility provided by `@nestjs/core` that allows developers to dynamically discover providers, controllers, and other metadata within a NestJS application. This can be particularly useful for building plugins, decorators, or features that rely on runtime introspection.
3
+
The `DiscoveryService` provided by the `@nestjs/core`package is a powerful utility that allows developers to dynamically inspect and retrieve providers, controllers, and other metadata within a NestJS application. This is particularly useful when building plugins, decorators, or advanced features that rely on runtime introspection. By leveraging `DiscoveryService`, developers can create more flexible and modular architectures, enabling automation and dynamic behavior in their applications.
4
4
5
-
Before using the `DiscoveryService`, you need to import the `DiscoveryModule` in your module:
5
+
#### Getting started
6
+
7
+
Before using `DiscoveryService`, you need to import the `DiscoveryModule` in the module where you intend to use it. This ensures that the service is available for dependency injection. Below is an example of how to configure it within a NestJS module:
@@ -17,7 +18,7 @@ import { ExampleService } from './example.service';
17
18
exportclassExampleModule {}
18
19
```
19
20
20
-
Then, inject `DiscoveryService` into a provider or service:
21
+
Once the module is set up, `DiscoveryService`can be injected into any provider or service where dynamic discovery is required.
21
22
22
23
```typescript
23
24
@@filename(example.service)
@@ -35,61 +36,64 @@ export class ExampleService {
35
36
}
36
37
```
37
38
38
-
> info **Hint** The `DiscoveryService` class is imported from the `@nestjs/core` package.
39
-
40
-
#### Discovering Providers
39
+
#### Discovering providers and controllers
41
40
42
-
You can retrieve all registered providers in the application:
41
+
One of the key capabilities of `DiscoveryService` is retrieving all registered providers in the application. This is useful for dynamically processing providers based on specific conditions. The following snippet demonstrates how to access all providers:
Each provider object contains information about the instance, token, and metadata.
50
-
51
-
#### Discovering Controllers
52
-
53
-
Retrieve all registered controllers:
48
+
Each provider object contains information such as its instance, token, and metadata. Similarly, if you need to retrieve all registered controllers within the application, you can do so with:
This feature is particularly beneficial for scenarios where controllers need to be processed dynamically, such as analytics tracking, or automatic registration mechanisms.
61
56
62
-
`DiscoveryService` can help find metadata attached to providers or controllers. This is useful when working with decorators that add metadata. Let's see an example. Suppose you have a custom decorator that adds metadata to a provider:
57
+
#### Extracting metadata
58
+
59
+
Beyond discovering providers and controllers, `DiscoveryService` also enables retrieval of metadata attached to these components. This is particularly valuable when working with custom decorators that store metadata at runtime.
60
+
61
+
For example, consider a case where a custom decorator is used to tag providers with specific metadata:
Now, you can use `DiscoveryService` to find all providers with this metadata:
80
+
Once metadata is attached to providers in this way, `DiscoveryService`makes it easy to filter providers based on assigned metadata. The following code snippet demonstrates how to retrieve providers that have been tagged with a specific metadata value:
console.log('Providers with cats metadata:', provider);
91
+
console.log(
92
+
'Providers with the "experimental" feature flag metadata:',
93
+
provider,
94
+
);
91
95
```
92
96
93
-
### Conclusion
97
+
####Conclusion
94
98
95
-
`DiscoveryService` is a powerful tool for runtime introspection in NestJS applications. It allows you to discover providers, controllers, and metadata dynamically, making it useful for plugin development, custom decorators, and advanced framework-level features.
99
+
The `DiscoveryService` is a versatile and powerful tool that enables runtime introspection in NestJS applications. By allowing dynamic discovery of providers, controllers, and metadata, it plays a crucial role in building extensible frameworks, plugins, and automation-driven features. Whether you need to scan and process providers, extract metadata for advanced processing, or create modular and scalable architectures, `DiscoveryService` provides an efficient and structured approach to achieving these goals.
0 commit comments