RW 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RW --version 1.0.0
NuGet\Install-Package RW -Version 1.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="RW" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RW --version 1.0.0
#r "nuget: RW, 1.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 RW as a Cake Addin
#addin nuget:?package=RW&version=1.0.0

// Install RW as a Cake Tool
#tool nuget:?package=RW&version=1.0.0

Result Wrapper

The ResultWrapper package provides a set of static methods in the ResultWrapper class, allowing you to easily create and work with success and failure scenarios. These wrappers encapsulate various data, including payloads, errors, success and error messages, status codes, and pagination information, providing a standardized way to handle and represent the results of operations.

Why to use?

The main benefit of this package is to provide access to strongly typed and loosely typed payloads and errors within controllers, as well as when returning responses from your API. By using this package, the returned response will only include the properties that you have explicitly returned. It ensures that no extra keys are included in the response.

It's important to note that the return type of the service or method where this package is used depends on whether it is generic or non-generic. If you use IResultWrapper as the return type, you will receive a non-generic result by default. On the other hand, if you use IResultWrapper<Type>, you will receive a strongly typed object.

Please let me know if there is anything else I can assist you with.

Installation

To install the ResultWrapper package, you can use the following command in the NuGet Package Manager Console: dotnet add package ResultWrapper

Alternatively, you can search for "ResultWrapper" in the NuGet Package Manager UI and install it from there.

Usage

The ResultWrapper class provides several overloaded methods for handling different success and failure scenarios. The methods are categorized into generic and non-generic overloads, allowing you to work with specific types or use the more general object type.

Success Generic Overloads

Return empty success result.

return ResultWrapper.Success<MyPayload>();

Return success result with payload only.

var payload = new MyPayload();
return ResultWrapper.Success<MyPayload>(payload);

Return success result with message and status

return ResultWrapper.Success<MyPayload>("Success message", 200);

Return success result with a payload, message, and status code.

var payload = new MyPayload();
return ResultWrapper.Success<MyPayload>(payload, "Success message", 200);

Return success result with a payload, pagination information, message, and status code.

var payload = new MyPayload();
var paginationInfo = new Pagination();
return ResultWrapper.Success(payload, paginationInfo, "Success message", 200);
Success Non Generic Overloads

Return empty success result.

return ResultWrapper.Success();

Return success result with payload only.

var payload = new MyPayload();
return ResultWrapper.Success(payload);

Return success result with message and status

return ResultWrapper.Success("Success message", 200);

Return success result with a payload, message, and status code.

var payload = new MyPayload();
return ResultWrapper.Success(payload, "Success message", 200);

Return success result with a payload, pagination information, message, and status code.

var payload = new MyPayload();
var paginationInfo = new Pagination();
return ResultWrapper.Success(payload, paginationInfo, "Success message", 200);
Failure Generic Overloads

Return empty failure.

return ResultWrapper.Failure<MyErrors>();

Return failure with a message and status code.

return ResultWrapper.Failure<MyErrors>("Error message", 500);

Return failure with errors of type TErrors

var errors = new MyErrors();
return ResultWrapper.Failure<MyErrors>(errors);

Return failure with errors of type TErrors, message, and status code.

var errors = new MyErrors();
var result = ResultWrapper.Failure<MyErrors>(errors, "Error message", 500);

Failure Non Generic Overloads

Return empty failure.

return ResultWrapper.Failure();

Return failure with a message and status code.

return ResultWrapper.Failure("Error message", 500);

Return failure with errors

var errors = new MyErrors();
return ResultWrapper.Failure(errors);

Return failure with errors, message, and status code.

var errors = new MyErrors();
var result = ResultWrapper.Failure(errors, "Error message", 500);

Sample Code

Note: The result wrapper will return a payload of type WeatherForecastInfo[] when using a generic approach. However, when a non-generic approach is used, the payload will be of type object.

public static class WeatherForecast
{
    public static IResultWrapper<WeatherForecastInfo[]> GetWeatherInfo()
    {
        var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };

        var result = Enumerable.Range(1, 5).Select(index =>
                new WeatherForecastInfo
                (
                    DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                    Random.Shared.Next(-20, 55),
                    summaries[Random.Shared.Next(summaries.Length)]
                ))
                .ToArray();
        if (result != null)
        {
            return ResultWrapper.Success<WeatherForecastInfo[]>(result, "Hello", 300);
        }
        return ResultWrapper.Failure<WeatherForecastInfo[]>("Not Found", 404);
    }

    public void ReturnWeatherInfo()
    {
        IResultWrapper<WeatherForecastInfo[]>? weatherInfo = GetWeatherInfo();
        // I am using generic version; that's why the payload will be of type WeatherForecastInfo[].
        WeatherForecastInfo[]? payload = weatherInfo.Payload;
        bool isSuccess = weatherInfo.IsSuccess;
        string message = weatherInfo.Message;
        int statusCode = weatherInfo.Code;
        var errors = weatherInfo.Errors;
    }

}
Conclusion

The ResultWrapper package simplifies the process of creating and working with result success and failure scenarios. It provides a consistent way to handle and represent the results

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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
1.0.5 126 9/20/2023
1.0.4 101 9/18/2023
1.0.3 96 9/14/2023
1.0.2 134 7/15/2023
1.0.1 134 7/15/2023
1.0.0 139 7/14/2023