The core library for DropBear.Codex providing Result pattern, error handling, and telemetry infrastructure for .NET 9+ applications. Features type-safe error handling, functional extensions, and OpenTelemetry integration.
$ dotnet add package DropBear.Codex.CoreCore library providing Result pattern, functional extensions, and telemetry infrastructure for .NET 9+ applications.
dotnet add package DropBear.Codex.Core
using DropBear.Codex.Core.Results;
using DropBear.Codex.Core.Results.Errors;
// Create a result-returning method
public Result<int, ValidationError> Divide(int numerator, int denominator)
{
if (denominator == 0)
{
return Result<int, ValidationError>.Failure(
new ValidationError("Cannot divide by zero"));
}
return Result<int, ValidationError>.Success(numerator / denominator);
}
// Use pattern matching
var result = Divide(10, 2);
var message = result.Match(
success: value => $"Result: {value}",
failure: error => $"Error: {error.Message}"
);
// Chain operations
var doubled = result.Map(x => x * 2);
var combined = result.Bind(x => Divide(x, 3));
Type-safe error handling without exceptions:
Result<User, ValidationError> GetUser(int id)
{
if (id <= 0)
return Result<User, ValidationError>.Failure(
new ValidationError("Invalid ID"));
var user = _repository.Find(id);
return user != null
? Result<User, ValidationError>.Success(user)
: Result<User, ValidationError>.Failure(
new ValidationError("User not found"));
}
Map, Bind, Match and more:
var result = await GetUserAsync(userId)
.MapAsync(user => user.Email)
.BindAsync(email => SendEmailAsync(email))
.TapAsync(async () => await LogSuccessAsync())
.OnFailureAsync(async error => await LogErrorAsync(error));
Built-in OpenTelemetry support:
// Configure telemetry at startup
TelemetryProvider.Configure(options =>
{
options.Mode = TelemetryMode.BackgroundChannel;
options.ChannelCapacity = 10000;
});
// Automatic telemetry tracking
var result = Result<int, ValidationError>.Success(42);
// Telemetry automatically recorded!
First-class async operations:
public async Task<Result<Data, ErrorType>> ProcessAsync(
CancellationToken cancellationToken = default)
{
return await GetDataAsync(cancellationToken)
.MapAsync(async (data, ct) => await TransformAsync(data, ct), cancellationToken)
.BindAsync(async (data, ct) => await ValidateAsync(data, ct), cancellationToken);
}
Message wrapping with metadata:
var envelope = new EnvelopeBuilder<MyData>()
.WithPayload(myData)
.AddHeader("version", "1.0")
.AddHeader("source", "api")
.Build();
// Serialize and deserialize
var json = await envelopeSerializer.SerializeAsync(envelope);
var restored = await envelopeSerializer.DeserializeAsync(json);
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please read our Contributing Guidelines first.
Made with ❤️ by Terrence Kuchel