ResultWrapper 1.0.1
dotnet add package ResultWrapper --version 1.0.1
NuGet\Install-Package ResultWrapper -Version 1.0.1
<PackageReference Include="ResultWrapper" Version="1.0.1" />
<PackageVersion Include="ResultWrapper" Version="1.0.1" />
<PackageReference Include="ResultWrapper" />
paket add ResultWrapper --version 1.0.1
#r "nuget: ResultWrapper, 1.0.1"
#:package ResultWrapper@1.0.1
#addin nuget:?package=ResultWrapper&version=1.0.1
#tool nuget:?package=ResultWrapper&version=1.0.1
ResultHandling# Result Handling
Overview
Result Handling is a lightweight library for implementing the Result<T> pattern in .NET applications. It simplifies error handling and response management in ASP.NET Core APIs by providing a clean and consistent way to represent operation results with HTTP status codes, error messages, and seamless integration with ActionResult.
Features
- Generic and Non-Generic Results: Supports both
Result<T>(for operations returning a value) andResult(for operations without a return value). - Error Handling: Includes error messages and HTTP status codes for failed operations.
- ASP.NET Core Integration: Provides extension methods to convert results into
IActionResultfor clean API responses. - .NET 8 Support: Fully compatible with .NET 8 and C# 12.
Installation
You can install the library via NuGet:
dotnet add package ResultWrapper
Or add the following to your .csproj file:
<PackageReference Include="ResultWrapper" Version="1.0.0" />
Usage
1. Using Result<T>
For operations that return a value:
using ResultHandling.Models;
public Result<string> GetGreeting(bool isSuccess)
{
if (isSuccess)
{
return Result<string>.Success("Hello, World!");
}
return Result<string>.Failure("An error occurred.", 400);
}
2. Using Result
For operations that do not return a value:
using ResultHandling.Models;
public Result PerformOperation(bool isSuccess)
{
if (isSuccess)
{
return Result.Success();
}
return Result.Failure("Operation failed.", 500);
}
3. Converting to IActionResult in ASP.NET Core
Use the provided extension methods to convert Result or Result<T> into IActionResult:
using Microsoft.AspNetCore.Mvc;
using ResultHandling.Models;
using ResultHandling.Extensions;
[ApiController]
[Route("api/[controller]")]
public class ExampleController : ControllerBase
{
[HttpGet]
public IActionResult GetExample(bool isSuccess)
{
var result = isSuccess
? Result<string>.Success("Data retrieved successfully.")
: Result<string>.Failure("Failed to retrieve data.", 404);
return result.ToActionResult();
}
}
4. Handling Errors
You can include multiple error messages for failed operations:
var errors = new List<string> { "Error 1", "Error 2" };
var result = Result<string>.Failure(errors, 400);
Metadata
- Authors: Yogesh Karpe
- Company: YogeshKarpe
- Repository: GitHub Repository
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve the library.
Happy coding!
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.