-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
107 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# This workflow will build a .NET project | ||
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net | ||
|
||
name: .NET | ||
|
||
on: | ||
push: | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v3 | ||
with: | ||
dotnet-version: 6.0.x | ||
- name: Restore dependencies | ||
run: dotnet restore | ||
- name: Build | ||
run: dotnet build --no-restore | ||
- name: Test | ||
run: dotnet test --no-build --verbosity normal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,84 @@ | ||
# ResponseBase | ||
ResponseBase is a generic return type of a service or api which contains: | ||
# ResponseBase Pattern in C# | ||
|
||
`ResponseBase` is a generic return type designed for use in services or APIs. It standardizes the way responses are handled, encapsulating key elements of the response in a structured format that improves readability, maintainability, and usability across the application. | ||
|
||
## Features of ResponseBase | ||
|
||
The `ResponseBase` class includes the following components: | ||
|
||
- **Status**: Represents the status of the operation, typically indicating success, failure, or any other defined state. | ||
- **Message**: Provides additional context or information about the operation, such as error messages or success confirmations. | ||
- **Data**: Contains the payload of the response, allowing any type of data to be returned (generic type `T`). | ||
|
||
## Implicit Conversions | ||
|
||
`ResponseBase` is designed with flexibility in mind, allowing implicit conversions between various types: | ||
|
||
1. **Conversion to ObjectResult**: `ResponseBase` can be implicitly converted to an `ObjectResult`, making it easy to return standardized responses from API controllers in ASP.NET Core. | ||
|
||
2. **Conversion to HttpStatusCodes**: `ResponseBase` can also be implicitly converted to HTTP status codes, simplifying the mapping between service results and HTTP responses. | ||
|
||
3. **Conversion from HttpStatusCodes and Tuples**: Both HTTP status codes and tuples can be implicitly converted to `ResponseBase`. This allows you to quickly create `ResponseBase` instances from common status codes or value tuples, making the API easier to use and reducing boilerplate code. | ||
|
||
## Benefits of Using ResponseBase | ||
|
||
- **Consistency**: By using `ResponseBase`, all services and APIs return responses in a consistent format, making it easier to handle responses across the application. | ||
|
||
- **Readability**: The structure of `ResponseBase` makes it clear what the status of a response is, what message (if any) accompanies it, and what data is being returned. | ||
|
||
- **Error Handling**: Centralizes error handling by encapsulating errors within the `Status` and `Message` fields, providing a clear and uniform way to handle exceptions and errors. | ||
|
||
- **Extensibility**: The generic type parameter `T` allows `ResponseBase` to be used with any data type, making it highly flexible and suitable for a wide range of applications. | ||
|
||
## Example Usage | ||
|
||
### Returning a Response from a Service | ||
|
||
Here’s a simple example of using `ResponseBase` in a service method: | ||
|
||
```csharp | ||
public ResponseBase<User> GetUserById(int userId) | ||
{ | ||
var result = new ResponseBase<User>(); | ||
var user = _userRepository.FindById(userId); | ||
if (user == null) | ||
{ | ||
return result.NotFound(); // Sets status to NotFound and returns | ||
} | ||
return result.Success(user); // Sets status to Success and returns the user data | ||
} | ||
``` | ||
|
||
Returning a Response from an API Controller : | ||
|
||
In an ASP.NET Core API controller, you can return a ResponseBase directly due to its implicit conversion to ObjectResult: | ||
```csharp | ||
[HttpGet("{id}")] | ||
public IActionResult GetUser(int id) | ||
{ | ||
var response = _userService.GetUserById(id); | ||
return response; // Implicitly converts to ObjectResult | ||
} | ||
``` | ||
|
||
You can also convert HTTP status codes directly into ResponseBase: | ||
|
||
```csharp | ||
public ResponseBase<string> DeleteUser(int userId) | ||
{ | ||
var result = _userRepository.Delete(userId); | ||
if (!result) | ||
{ | ||
return HttpStatusCode.BadRequest; // Converts to ResponseBase with appropriate status | ||
} | ||
|
||
return HttpStatusCode.OK; // Converts to ResponseBase with status success | ||
} | ||
``` | ||
|
||
|
||
|
||
* Status | ||
* Message | ||
* Data | ||
|
||
It can implicitly be converted to objectResult and httpStatusCodes, | ||
and also httpStatusCodes and tuples can be implicitly converted to responseBase. | ||
|
||
|
||
|