DropBear.Codex.Core
2024.7.1
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 (6)
Showing the top 5 NuGet packages that depend on DropBear.Codex.Core:
Package | Downloads |
---|---|
DropBear.Codex.Utilities
DropBear Codex Utilities. |
|
DropBear.Codex.StateManagement
The `StateSnapshotManager` library provides a comprehensive solution for managing state snapshots in .NET applications. It supports automatic snapshotting, state reversion, and notifications upon state changes, making it ideal for applications that require historical state management or undo capabilities. |
|
DropBear.Codex.Serialization
This project provides advanced serialization and deserialization capabilities, supporting formats like JSON, MessagePack, and MemoryPack. It includes performance optimizations, error handling, and supports compression and encoding options. |
|
DropBear.Codex.Files
DropBear.Codex.Files is a versatile .NET library designed for efficient file management, offering seamless integration with advanced features such as delta updates for optimized storage and bandwidth usage. Tailored for applications requiring robust file operations, it provides a foundation for both local and cloud storage scenarios, ensuring high performance and scalability. (Advanced features available when paired with DropBear.Codex.DeltaBlobStorage package.) |
|
DropBear.Codex.Operations
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2024.7.1 | 70 | 7/24/2024 |
2024.6.2 | 135 | 6/19/2024 |
2024.5.9 | 147 | 5/19/2024 |
2024.5.8 | 87 | 5/19/2024 |
2024.5.6 | 201 | 5/4/2024 |
2024.5.5 | 86 | 5/4/2024 |
2024.5.4 | 111 | 5/4/2024 |
2024.5.3 | 49 | 5/3/2024 |
2024.5.2 | 55 | 5/3/2024 |
2024.5.1 | 57 | 5/3/2024 |
2024.4.8 | 248 | 4/30/2024 |
2024.4.5 | 436 | 4/17/2024 |
2024.4.4 | 90 | 4/16/2024 |
2024.4.3 | 86 | 4/16/2024 |
2024.4.1 | 133 | 4/13/2024 |
2024.3.13 | 274 | 3/28/2024 |
2024.3.12 | 144 | 3/21/2024 |
2024.3.11 | 145 | 3/16/2024 |
2024.3.10 | 95 | 3/16/2024 |
2024.3.9 | 95 | 3/15/2024 |
2024.3.6 | 105 | 3/15/2024 |
2024.3.5 | 103 | 3/13/2024 |
2024.3.4 | 114 | 3/11/2024 |
2024.3.1 | 110 | 3/2/2024 |
2024.2.34 | 115 | 2/26/2024 |
2024.2.31 | 96 | 2/26/2024 |
2024.2.30 | 130 | 2/22/2024 |
2024.2.25 | 117 | 2/21/2024 |