Allowing Result to be partial
- 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
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