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 | 137 | 11/12/2024 |
2024.9.44 | 163 | 9/20/2024 |
2024.9.43 | 174 | 9/16/2024 |
2024.9.42 | 156 | 9/16/2024 |
2024.9.32 | 158 | 9/13/2024 |
2024.9.25 | 181 | 9/11/2024 |
2024.9.1 | 166 | 9/8/2024 |
2024.8.54 | 176 | 9/6/2024 |
2024.8.36 | 163 | 9/2/2024 |
2024.7.1 | 136 | 7/24/2024 |
2024.6.2 | 173 | 6/19/2024 |
2024.5.9 | 186 | 5/19/2024 |
2024.5.8 | 107 | 5/19/2024 |
2024.5.6 | 266 | 5/4/2024 |
2024.5.5 | 113 | 5/4/2024 |
2024.5.4 | 138 | 5/4/2024 |
2024.5.3 | 61 | 5/3/2024 |
2024.5.2 | 74 | 5/3/2024 |
2024.5.1 | 75 | 5/3/2024 |
2024.4.8 | 347 | 4/30/2024 |
2024.4.5 | 581 | 4/17/2024 |
2024.4.4 | 117 | 4/16/2024 |
2024.4.3 | 107 | 4/16/2024 |
2024.4.1 | 173 | 4/13/2024 |
2024.3.13 | 369 | 3/28/2024 |
2024.3.12 | 178 | 3/21/2024 |
2024.3.11 | 193 | 3/16/2024 |
2024.3.10 | 124 | 3/16/2024 |
2024.3.9 | 116 | 3/15/2024 |
2024.3.6 | 128 | 3/15/2024 |
2024.3.5 | 126 | 3/13/2024 |
2024.3.4 | 167 | 3/11/2024 |
2024.3.1 | 130 | 3/2/2024 |
2024.2.34 | 141 | 2/26/2024 |
2024.2.31 | 126 | 2/26/2024 |
2024.2.30 | 164 | 2/22/2024 |
2024.2.25 | 153 | 2/21/2024 |