CD.Results
2.0.0
dotnet add package CD.Results --version 2.0.0
NuGet\Install-Package CD.Results -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CD.Results" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CD.Results --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CD.Results, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install CD.Results as a Cake Addin #addin nuget:?package=CD.Results&version=2.0.0 // Install CD.Results as a Cake Tool #tool nuget:?package=CD.Results&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Result Pattern
Description
A lightweight and flexible Result pattern implementation for .NET applications. Simplifies API response handling and error management.
Installation
dotnet add package CD.Results
Features
- Generic Result Pattern implementation
- Built-in HTTP Status Code support
- Error message management
- JSON serialization support
- Easy to use Success/Failure patterns
- Common HTTP status code helpers (NotFound, BadRequest, etc.)
Quick Start
- Return a success result:
public async Task<Result<UserDto>> GetUserAsync(int id)
{
var user = await _repository.GetByIdAsync(id);
var userDto = _mapper.Map<UserDto>(user);
return Result<UserDto>.Success(userDto);
}
- Return a failure result:
public async Task<Result<UserDto>> GetUserAsync(int id)
{
var user = await _repository.GetByIdAsync(id);
if (user is null)
return Result<UserDto>.NotFound($"User not found with id: {id}");
var userDto = _mapper.Map<UserDto>(user);
return Result<UserDto>.Success(userDto);
}
- Use in API Controller:
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id)
{
var result = await _userService.GetUserAsync(id);
return StatusCode((int)result.StatusCode, result);
}
}
Response Examples
Success Response
{
"data": {
"id": 1,
"name": "Alparslan Akbas",
"email": "alparslan@example.com"
},
"isSuccessful": true,
"statusCode": 200,
"errorMessages": null
}
Failure Response
{
"data": null,
"isSuccessful": false,
"statusCode": 404,
"errorMessages": ["User not found with id: 1"]
}
Common Methods
// Success results
Result<T>.Success(data);
// Failure results
Result<T>.Failure(HttpStatusCode.BadRequest, "Invalid request");
Result<T>.NotFound("Resource not found");
Result<T>.BadRequest("Validation failed");
Result<T>.Unauthorized("Access denied");
Result<T>.Forbidden("Not allowed");
Requirements
- .NET 9.0 or later
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.