avatarFuji Nguyen

Summary

The undefined website presents the OnionAPI Visual Studio template, which enables developers to quickly scaffold a clean architecture ASP.NET Core REST API project with advanced features like CRUD operations, data filtering, sorting, paging, and shaping.

Abstract

The undefined website content introduces the OnionAPI template as a solution for developers seeking to create a REST API with ASP.NET Core that adheres to clean architecture principles. The template, available for download from the Visual Studio Marketplace, facilitates the generation of a project with five pre-configured projects within a solution, each serving a specific purpose in the architecture. It incorporates design patterns such as Repository and CQRS, and utilizes Entity Framework Core for data access. The template also includes Swashbuckle for API documentation and Bogus for generating mock data. The article guides users through downloading, installing, and using the template to create a new REST API project, emphasizing its ability to streamline the development process by providing boilerplate code for sophisticated features.

Opinions

  • The author expresses a personal need for a template that combines clean architecture with advanced REST API features, which led to the creation of OnionAPI.
  • The author acknowledges the contributions of other developers and sources, such as Mukesh Murugan's Clean Architecture WebAPI template and CodeMaze's data shaping techniques, indicating a collaborative approach to tool development.
  • The article suggests that the OnionAPI template can enhance governance and consistency in API development while reducing delivery times, reflecting the author's belief in its value for organizational process improvement.
  • The author seems to value the ease of use and rapid prototyping capabilities of the

NetCore REST API CRUD, Filter, Sort, Page, and Shape Data with VS Template OnionAPI

Photo by Simona Sergi on Unsplash

I have been searching for a Visual Studio template to generate a Clean Architecture ASP.NET Core REST API solution with boilerplate code for advanced features such as data CRUD, filtering, sorting, paging, and shaping using EntityFramework. Unable to find one, I cooked up the OnionAPI template and served it up for free download in the Visual Studio marketplace.

In this story, I will go over the steps to download/install the OnionAPI template into Visual Studio 2019 and then use the template to scaffold a new REST API project. You will find a complimentary Youtube video and Github source code for download at the end of this story.

Let’s get started.

Credit Where Credit Is Due

To develop this Visual Studio template, I have reviewed and integrated tools and techniques from the following sources

  1. ASP.NET Core WebAPI Clean Architecture template from Mukesh Murugan
  2. Data Shaping in ASP.NET Core Web API from CodeMaze

Overview of OnionAPI template

Developers can use the Visual Studio template OnionAPI to scaffold a clean architecture (onion view) ASP.NET Core REST API project. Now you know where the name OnionAPI coming from (:

You provide the project name and the template will create five (5) projects inside the solution. Each project is prefixed with the project name and has proper project namespaces in the source code.

  1. Domain — for entities and the common models
  2. Application —for Interfaces, CQRS Features, Exceptions, Behaviors
  3. Infrastructure.Persistence — for application-specific database access
  4. Infrastructure.Shared — for common services such as Mail Service, Date Time Service, Mock, and so on
  5. WebApi — for controllers to expose REST API resources and endpoints

The below screenshot depicts the organization of 5 projects in the Visual Studio solution after the template generated source code for the sample project name TalentManagement.

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

  1. ASP.NET CORE 5 — 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

Download OnionAPI Template

You can download the OnionAPI template from the Visual Studio Marketplace.

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.

Generate 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.

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

Task 1 — Select a Template Type

On the Create a new project page, a list of your recently selected templates appears 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

Enter the project name (ex. TalentManagement) and click on the Create button

Right-mouse click on TalentManagement.WebApi and Set as Startup Project

Hit F5 to run. The swagger should show in the browser

Task 2 — Try Out Filtering, Sorting, Shaping, and Paging 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 first run.

To try out REST API in Swagger

  1. Click on Employees > GET to expand the screen
  2. Click on Try it out
  3. Enter parameters (can leave blank to use default values)
  4. Click on the Execute button
  5. Review the JSON output

Youtube Video

Related Stories

Check my other stories related to REST API and Angular development

  1. DevKit API Security — IdentityServer4 with Admin UI, ASP.NET WebAPI, and Angular Tutorial — a multipart tutorial for implementing Token Service to secure Clients and API Resources using OIDC/oAuth2/JWT
  2. Rapid Prototype of ASP.NET Core WebAPI with Bogus — a deep dive into using Bogus libraries to design and simulate REST endpoints
  3. Securing Angular 11 App with Code Flow PKCE and IdentityServer4 — a tutorial on scaffolding an enterprise-level Angular app using starter kit Ngx-Rocket and making secure REST API calls using a JWT Access Token
  4. Rapid Prototype Asp.Net Core REST API using KissApi Template — similar to OnionAPI, this template can be used to scaffold ASP.NET REST API project. However, instead of using EntityFramwork Core for data access, it is scaffold code based on micro ORM Dapper and query builder SQLKata.
  5. CAT architecture pattern for modern app SPA/Mobile — the modern approach to build scalable and maintainable applications.

Source Code

You can download the scaffolded TalentManagement Visual Studio solution from https://github.com/workcontrolgit/TalentManagement

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 decreases delivery times.

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!

Webapi
Entity Framework Core
Recommended from ReadMedium