A .NET library which provides a Result type for representing the outcome of operations in C#.
Find a file
Louis Seubert b3ed128536
All checks were successful
default / dotnet default workflow (8.0) (push) Successful in 1m10s
fix: type mismatch in task extensions
2024-07-03 22:26:43 +02:00
.forgejo/workflows chore: adjust workflow to publish release nupkg 2024-05-01 16:23:17 +02:00
src fix: type mismatch in task extensions 2024-07-03 22:26:43 +02:00
.editorconfig chore: add copyright header 2024-05-10 22:20:09 +02:00
.gitignore feat: initial project commit 2024-04-27 20:11:24 +02:00
Directory.Build.props chore: add copyright header 2024-05-10 22:20:09 +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-10 22:20:09 +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-10 22:20:09 +02:00

Geekeey.Extensions.Result

Result is a .NET library which 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();
	});