Nlabs.Result
9.0.0
dotnet add package Nlabs.Result --version 9.0.0
NuGet\Install-Package Nlabs.Result -Version 9.0.0
<PackageReference Include="Nlabs.Result" Version="9.0.0" />
paket add Nlabs.Result --version 9.0.0
#r "nuget: Nlabs.Result, 9.0.0"
// Install Nlabs.Result as a Cake Addin #addin nuget:?package=Nlabs.Result&version=9.0.0 // Install Nlabs.Result as a Cake Tool #tool nuget:?package=Nlabs.Result&version=9.0.0
NLabs.Result NuGet Package
Overview
The NLabs.Result
package is designed to encapsulate the result of operations in .NET applications, offering a structured way to handle success and failure states with associated data or error messages. It is ideal for improving error handling and response consistency across various application layers.
Features
- Generic Result Type: Facilitates strong typing of the operation outcome, accommodating any data type.
- Error Handling: Enables capturing multiple error messages or detailed error structures using
Dictionary<string, List<string>>
, suitable for scenarios requiring detailed feedback. - HTTP Status Code Integration: Aligns operation results with HTTP response standards, enhancing API development.
- Implicit Conversions: Streamlines result creation from data or errors through implicit conversion operators.
Getting Started
Installation
To integrate NLabs.Result
into your project, install it via the NuGet package manager:
Install-Package NLabs.Result
Or through the .NET CLI:
dotnet add package NLabs.Result
Usage
- For a successful operation, instantiate a Result object with the desired data:
var successResult = new Result<string>("Operation successful.");
- Alternatively, leverage implicit conversion from data:
Result<string> result = "Operation successful.";
- For failures, create a Result object with an HTTP status code and error messages:
var errorResult = new Result<string>(400, new List<string> { "Error 1", "Error 2" });
- Or use implicit conversion from error details:
Result<string> result = (400, new List<string> { "Error 1", "Error 2" });
- For single error messages:
Result<string> result = (400, "Single error message");
- One error message return 400 status code
Result<string> result = Result<string>.Fail("Is fail!"); //return 400 status code
- Multiple error messages return 400 status code
Result<string> result = Result<string>.Fail(new List<string>() {"Is fail!","Is not unique!"}); //return 400 status code
- **For example, to return multiple validation errors for specific fields:
var errorDetails = new Dictionary<string, List<string>>
{
{ "Email", new List<string> { "Invalid email format", "Email already in use" } },
{ "Password", new List<string> { "Password must be at least 6 characters long" } }
};
Result<string> result = (400, errorDetails);
- **Extended Failure Method for Error Details
- In scenarios where detailed validation errors are required, you can use the following syntax:
Result<string> errorResult = Result<string>.Fail(400, new Dictionary<string, List<string>>
{
{ "Email", new List<string> { "Invalid email", "Email is already taken" } },
{ "Password", new List<string> { "Password must be stronger" } }
});
Contributing
We welcome contributions! Feel free to open an issue or submit a pull request on our GitHub repository for any suggestions or improvements.
License
NLabs.Result
is licensed under the MIT License. See the LICENSE file in the source repository for full details.
This Markdown formatted README provides a comprehensive guide on how to use the `NLabs.Result` package, suitable for your project's repository or documentation.
This README file has been updated to align with the new code, incorporating the `Success` and `Fail` methods, the default `400` status code, and lambda expressions.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
-
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.