avatarFuji Nguyen

Summary

The undefined website details the release of OnionAPI version 1.5, a Visual Studio template designed to quickly scaffold a clean architecture REST API project in C#, targeting .NET 6 and Visual Studio 2022, and includes advanced features such as pagination, filtering, sorting, and CRUD operations.

Abstract

The undefined website introduces the OnionAPI template version 1.5, which is a significant update to support .NET 6 and Visual Studio 2022. This template facilitates the development of REST API projects by providing a structured solution based on Clean Architecture principles. It includes five pre-configured projects that cover domain entities, application interfaces and behaviors, data access with Entity Framework, shared infrastructure services, and the WebAPI layer for exposing endpoints. The template incorporates design patterns such as Repository and CQRS, and integrates tools like Swashbuckle for API documentation and Bogus for generating mock data. The website outlines the prerequisites for using the template, provides a step-by-step tutorial for installing and running the template, and demonstrates how to utilize the template's features such as filtering, sorting, paging, and data shaping through Swagger UI. The source code for a sample project is available on GitHub, and the article concludes by emphasizing the template's benefits in improving development efficiency and consistency.

Opinions

  • Fuji Nguyen, the author of OnionAPI Template, believes that the template can save at least 40 hours of coding time compared to starting a REST API project from scratch.
  • The template is praised for its ability to provide a loosely-coupled and inverted-dependency architecture, adhering to good design patterns and practices.
  • The author suggests that using the OnionAPI template can enhance scalability, maintainability, and performance of REST API projects.
  • The article promotes the OnionAPI template as a means to increase consistency and decrease delivery time in software development processes.
  • The author encourages readers to try out the AI service recommended in the article, positioning it as a cost-effective alternative to ChatGPT Plus with similar capabilities.

.NET 6 Upgrade of OnionAPI — a Visual Studio template to scaffold a clean architecture REST API C# project

Photo by Bewakoof.com Official on Unsplash

REST API C# template for Visual Studio 2022/.NET 6

Introduction

I am happy to announce the release of OnionAPI version 1.5 to support Visual Studio 2022 and .NET 6. This story provides a tutorial on installing and running the template to scaffold an ASP.NET WebAPI project in minutes. The template includes advanced features such as pagination, filter, sorting, and CRUD. The solution is structured based on Clean Architecture, with a layering approach for scalability and maintainability.

OnionAPI template is a recipe for building advanced REST API. The template can save at least 40 hours of coding time in comparision to starting the REST API project from scratch. — Fuji Nguyen, Author of OnionAPI Template

What is a Visual Studio template?

A Visual Studio template provides the files that are required for a particular project type, including standard assembly references, set default project properties, and compiler options. A number of predefined project templates are installed with Visual Studio. These templates, such as the ASP.NET Web Application and Class Library templates, are available to choose from when you create a new project.

What is the OnionAPI template?

Developers can use the Visual Studio template OnionAPI to scaffold a clean architecture (onion view, see Figure 1) ASP.NET Core REST API project.

Once you provide the project name, the template will create five (5) projects inside the Visual Studio solution. Each project is prefixed with the project name and has proper project namespaces in the source code. The five (5) projects are:

  1. Domain — entities and the common models
  2. Application — Interfaces, CQRS Features, Exceptions, Behaviors
  3. Infrastructure.Persistence — data access API based on Entity Framework
  4. Infrastructure.Shared — for common services such as Mail Service, Date Time Service, Mock, and so on
  5. WebApi — for API controllers to expose REST API resources and endpoints
Figure 1- Clean Architecture Onion View

Figure 2 contains a screenshot of the source code scaffolded by the OnionAPI template for the sample project MyOnionApi. The screenshot depicts the organization of 5 projects, grouped into 3 folders Core, Infrastructure, and Presentation, which are corresponding to the layers described in the Clean Architecture pattern.

Figure 2— Scaffolded source code organization within the Visual Studio solution

The solution tech stack provides loosely-coupled and inverted-dependency architecture with good design patterns and practices.

  1. ASP.NET CORE 6— a framework for creating RESTful services, also known as web APIs, using C#
  2. Repository Pattern — abstraction layer between the data access layer and the controller
  3. CQRS (Command and Query Responsibility Segregation) Pattern — separating read and update operations for a data store to maximize performance, scalability, and security
  4. Entity Framework Core — a lightweight, extensible, open-source, and cross-platform version of the popular Entity Framework data access technology
  5. Swashbuckle — Seamlessly adds a Swagger to WebApi projects! Combines ApiExplorer and Swagger/swagger-ui to provide a rich discovery, documentation, and playground experience to your API consumers
  6. Bogus — providing realistic, easy to use mock data .NET library for rapid REST API design and testing

Prerequisites

  1. Latest Net Core 6 SDK
  2. Visual Studio 2022 Community — free code editor for C#Tutorial Content

Tutorial Content

The tutorial contains three parts:

Part 1 — Download and install OnionAPI template from Visual Studio Marketplace

Part 2 — Scaffold new API project using OnionAPI tempalte

Part 3 — Try Out Filtering, Sorting, Paging, and Shape features

Part 1 — Download and Install OnionAPI Template

You can download the OnionAPI template from the Visual Studio Marketplace as shown in Figure 3. At the time of this writing, the template was downloaded 975 times by developers.

After download, click on the VSIXTemplateOnionAPI.vsix to install the extension. If you are new to Visual Studio Extension, visit Manage extensions for Visual Studio for installation instructions.

Figure 3— OnionAPI template available for free download from Visual Studio Marketplace

Part 2 — Scaffold a New REST API Project with OnionAPI Template

The easiest way to create a new project is to start from a project template for a particular type of application or website. A project template consists of a basic set of pre-generated code files, config files, assets, and settings.

When you first open Visual Studio, the start window appears and from there you can choose to create a new project as shown in Figure 4.

Figure 4— Option to create a new project in Visual Studio 2022

If the Visual Studio development environment is already open, you can create a new project by choosing File > New > Project on the menu bar, as shown in Figure 5, or by clicking the New Project button on the toolbar.

Figure 5— New Project option on menu bar

Task 1 — Select a Template Type

On the Create a new project page, a list of your recently selected templates will appear on the left. The templates are sorted by most recently used.

If you’re not selecting from the recently used templates, you can filter all available project templates by entering search text into the search box to further filter the templates; for example, “OnionAPI”.

Task 2 — Create a Solution

On the Create a new project screen, search for and select the OnionAPI template and click on the Next button. See Figure 6 for visual aid.

Figure 6 — Search and select the OnionAPI template in Visual Studio

Enter the project name (ex. MyOnionAPI) and click on the Create button. See Figure 7 for visual aid.

Figure 7 — Configure your new project screen in Visual Studio

Hit F5 to run. The swagger should show in the browser as shown in Figure 8.

Figure 8 — Swagger showing the WebAPI

Part 3— Try Out Filtering, Sorting, Paging and Shaping Features

The OnionAPI template creates two REST resources:

  1. Employees — This is a mock REST API resource where the data is dynamically generated by Bogus library at run time. To learn more about Bogus, check the link in the Related Stories section later on.
  2. Positions — This REST API resource gets its data from the actual database table Positions. This table is automatically seeded with 1,000 rows of fake data when the project runs the first time.

To try out REST API in Swagger (see Figure 9 for visual aid)

  1. Click on Employees > GET to expand the screen
  2. Click on Try it out
  3. Enter parameters (can be left blank for default values)
  4. Click on the Execute button
  5. Review the JSON output
Figure 9 — Example of search criteria

Figure 10 is an example screenshot of the JSON output. For performance, the server-side paging is used. Notice that the response body includes “wrapper” with friendly data for the frontend app to consume.

  1. pageNumber — pagination index such as 1, 2, 3, etc. The default is page 1.
  2. pageSize — number of records per page. The default is 10.
  3. recordsFiltered — number of records found matching the search criteria. In this example, the search criteria is “Chief”.
  4. recordsTotal — total records number of employees in the database.
  5. succeeded — boolean true/false indicating status of webapi response
  6. message — friendly error message if any
  7. errors — system error if any
  8. data — response of data in JSON
Figure 10 — Example of JSON output including the wrapper showing the pageNumber, pageSize, etc.

Notice that the data response consists of eleven (11) columns. Suppose you want to shape the response with fewer columns to minimize the data sent over the network. For example, the frontend app only needs three columns: firstName, lastName, and employeeTitle. You can input the column names in the Fields as shown in Figure 11 and the JSON response will contain only three columns in the “Data” section as shown in Figure 12. Check out the References' section for my tutorials on Pagination.

Figure 11 — Specify the columns to return to in the Fields parameter
Figure 12 — Result of JSON output with the fewer columns when specifying the column names in the Fields parameter

The scaffolded source code also has the data modification example code for the Positions resource. As shown in Figure 13, you find methods such as POST, GET, PUT, and DELETE to support CRUD (create, read, update and delete). Check out the References' section for my tutorials on CRUD.

Figure 13 — The REST methods GET, POST, PUT and DELETE to support CRUD

Source Code

For reference purposes, you can find the scaffolded source code project at workcontrolgit/MyOnionApi (github.com). You can fork and run it with Visual Studio 2022 without any modification.

References

Below are a few technical references on software patterns and my related blogs on pagination, CRUD, etc.

  1. Common web application architectures — Microsoft publication on common application patterns. Check out the Clean Architecture section.
  2. CAT architecture pattern for modern app SPA/Mobile — my blog on the modern app architecture based on separation of services.
  3. Bogus Library for Fake Data In ASP.NET Core WebAPI — my blog on using Bogus library in the Web API project.
  4. NetCore REST API/Dapper/SQLKata with VS Template KissApi — if you prefer Dapper over EntityFramework, check out the KissAPI template.
  5. NetCore 5 Pagination 100,000+ Rows — my blog tutorial on implementing pagination using Angular as SPA and .NET CORE as backend WebAPI.
  6. Angular 11 CRUD with .Net 5 REST API Tutorial — my blog tutorial on implementing CRUD using Angular as SPA and .NET CORE as backend WebAPI.

Summary

When managing the development, many organizations are struggling with governance and look for value-added process improvement tools. The OnionAPI template increases consistency while decreasing delivery time.

The OnionAPI provides boilerplate code to create advanced REST API features such as data filtering, sorting, shaping, paging, and CRUD. These features are written as the base classes, so you can inherit and extend them to meet your project-specific requirements.

Let’s peel the onion!

Net6
Web Development
Technology
Webapi
Recommended from ReadMedium