A .NET library which provides a Result type for representing the outcome of operations in C#.
Find a file
Louis Seubert 165e0c5c26
chore: remove async naming rule
Remove the async naming rule as the tooling does not really work out to detecting the right usage of the `Async` suffix.
2024-05-01 18:26:15 +02:00
.forgejo/workflows chore: adjust workflow to publish release nupkg 2024-05-01 16:23:17 +02:00
src feat: fix inconsistent naming of transformation functions 2024-05-01 17:55:07 +02:00
.editorconfig chore: remove async naming rule 2024-05-01 18:26:15 +02:00
.gitignore feat: initial project commit 2024-04-27 20:11:24 +02:00
Directory.Build.props feat: initial project commit 2024-04-27 20:11:24 +02:00
Directory.Build.targets feat: initial project commit 2024-04-27 20:11:24 +02:00
Directory.Packages.props feat: initial project commit 2024-04-27 20:11:24 +02:00
Geekeey.Extensions.Result.sln feat: initial project commit 2024-04-27 20:11:24 +02:00
global.json feat: initial project commit 2024-04-27 20:11:24 +02:00
LICENSE.md chore: add README and LICENSE to describe project 2024-05-01 18:03:11 +02:00
nuget.config feat: initial project commit 2024-04-27 20:11:24 +02:00
README.md chore: add README and LICENSE to describe project 2024-05-01 18:03:11 +02:00

Geekeey.Extensions.Result

A .NET library provides a Result 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

<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

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);
}
_ = 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();
	});