RW 1.0.5
dotnet add package RW --version 1.0.5
NuGet\Install-Package RW -Version 1.0.5
<PackageReference Include="RW" Version="1.0.5" />
paket add RW --version 1.0.5
#r "nuget: RW, 1.0.5"
// Install RW as a Cake Addin
#addin nuget:?package=RW&version=1.0.5
// Install RW as a Cake Tool
#tool nuget:?package=RW&version=1.0.5
📦 Package Name Update: RW is now ResponseWrapper
We are excited to announce that we have updated the package name from "RW" to "ResponseWrapper" to better reflect the purpose and contents of the package.
🚀 What's Changed
- OldPackage: RW
- NewPackage: ResponseWrapper
- Upgrade Instructions: If you are currently using the "RW" package, please update your package references and code accordingly to use ResponseWrapper.
We believe this update will provide greater clarity and consistency when working with service result responses. Thank you for your continued support, and we look forward to bringing you more improvements in the future!
Response Wrapper
The ResponseWrapper
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. 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.
Note
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.
Installation
To install the ResponseWrapper
package, you can use the following command in the NuGet Package Manager Console: dotnet add package RW
Alternatively, you can search for "RW" 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.
Example: How to use Non Generic IResultWrapper
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet(Name = "GetWeatherForecast")]
public ActionResult Get()
{
var result = Weather.GetWeatherInfo();
/*
// These are the properties available in IResultWrapper
var payload = result.Payload; // To get the payload
bool isSuccess = result.IsSuccess; // To check whether a success case is returned or a failure
string message = result.Message; // Get the failure or success message if you have passed it as an argument
int statusCode = result.Code; // Get the failure or success codes if you have passed them as arguments
var errors = result.Errors; // To get a list of errors
*/
// Note: If you are using non generic IResult Wrapper, you can still get strongly typed payload by using TypedPayload method
WeatherForecast[]? weatherForecasts = result.TypedPayload<WeatherForecast[]>();
if (result.IsSuccess)
{
return Ok(result);
}
return BadRequest();
}
}
public static class Weather
{
public static IResultWrapper GetWeatherInfo()
{
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
var weatherForecasts = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Summary = summaries[Random.Shared.Next(summaries.Length)],
TemperatureC = Random.Shared.Next(-20, 55),
})
.ToArray();
if (weatherForecasts != null)
{
return ResultWrapper.Success(weatherForecasts, "Hello", 300);
}
return ResultWrapper.Failure("Not Found", 404);
}
}
Example: How to use Generic IResultWrapper<T>
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet(Name = "GetWeatherForecast")]
public ActionResult Get()
{
var result = Weather.GetWeatherInfo();
/*
// These are the properties available in IResultWrapper
var payload = result.Payload; // To get the payload
bool isSuccess = result.IsSuccess; // To check whether a success case is returned or a failure
string message = result.Message; // Get the failure or success message if you have passed it as an argument
int statusCode = result.Code; // Get the failure or success codes if you have passed them as arguments
var errors = result.Errors; // To get a list of errors
*/
if (result.IsSuccess)
{
return Ok(result);
}
return BadRequest();
}
}
public static class Weather
{
public static IResultWrapper<WeatherForecast[]> GetWeatherInfo()
{
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
var weatherForecasts = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Summary = summaries[Random.Shared.Next(summaries.Length)],
TemperatureC = Random.Shared.Next(-20, 55),
})
.ToArray();
if (weatherForecasts != null)
{
return ResultWrapper.Success<WeatherForecast[]>(weatherForecasts, "Hello", 300);
}
return ResultWrapper.Failure<WeatherForecast[]>("Not Found", 404);
}
}
Conclusion
The Response Wrapper 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 | 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.
CHANGELOG.md