ardalis / ardalis/Result

Allowing Result to be partial

Open
#205 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1k
Forks
127
PR merge metrics
No merged PRs in 30d

Description

Hey,

Would you consider modifying the `Result` class to be marked with `partial`?

I've built up a small little arsenal of extension methods for `Result` in my codebase (that are probably too specific to hold value in your repo) however sometimes these extension methods would fit better as traditional static methods.

My current example is an attempt to get around a common pattern I find myself using where I need to use an `if` to return either a `Success` or a `NotFound` based on the nullability of a variable:
```cs
if (returnValue is null)
return Result.NotFound();

return Result.Success(returnValue);
```

I'm currently getting around this by using a `OrNotFound` extension method:
```cs
// Implementation
public static class ResultExtensions
{
public static Result OrNotFound(this Result value) =>
value.Value is null ? Result.NotFound() : Result.Success(value.Value);
}

// Usage
return Result.Success(returnValue).OrNotFound();
```
This works because it turns the `Result` (nullable) into a `Result` (non-nullable), however I'd probably prefer something like this:
```cs
namespace Ardalis.Result
{
public partial class Result
{
public static Result SuccessOrNotFound(T? value) =>
value is not null ? Result.Success(value) : Result.NotFound();
}
}
```
By marking `Result` as `partial` I'd able to create my own static method without the need to inspect the `.Value` of a `Result` in order to return either a `Success` or `NotFound`.

Thanks, let me know what you think :)

Contributor guide

Open the contributing guide

Research direction

Locate the Result class declaration and inspect the surrounding project tests and usage sites. Confirm that the requested extensibility change is compatible with the existing Result API, and ensure the project still builds and its tests pass when the change is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.