chore: add README and LICENSE to describe project
This commit is contained in:
parent
fb6e06952c
commit
58a3666eac
2 changed files with 358 additions and 0 deletions
71
README.md
Normal file
71
README.md
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
# `Geekeey.Extensions.Result`
|
||||
|
||||
Result is a .NET library which provides a Result<T> type for representing the outcome of operations in C#. It offers a
|
||||
clean and concise way to handle success and failure scenarios, promoting better error handling and code readability.
|
||||
|
||||
## Features
|
||||
|
||||
- **Success and Failure States:** Represent successful outcomes with a value (`Prelude.Success()`) or failures with an
|
||||
error (`Prelude.Failure()`).
|
||||
- **Immutability:** `Result<T>` objects are immutable, ensuring thread safety and preventing accidental modification.
|
||||
- **Chaining Operations:** Methods like `Map` and `Then` allow for chaining operations on successful results, promoting
|
||||
a functional programming style.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Install the NuGet package:
|
||||
|
||||
```
|
||||
dotnet add package Geekeey.Extensions.Result
|
||||
```
|
||||
|
||||
You may need to add our NuGet Feed to your `nuget.config` this can be done by adding the following lines
|
||||
|
||||
```xml
|
||||
<packageSources>
|
||||
<add key="geekeey" value="https://git.geekeey.de/api/packages/geekeey/nuget/index.json" />
|
||||
</packageSources>
|
||||
```
|
||||
|
||||
### Configure
|
||||
|
||||
The package adds a global using for the functions in the `Prelude` class when the `ImplicitUsings` is enabled in
|
||||
the `.csproj` file. This global using is recommended but can also be removed, by removing
|
||||
the `Geekeey.Extensions.Result.Prelude` value from the item group `<Usings>`. For more information about that see
|
||||
the `Project.props` file inside the project tree.
|
||||
|
||||
### Usage
|
||||
|
||||
```csharp
|
||||
public Result<int> Divide(int dividend, int divisor)
|
||||
{
|
||||
if (divisor == 0)
|
||||
{
|
||||
return Prelude.Failure<int>("Division by zero");
|
||||
}
|
||||
|
||||
return Prelude.Success(dividend / divisor);
|
||||
}
|
||||
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
Console.WriteLine("Result: " + result.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Error: " + result.Error);
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
_ = await Prelude.Try(() => File.ReadAllLines("i_do_not_exist.txt"))
|
||||
.ThenAsync(static async Task<Result<IReadOnlyList<int>>> (list) =>
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
Task<Result<int>> DoSomeThing(string line)
|
||||
=> Prelude.TryAsync(() => client.GetAsync(line))
|
||||
.Map(static async response => int.Parse(await response.Content.ReadAsStringAsync()));
|
||||
var results = await Task.WhenAll(list.Select(DoSomeThing).ToArray());
|
||||
return results.Join();
|
||||
});
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue