.Net: Enhance Fluent Syntax for the Process Framework & demo update to Simplify fluent syntax, Transitions and Reduce Cognitive Overload #10002
+79
−29
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation and Context
The current implementation of ProcessBuilder requires developers to manually define transitions between steps using verbose methods like OnEvent, OnFunctionResult, and SendEventTo. While functional, this approach introduces cognitive overhead and reduces the readability of process definitions.
To make the developer experience cleaner, this change introduces a fluent API that simplifies step transitions, ensuring they are intuitive, easy to follow, and less error-prone.
What problem does it solve?
Reduces Boilerplate: Developers no longer need to manually handle event linking or explicitly call SendEventTo.
Improves Readability: The fluent syntax aligns with natural language patterns, making process flows easier to understand and maintain.
Minimizes Cognitive Overload: By automating step transitions, developers can focus on process logic rather than edge management.
What scenario does it contribute to?
This change contributes to scenarios where developers need to define multi-step processes in a clean, concise, and readable manner.
Simple Processes: Processes with sequential steps can now be defined fluently with minimal boilerplate.
Complex Processes: Transitions can still include explicit input events, ensuring flexibility for more advanced workflows.
Closes #<issue_number>: Enhance Fluent Syntax for ProcessBuilder to Simplify Transitions and Reduce Cognitive Overload
Description
The following new fluent methods have been introduced:
StartWith(string eventName)
Starts the process with a specified step, binding it to an input event.
AndThen(string? eventOrFunctionName = null)
Chains the next step using either:
Default function result (OnFunctionResult) when no event name is provided.
Explicit input event (OnEvent(eventName)).
AndFinally(string? eventOrFunctionName = null)
Marks the final step in the process and stops it automatically.
Transitions:
Transitions (OnFunctionResult or OnEvent) now automatically start from the previous step added to the process, retrieved directly from the _steps list.
Example
Old Syntax (Manual and Verbose):
New Syntax (Fluent and Clean):
Benefits
Improved Usability:
Reduces manual configuration and boilerplate code.
Developers can define processes fluently and intuitively.
Enhanced Readability:
The new syntax reads like a natural flow, improving clarity.
Backward Compatibility:
Existing APIs (OnEvent, SendEventTo) remain available, ensuring no breaking changes.
Simplified Edge Management:
Transitions now start automatically from the last step added to the process, reducing the risk of misconfigurations.
Testing
Added unit tests to verify:
StartWith binds the first step to an input event.
AndThen correctly transitions between steps using both default function results and explicit input events.
AndFinally marks the final step and stops the process.
Contribution Checklist