ardalis / ardalis/SmartEnum

Use SmartEnumValue.When().Then() with async

Open
#564 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

I couldn't find this in the issues or documentation: Is it allowed to switch on SmartEnum values using the `.When().Then()` pattern, when the `Then()` action delegate is asynchronous?

For example, in a FastEndpoints endpoint I currently have the following:

```csharp
public override Task HandleAsync(
ListAddressRequest request,
CancellationToken cancellationToken)
{
ThrowArgumentNullException.If.Null(request);

var country = Country.FromValue(request.Address.Country);

country
.When(Country.Belgium).Then(async () =>
{
var listResult = await this.ListBelgiumAddressesAsync(request, cancellationToken);
await this.SendResultAsync(listResult);
})
.Default(async () =>
{
var errorResult = Result.Error(new ErrorList
(
ErrorMessages: [$"Country '{country.Value}' is not supported."]
));
await this.SendResultAsync(errorResult);
});

return Task.CompletedTask;
}
```

(Country is `SmartEnum` where each value is the country code.)

Both action delegates passed to `Then()` and `Default()` are async, and the code compiles. But will this execute correctly?

Contributor guide

Open the contributing guide

Research direction

Start by reading the SmartEnum When().Then() and Default() implementation and inspecting existing tests for delegate execution. Determine the expected behavior for asynchronous actions and add coverage or documentation that makes the supported behavior explicit.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.