Open mitchdenny opened 1 month ago
@samsp-msft @mattmccleary @eerhardt @davidfowl @maddymontaquila @DamianEdwards
@rajkumar-rangaraj @TimothyMothra @vishweshbankwar, FYI, feedback welcome.
I’d love to see a more convincing example. Something that goes from ugly ifs to clean When calls.
Background and Motivation
When using the fluent-like API in the .NET Aspire app model developers sometimes find themselves wanting to conditionally apply a method to a resource builder. For example, you might want to only add a reference to a resource in publish mode, or for a particular environment.
Today to do this you need to break out of the fluent pattern to add conditional blocks to do this because C# doesn't have an inline conditional method invocation (as far as I know). Example of current approach:
This makes the code look a lot less concise. Here is an example which inspired this API proposal:
https://github.com/dotnet/eShop/pull/464
Proposed API
Usage Examples
Applying the example above to this new API design:
To help make things even more concise we could add reusable conditional functions:
Alternative Designs
We could add conditional logic to methods like
WithReference
andWithEnvironment
. However this would further complicate the set of overloads on these methods so this general purpose helper method is probably the most flexible building block that doesn't require additional ongoing work to implement.Risks
One day we might get a more elaborate method invocation syntax for C# which makes this redundant.