Result.MCA
8.0.0
dotnet add package Result.MCA --version 8.0.0
NuGet\Install-Package Result.MCA -Version 8.0.0
<PackageReference Include="Result.MCA" Version="8.0.0" />
paket add Result.MCA --version 8.0.0
#r "nuget: Result.MCA, 8.0.0"
// Install Result.MCA as a Cake Addin #addin nuget:?package=Result.MCA&version=8.0.0 // Install Result.MCA as a Cake Tool #tool nuget:?package=Result.MCA&version=8.0.0
MCA.Result Library
Overview
Note: This package is prepared for personal development and is not yet intended for use in real projects!
The MCA.Result
library provides a generic Result<T>
class that represents the outcome of an operation. It encapsulates both successful and unsuccessful results, including relevant data, error messages, and status codes. This approach is particularly useful in APIs and services where you need to standardize the response format across different operations.
Features
- Success and Failure Handling: Provides a clear distinction between successful and unsuccessful operations.
- Status Codes: Supports HTTP status codes to provide additional context for the result.
- Error Messages: Allows for single or multiple error messages to be returned when an operation fails.
- JSON Serialization: Ensures that irrelevant fields (like
StatusCode
) are ignored during JSON serialization.
Installation
To use the MCA.Result
class in your project please download from 'Manage NuGet Packages' screen, otherwise I still don't know other ways. Ensure that you have references to the necessary libraries, such as System.Text.Json
for JSON serialization.
Usage
Success Result
To create a successful result, use the Succeed
method. This method returns a Result<T>
object with the IsSuccessful
flag set to true
and the StatusCode
set to 200
.
var successResult = Result<string>.Succeed("Operation was successful.");
Failure Result
To create a failure result, use the Failure method. This method allows you to specify an error message and a status code.
Single Error Message
var failureResult = Result<string>.Failure("An error occurred.", 500);
Multiple Error Messages
If you need to return multiple error messages, use the overloaded Failure method:
var errorMessages = new List<string> { "Error 1", "Error 2" };
var failureResult = Result<string>.Failure(errorMessages, 400);
Accessing the Result
You can access the properties of the Result<T> object to inspect the outcome of an operation:
if (result.IsSuccessful)
{
Console.WriteLine("Success: " + result.Data);
}
else
{
Console.WriteLine("Failure: " + string.Join(", ", result.ErrorMessages));
Console.WriteLine("Status Code: " + result.StatusCode);
}
Contributing
This is still so far away from perfection please don't lose your time here. You can find better things than this one.
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 | 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. |
-
net8.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.
Version | Downloads | Last updated |
---|---|---|
8.0.0 | 117 | 8/19/2024 |
This is first commit.