How to utilize the REPR design pattern in ASP.NET Core

Uncategorized

Style patterns have developed to attend to problems that are typically encountered in software application applications. They are services to repeating problems and complexities in software design. We’ve talked about numerous design patterns here consisting of the specification pattern, the system of work pattern, the null item pattern, the options pattern, the flyweight pattern, the command pattern, the interpreter pattern, and the singleton pattern.

In this post we will delve into the REPR (request-endpoint-response) design pattern, how it streamlines the development of APIs, and how it can be executed in C#.

To utilize the code examples offered in this post, you must have Visual Studio 2022 installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.

What is the REPR style pattern?The REPR style pattern is a technique that helps developers boost code maintainability, reusability, and extensibility by isolating concerns. Designers might develop well-structured and easily expandable APIs by focusing on the demand, endpoint, and response.The REPR style pattern promotes modularization and guarantees an unique separation between the input request, the reasoning at the endpoint, and the output response. Analogous to the vertical slice architecture, the REPR style pattern simplifies API advancement by arranging your APIs around endpoints rather of controllers. Keep in mind that the REPR style pattern is neither REST-based nor resource-based. Instead, it is a pattern utilized for defining your API endpoints.Why utilize the REPR

design pattern?The MVC(model-view-controller)

pattern has actually typically been utilized to develop API endpoints. While the MVC pattern does have several advantages, one of the major constraints of this approach is puffed up controllers, aka the”inflamed controller problem.”This is because you frequently wind up with controllers having disparate methods that are not cohesive( they never ever call each other)and don’t actually belong together. As an outcome, the application wanders away from REST-based methods and winds up with a loose collection of methods exposed over HTTP endpoints. The REPR pattern solves the issue of controller bloat by removing the need

to have numerous action approaches in one controller. Rather, the REPR pattern adheres to the single responsibility concept, permitting you to have one controller per action supported in a normal usage case. Other key advantages consist of the separation of concerns, improved code reusability, improved readability and maintainability, much better testability and simpler debugging, and enhanced security and scalability.However, the REPR pattern also has certain disadvantages. These consist of increased intricacy and duplication of code. Create an ASP.NET Core Web API project in Visual Studio 2022 To develop an ASP.NET Core 8 Web API project in

Visual Studio 2022, follow the steps laid out below. Launch the Visual

Studio 2022 IDE. Click on” Develop brand-new task.” In the” Produce brand-new job”window, select”ASP.NET Core Web API

  1. “from the list of design templates showed. Click Next. In the”
  2. Configure your brand-new job” window, define the name and place for the new job. Additionally examine the”Place solution and job
  3. in the very same directory” check box, depending on your choices. Click Next. In the “Additional Details”window shown next, select”. NET 8.0(Long Term Support )”as the structure
  4. version and ensure
  5. that the “Usage controllers” box is checked. We will be using controllers in this project. Somewhere else in the “Additional Information” window, leave the “Authentication Type”set to”None” (the default)
  6. and make sure the check boxes” Enable Open API Support,”” Configure for HTTPS, “and “Enable Docker “remain untreated. We will not be using any of those functions here. Click Develop. We’ll utilize this ASP.NET Core Web API project to work with the REPR style pattern
  7. in the sections below.Components of the

REPR style pattern As the name recommends, the REPR style pattern consists of 3 parts: Demand: This represents the

input data that the endpoint anticipates. These demand items ought to be used for input recognition and passing data between the layers of the application. Endpoint: This represents the logic carried out by the endpoint for an offered demand. Action: This represents the output data that the endpoint returns. Create a request object The following code

  • bit illustrates a typical demand class. public class CreateUserRequest li>
  • public int UserId public string Username ol>

    . get; set; public string Password public string Email get; set; Likewise, a request class for producing a new product would look like this: public class CreateProductRequest public int Id get; set; public string ProductName get; set;

    public string Category get; set; public string Description get; set; public decimal Amount public decimal Price get; set; And a demand class for obtaining product data would look like this: public class GetProductRequest public int Id get; set; public string ProductName get; set;

    public string Description get; set; public decimal Price Carry out the logic at the endpoint Refer to the Controllers folder in the task you developed earlier. Right-click on the Controllers folder and create a new API controller called CreateProductController. Change the default produced code with the following code.using Microsoft.AspNetCore.Mvc; namespace REPR_Example. ProductAPI.Endpoints.Product.CreateProduct [controller])] [ApiController] public class CreateProductController: ControllerBase p>. [HttpPost(Name= “GetProducts”)] [ProducesResponseType( StatusCodes.Status204NoContent )] public ActionResult GetProductsAsync(GetProductRequest getProductRequest ) Compose your code here to retrieve items. return NoContent ();. [HttpPost(Name=”CreateProduct ” )] [ProducesResponseType(StatusCodes.Status204NoContent)]. public ActionResult CreateProductAsync (CreateProductRequest createProductRequest ). Now, consider the following HTTP GET endpoint. https://localhost:4586/api/getproducts!.?.!When this endpoint is hit, the typical action in JSON will look like this: .”items”: [“id”: 1,”name “:”HP ZBook Laptop “,”description”:” i9 Laptop computer with 32 GB RAM and 1 TB SSD “, ” rate”: 2500, “id”: 2,”name “:”Lenovo Thinkpad Laptop Computer”,”description”

    :”i9 Laptop computer with 16 GB RAM and 1 TB SSD “,” price”: 1500] Produce the response things Lastly, the following code bit illustrates a

    common action class.public class CreateProductResponse. It needs to be noted here that not all endpoints will need input information for the request or action classes. In most cases, you might require just to send the HTTP status code in the response.REPR pattern use cases A typical use case of the REPR pattern is

    CQRS(command and question duty partition), where you require to have a different endpoint for each command and question. The vertical piece architecture is yet another use case of the REPR pattern, where the application is divided into vertical layers depending on their responsibilities.That stated, you are not constrained to use REPR to construct a particular design of architecture. You might define Relaxing resources and even RPC-style endpoints utilizing the REPR design pattern. You can decide to choose this pattern based on your application’s requirements.The REPR style pattern helps enhance the readability, maintainability, and scalability of your code base by isolating the different layers of your application, such as the interface, business logic, and the data access layers. You may modify and expand the REPR pattern according to your requirements by integrating extra layers or parts. Copyright © 2024 IDG Communications, Inc. Source

  • Leave a Reply

    Your email address will not be published. Required fields are marked *