Proxy vs Generated Dispatcher
September 4, 2023 ยท View on GitHub
In the context of the Pipelines framework, two distinct approaches exist for handling the routing of inputs to their respective handlers: the Dispatcher Proxy and the Pipelines.WrapperDispatcherGenerator generated using source generators.
This section provides an in-depth exploration of both Dispatcher Proxy and WrapperDispatcherGenerator, describing their functionalities, key features, and practical applications.
Table of Contents
1. Dispatcher Proxy
1.1 Description
Dispatcher Proxy is a mechanism that allows for dynamic redirection of method calls to objects that handle specific inputs in the Pipelines architecture. It is used to handle inputs and route them to the appropriate handlers dynamically.
1.2 Key Features
- Dynamic Redirection: Dispatcher Proxy enables dynamic redirection of calls to handlers based on the type of input.
- Flexibility: The Proxy uses reflection and can handle various types of inputs, making it a flexible solution for managing different types of inputs.
2. WrapperDispatcherGenerator
2.1 Description
The WrapperDispatcherGenerator is a crucial component of the Pipelines framework that automates the generation of dispatchers during the build process. This source generator analyzes your Pipelines configuration and creates optimized dispatcher code tailored to your project's needs that handles the routing of inputs to handlers.
2.2 Key Features
-
Static Code Analysis: When you build your project, the WrapperDispatcherGenerator performs static code analysis. It examines your Pipelines configuration, including registered inputs, handlers, and dispatchers.
-
Automatic Code Generation: Based on the analysis, the generator automatically generates dispatcher code. This code is specifically crafted to efficiently route requests to their corresponding handlers, ensuring type safety and performance.
-
Integration with Dependency Injection: The generated dispatchers seamlessly integrate with the Dependency Injection (DI) system. They are registered as services, making them available for use within your application.
-
Compile-Time Benefits: Since code generation occurs during the build process, any issues or errors related to dispatcher configuration are identified at compile time. This helps you catch potential problems early in the development cycle.
-
Enhanced Performance: The generated dispatchers are optimized for performance, as they eliminate the need for reflection-based dispatching, resulting in faster request handling.
3. Comparison
- Flexibility vs. Performance: The Proxy dispatcher is flexible and suitable for scenarios where the configuration is dynamic and unknown during compilation. In contrast, the WrapperDispatcherGenerator offers optimized performance and type safety due to its fixed configuration.
- Dynamic Dependency Resolution vs. Static Configuration: The Proxy dispatcher require dynamic dependency and type resolution, impacting performance, while the WrapperDispatcherGenerator has a static configuration, ensuring efficient operation.
- Planned feature: Compile-Time Errors: Dispatchers generated by the WrapperDispatcherGenerator identify configuration issues at compile time, reducing the likelihood of runtime errors.
Recommendation: We highly recommend using the WrapperDispatcherGenerator for its significant performance advantages and versatility in handling various configuration types. It is designed to efficiently handle a wide range of configurations, providing both type safety and optimized performance, making it the preferred choice for most projects.
Please check Benchmarks section to see performance difference.