Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Enhance Fluent Syntax for the Process Framework & demo update to Simplify fluent syntax, Transitions and Reduce Cognitive Overload #10002

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

joslat
Copy link
Contributor

@joslat joslat commented Dec 17, 2024

Motivation and Context

  1. Why is this change required?
    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.

  1. 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.

  2. 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.

  1. If it fixes an open issue, please link to the issue here.
    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):

var startStep = processBuilder.AddStepFromType<StartStep>();
var doSomeWorkStep = processBuilder.AddStepFromType<DoSomeWorkStep>();
var doMoreWorkStep = processBuilder.AddStepFromType<DoMoreWorkStep>();
var lastStep = processBuilder.AddStepFromType<LastStep>();

processBuilder
    .OnInputEvent(ProcessEvents.StartProcess)
    .SendEventTo(new ProcessFunctionTargetBuilder(startStep));

startStep
    .OnFunctionResult()
    .SendEventTo(new ProcessFunctionTargetBuilder(doSomeWorkStep));

doSomeWorkStep
    .OnFunctionResult()
    .SendEventTo(new ProcessFunctionTargetBuilder(doMoreWorkStep));

doMoreWorkStep
    .OnFunctionResult()
    .SendEventTo(new ProcessFunctionTargetBuilder(lastStep));

lastStep
    .OnFunctionResult()
    .StopProcess();

New Syntax (Fluent and Clean):

processBuilder
    .StartWith<StartStep>(ProcessEvents.StartProcess)
    .AndThen<DoSomeWorkStep>()                        // Default: OnFunctionResult
    .AndThen<DoMoreWorkStep>(ProcessEvents.CustomEvent) // Explicit event
    .AndFinally<LastStep>();                         // Automatically stops the process

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

@joslat joslat requested a review from a team as a code owner December 17, 2024 14:08
@markwallace-microsoft markwallace-microsoft added the .NET Issue or Pull requests regarding .NET code label Dec 17, 2024
@github-actions github-actions bot changed the title Enhance Fluent Syntax for the Process Framework & demo update to Simplify fluent syntax, Transitions and Reduce Cognitive Overload .Net: Enhance Fluent Syntax for the Process Framework & demo update to Simplify fluent syntax, Transitions and Reduce Cognitive Overload Dec 17, 2024
@crickman crickman requested a review from alliscode December 17, 2024 19:57
@crickman crickman requested a review from esttenorio December 17, 2024 19:58
@crickman crickman added experimental Associated with an experimental feature enhancement processes labels Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement experimental Associated with an experimental feature .NET Issue or Pull requests regarding .NET code processes
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants