A lightweight and powerful Result Pattern implementation for .NET 9. This library is designed to make error handling and validation operations cleaner and more readable.
- ✅ Result Pattern Implementation
- ✅ Comprehensive error handling
- ✅ Built-in validation support
- ✅ Full async/await support
- ✅ LINQ-style operations
- ✅ Fluent API for chaining operations
- ✅ Extensible structure
- ✅ Lightweight design
- ✅ .NET 9 Support
- .NET 9.0 or later
dotnet add package TinyResult
// Creating a successful result
var success = Result<int>.Success(42);
// Creating a failed result
var failure = Result<int>.Failure(ErrorCode.NotFound, "Item not found");
// Pattern matching
var message = success.Match(
value => $"Success: {value}",
error => $"Error: {error.Message}"
);
// Chaining operations
var result = ResultPipeline<int>
.Start(10)
.Map(x => x * 2)
.Validate(x => x > 0, ErrorCode.ValidationError, "Value must be positive")
.OnSuccess(x => Console.WriteLine($"Value: {x}"))
.OnFailure(error => Console.WriteLine($"Error: {error.Message}"))
.Build();
// Creating detailed errors
var error = Error.Create(
ErrorCode.ValidationError,
"Invalid input",
new Dictionary<string, object>
{
{ "Field", "Email" },
{ "Value", "invalid-email" }
}
);
// Exception handling
var result = Result.FromTry(
() => int.Parse("not-a-number"),
ex => Error.Create(ErrorCode.InternalError, "Failed to parse number", ex)
);
// Creating validation result
var validationResult = ValidationResult.Create()
.AddError("Email", "Invalid email format")
.AddError("Password", "Password too short");
// Using validation with Result
var result = Result<int>.Success(42)
.Validate(value => value > 0, ErrorCode.ValidationError, "Value must be positive")
.Validate(value => value < 100, ErrorCode.ValidationError, "Value must be less than 100");
// Async result creation
var asyncResult = await Result.FromTryAsync(
async () => await GetDataAsync(),
ex => Error.Create(ErrorCode.InternalError, "Failed to get data", ex)
);
// Async pipeline
var pipelineResult = await ResultPipeline<int>
.Start(10)
.ThenAsync(async x => await ProcessAsync(x))
.MapAsync(async x => await TransformAsync(x))
.BuildAsync();
// Combining two results
var result1 = Result<int>.Success(10);
var result2 = Result<int>.Success(20);
var combinedResult = Result.Combine(
result1,
result2,
(value1, value2) => value1 + value2
);
// Combining multiple results
var results = new[]
{
Result<int>.Success(1),
Result<int>.Success(2),
Result<int>.Success(3)
};
var combinedResults = Result.Combine(results);
For more detailed information, please visit our documentation.
We welcome contributions! Please see our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.