DropBear.Codex.Core
2024.7.1
See the version list below for details.
dotnet add package DropBear.Codex.Core --version 2024.7.1
NuGet\Install-Package DropBear.Codex.Core -Version 2024.7.1
<PackageReference Include="DropBear.Codex.Core" Version="2024.7.1" />
paket add DropBear.Codex.Core --version 2024.7.1
#r "nuget: DropBear.Codex.Core, 2024.7.1"
// Install DropBear.Codex.Core as a Cake Addin #addin nuget:?package=DropBear.Codex.Core&version=2024.7.1 // Install DropBear.Codex.Core as a Cake Tool #tool nuget:?package=DropBear.Codex.Core&version=2024.7.1
DropBear.Codex.Core Result Types
Overview
The DropBear.Codex.Core library provides a set of Result types that offer a robust and type-safe way to handle operation outcomes in your C# applications. These types help eliminate null checks, reduce exceptions, and make error handling more explicit and easier to manage.
Available Result Types
Result
Result<T>
Result<TSuccess, TFailure>
ResultWithPayload<T>
Basic Usage
Result
Result
is the most basic type, representing success or failure without any associated value.
public Result DoSomething()
{
if (everythingWentWell)
return Result.Success();
else
return Result.Failure("Something went wrong");
}
var result = DoSomething();
if (result.IsSuccess)
Console.WriteLine("Operation succeeded");
else
Console.WriteLine($"Operation failed: {result.ErrorMessage}");
Result<T>
Result<T>
represents an operation that, when successful, returns a value of type T.
public Result<int> CalculateValue()
{
if (canCalculate)
return Result<int>.Success(42);
else
return Result<int>.Failure("Unable to calculate value");
}
var result = CalculateValue();
result.Match(
onSuccess: value => Console.WriteLine($"Calculated value: {value}"),
onFailure: (error, _) => Console.WriteLine($"Calculation failed: {error}")
);
Result<TSuccess, TFailure>
Result<TSuccess, TFailure>
allows you to specify both success and failure types.
public Result<int, string> Divide(int a, int b)
{
if (b == 0)
return Result<int, string>.Failed("Cannot divide by zero");
return Result<int, string>.Succeeded(a / b);
}
var result = Divide(10, 2);
var output = result.Match(
onSuccess: value => $"Result: {value}",
onFailure: error => $"Error: {error}"
);
Console.WriteLine(output);
ResultWithPayload<T>
ResultWithPayload<T>
is useful when you need to include additional metadata or when working with serialized data.
public ResultWithPayload<UserData> GetUserData(int userId)
{
var userData = // ... fetch user data
if (userData != null)
return ResultWithPayload<UserData>.SuccessWithPayload(userData);
else
return ResultWithPayload<UserData>.FailureWithPayload("User not found");
}
var result = GetUserData(123);
if (result.IsValid)
{
var userData = result.DecompressAndDeserialize().ValueOrThrow();
Console.WriteLine($"User name: {userData.Name}");
}
else
{
Console.WriteLine($"Error: {result.ErrorMessage}");
}
Advanced Features
Chaining Operations
You can chain multiple operations using the Bind
method:
Result<int> GetNumber() => Result<int>.Success(10);
Result<int> Double(int n) => Result<int>.Success(n * 2);
Result<string> ToString(int n) => Result<string>.Success(n.ToString());
var result = GetNumber()
.Bind(Double)
.Bind(ToString);
// result will be Success("20") if all operations succeed
Transforming Results
Use the Map
method to transform the success value:
var result = GetNumber().Map(n => n.ToString());
// If GetNumber returns Success(10), result will be Success("10")
Error Handling
You can handle errors using the OnFailure
method:
GetNumber()
.OnFailure((error, ex) => Console.WriteLine($"An error occurred: {error}"))
.OnSuccess(n => Console.WriteLine($"The number is: {n}"));
Try-Catch Wrapper
The Try
method provides a convenient way to wrap operations that might throw exceptions:
var result = Result<int>.Try(() =>
{
// Some operation that might throw
return int.Parse("not a number");
});
// result will be a Failure containing the exception message
Best Practices
- Prefer using Result types over throwing exceptions for expected failure cases.
- Use the
Match
method to handle both success and failure cases explicitly. - Chain operations using
Bind
andMap
to create clean and readable code. - Use
ResultWithPayload<T>
when you need to include additional metadata or work with serialized data. - Leverage the
Try
method to convert exception-throwing code into Result-returning code.
By consistently using these Result types, you can make your code more predictable, easier to reason about, and less prone to runtime errors.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages (8)
Showing the top 5 NuGet packages that depend on DropBear.Codex.Core:
Package | Downloads |
---|---|
DropBear.Codex.Utilities
Provides utility classes and helpers for the DropBear.Codex ecosystem |
|
DropBear.Codex.StateManagement
Simplified state management and snapshot system for the DropBear Codex framework. |
|
DropBear.Codex.Serialization
Provides serialization and deserialization utilities for the DropBear.Codex ecosystem |
|
DropBear.Codex.Files
Provides file management and storage capabilities for the DropBear.Codex ecosystem |
|
DropBear.Codex.Hashing
Provides various hashing implementations and utilities for the DropBear.Codex ecosystem |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2024.11.5 | 116 | 11/12/2024 |
2024.9.44 | 156 | 9/20/2024 |
2024.9.43 | 167 | 9/16/2024 |
2024.9.42 | 149 | 9/16/2024 |
2024.9.32 | 151 | 9/13/2024 |
2024.9.25 | 174 | 9/11/2024 |
2024.9.1 | 159 | 9/8/2024 |
2024.8.54 | 171 | 9/6/2024 |
2024.8.36 | 158 | 9/2/2024 |
2024.7.1 | 131 | 7/24/2024 |
2024.6.2 | 168 | 6/19/2024 |
2024.5.9 | 181 | 5/19/2024 |
2024.5.8 | 102 | 5/19/2024 |
2024.5.6 | 261 | 5/4/2024 |
2024.5.5 | 108 | 5/4/2024 |
2024.5.4 | 133 | 5/4/2024 |
2024.5.3 | 56 | 5/3/2024 |
2024.5.2 | 69 | 5/3/2024 |
2024.5.1 | 70 | 5/3/2024 |
2024.4.8 | 342 | 4/30/2024 |
2024.4.5 | 576 | 4/17/2024 |
2024.4.4 | 112 | 4/16/2024 |
2024.4.3 | 102 | 4/16/2024 |
2024.4.1 | 168 | 4/13/2024 |
2024.3.13 | 364 | 3/28/2024 |
2024.3.12 | 173 | 3/21/2024 |
2024.3.11 | 188 | 3/16/2024 |
2024.3.10 | 119 | 3/16/2024 |
2024.3.9 | 111 | 3/15/2024 |
2024.3.6 | 123 | 3/15/2024 |
2024.3.5 | 119 | 3/13/2024 |
2024.3.4 | 162 | 3/11/2024 |
2024.3.1 | 125 | 3/2/2024 |
2024.2.34 | 134 | 2/26/2024 |
2024.2.31 | 119 | 2/26/2024 |
2024.2.30 | 157 | 2/22/2024 |
2024.2.25 | 146 | 2/21/2024 |