https://github.com/usercode/smartresults
Lightweight .NET library to use result pattern instead of throwing exceptions.
https://github.com/usercode/smartresults
csharp pattern results
Last synced: 10 months ago
JSON representation
Lightweight .NET library to use result pattern instead of throwing exceptions.
- Host: GitHub
- URL: https://github.com/usercode/smartresults
- Owner: usercode
- License: mit
- Created: 2024-05-01T21:23:25.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-11T21:44:32.000Z (over 1 year ago)
- Last Synced: 2024-11-07T07:53:09.658Z (over 1 year ago)
- Topics: csharp, pattern, results
- Language: C#
- Homepage:
- Size: 63.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SmartResults
Lightweight .NET library to use result pattern instead of throwing exceptions.
[](https://opensource.org/licenses/MIT)
[](https://www.nuget.org/packages/SmartResults/)
## How to use it
- `Result` and `Result` are structs to prevent memory allocation
- Support for JSON
- Integrated JsonConverter for `Result` and `Result`
- Polymorphic `Error` objects
- Extensions for HttpResponseMessage
- Chaining flow
- Exception flow
### Create and use results
```csharp
Result result = Result.Ok();
if (result.IsSucceeded)
{
Console.WriteLine("Succeeded");
}
else
{
Console.WriteLine(result.Error);
}
```
### Create and use results with value
```csharp
Result result = Result.Ok(100);
if (result.IsSucceeded)
{
Console.WriteLine(result.Value);
}
else
{
Console.WriteLine(result.Error);
}
```
### Create and use failed results
```csharp
Result result = Result.Failed("Something is wrong!");
Result result = Result.Failed(new Error(..));
if (result.IsFailed)
{
Console.WriteLine(result.Error);
}
```
### Use match to evaluate the result
```csharp
Result result = Result.Ok(100);
bool b = result.Match(value => true, error => false);
```
### Explicit conversion
```csharp
Result result = Result.Ok(100).ToResult(x => x.ToString());
```
### Implicit operator for value
```csharp
Result result = 100;
int value = result;
```
### Exception flow by implicit operator
```csharp
int value = Create(); //throws exception if result is failed
```
### Chaining flow
```csharp
Result result1 = Create().Then(x => Console.WriteLine(x));
Result result2 = await Create().ThenAsync(async x => await Service.ExecuteAsync(x));
Result result3 = await CreateAsync().ThenAsync(x => Console.WriteLine(x));
Result result4 = await CreateAsync().ThenAsync(async x => await Service.ExecuteAsync(x));
```
### HttpClient extensions
```csharp
Result result1 = await httpClient.GetAsync("/").ReadResultFromJsonAsync();
Result result2 = await httpClient.GetAsync("/").ReadResultFromJsonAsync();
```
### Supports polymorphic error objects for JSON
```csharp
public class PermissionError : IError { }
Error.Register();
Result result = Result.Failed(new PermissionError("Error"));
string json = JsonSerializer.Serialize(result);
Result result2 = JsonSerializer.Deserialize(json);
result2.Error //PermissionError
```