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 | 133 | 11/12/2024 |
2024.9.44 | 161 | 9/20/2024 |
2024.9.43 | 172 | 9/16/2024 |
2024.9.42 | 154 | 9/16/2024 |
2024.9.32 | 156 | 9/13/2024 |
2024.9.25 | 179 | 9/11/2024 |
2024.9.1 | 164 | 9/8/2024 |
2024.8.54 | 174 | 9/6/2024 |
2024.8.36 | 161 | 9/2/2024 |
2024.7.1 | 134 | 7/24/2024 |
2024.6.2 | 171 | 6/19/2024 |
2024.5.9 | 184 | 5/19/2024 |
2024.5.8 | 105 | 5/19/2024 |
2024.5.6 | 264 | 5/4/2024 |
2024.5.5 | 111 | 5/4/2024 |
2024.5.4 | 136 | 5/4/2024 |
2024.5.3 | 59 | 5/3/2024 |
2024.5.2 | 72 | 5/3/2024 |
2024.5.1 | 73 | 5/3/2024 |
2024.4.8 | 345 | 4/30/2024 |
2024.4.5 | 579 | 4/17/2024 |
2024.4.4 | 115 | 4/16/2024 |
2024.4.3 | 105 | 4/16/2024 |
2024.4.1 | 171 | 4/13/2024 |
2024.3.13 | 367 | 3/28/2024 |
2024.3.12 | 176 | 3/21/2024 |
2024.3.11 | 191 | 3/16/2024 |
2024.3.10 | 122 | 3/16/2024 |
2024.3.9 | 114 | 3/15/2024 |
2024.3.6 | 126 | 3/15/2024 |
2024.3.5 | 124 | 3/13/2024 |
2024.3.4 | 165 | 3/11/2024 |
2024.3.1 | 128 | 3/2/2024 |
2024.2.34 | 139 | 2/26/2024 |
2024.2.31 | 124 | 2/26/2024 |
2024.2.30 | 162 | 2/22/2024 |
2024.2.25 | 151 | 2/21/2024 |