avatarFuji Nguyen

Summary

The web content describes a method for rapidly prototyping ASP.NET Core WebAPI projects using the Swashbuckle.AspNetCore and Bogus libraries to simulate REST endpoints and generate fake data for testing and design validation before backend development.

Abstract

The article outlines an approach for developers to quickly prototype ASP.NET Core WebAPI projects by leveraging the Swashbuckle.AspNetCore library for API documentation and interactive testing, and the Bogus library for creating realistic fake data. This process enables teams to validate software design and gather customer feedback early in the development cycle without the need for a fully developed backend database. The tutorial includes a use case scenario for an Incident Reporting System, demonstrating how to generate and shape fake data for REST API endpoints using Clean Architecture principles. It also provides guidance on setting up the necessary classes and interfaces, registering services, and testing the API using Swagger UI. The author emphasizes the benefits of this prototyping method for agile development teams and includes FAQs, related tutorials, and a link to the source code for further exploration.

Opinions

  • The author suggests that using Swashbuckle.AspNetCore and Bogus libraries can significantly streamline the process of prototyping WebAPI projects.
  • Brian Chavez, the creator of Bogus, is quoted expressing the need for a robust fake data generator in the .NET ecosystem, which led to the creation of Bogus.
  • The article conveys that rapid prototyping with fake data allows for early design validation and customer feedback, potentially minimizing rework.
  • The author recommends specific Visual Studio templates, KissAPI and OnionAPI, for scaffolding clean architecture solutions for prototyping.
  • The author provides a walkthrough of the process, from cloning the project to testing the API in Swagger, suggesting that this approach is user-friendly and beneficial for agile development teams.
  • The inclusion of a screencast and related tutorials indicates the author's commitment to providing comprehensive learning resources for developers.
  • The author encourages appreciation for the Bogus library by starring its Github repository, indicating a positive opinion of the library's utility and community support.

Bogus Library for Fake Data In ASP.NET Core WebAPI

Photo by Amos Bar-Zeev on Unsplash

If you look for an easy way to rapidly prototype ASP.NET WebAPI projects without involving the heavy lifting of the backend database, this story is for you. By using a combination of Swashbuckle.AspNetCore and Bogus libraries, you can easily construct and simulate REST endpoints. This allows for validation of the software design prior to the actual build and minimizing rework.

Software Design Challenge

When buying a new car, we want to test drive before purchase. When creating a new WebAPI project, how can developers try out its functionalities? For examples, typical coding questions are

  1. What are the filter, paging, sorting, and shaping parameters?
  2. How to pass those parameters to the WebAPI? Via Header, Query or URL path?
  3. What are the fields names and data types in the JSON payload?

It really comes down to the interface design between the frontend, middleware API, and the backend. Two libraries Swashbuckle.AspNetCore and Bogus can help to expose WebAPI endpoints to developers early in the design phase.

Swashbuckle.AspNetCore Library

Swashbuckle provides an embedded version of the awesome swagger-ui that’s powered by the generated Swagger JSON. It also generates beautiful API documentation, including a UI to explore and test operations, directly from your routes, controllers, and models.

Bogus Library

Bogus is a simple and sane fake data generator for .NET languages like C#, F#, and VB.NET. Bogus is fundamentally a C# port of faker.js and inspired by FluentValidation’s syntax sugar. Bogus will help you load databases, UI, and apps with fake data for your prototyping and testing needs.

For this story, I reached out to Brian Chavez, the creator of Bogus library for his comments. See below for Brian’s response. Bless his heart!

I created Bogus after seeing a demo on faker.js; I immediately thought we needed an equivalent for the .NET ecosystem. I saw how powerful realistic fake data could be for testing software. At the time, the .NET ecosystem didn’t have a reliable or well-maintained fake data generator with the features I was looking for. And so Bogus was born. Now, we have a great community of users and contributors all over the world. Bogus would not be the success it is today without its users or contributors.

— Brian Chavez

You can show Brian your appreciation by “star” his Bogus repo on Github.

Use Case Scenario

Your team is working on an Incident Reporting System (IRS) application to report adverse events and near misses. The tech stack of the application is Angular SPA and ASP.NET WebAPI. The development team consists of product owner, business analyst, frontend Angular developer, ASP.NET middleware developers, and DBA. Being an agile development team, your team wants to do a rapid prototype and get quick feedback from customers. Your team is in the requirement gathering phase and the team wants to simulate the REST GET end-point that has 5,000 rows of incident records. The team will then review/confirm with customers data fields and data types in the record layout. The goal is to make the REST GET endpoint accessible to Angular clients and to provide fake data that looks real without building out the backend DB (yet). The first cut of the nested JSON output design is in the listing below.

REST API Prototype

For the rapid prototyping ASP.NET Core WebAPI projects, you can use either template

  1. KissAPI — Visual Studio template to generate a Clean Architecture solution based on micro ORM Dapper and SQL builder SQLKata for data access
  2. OnionAPI — Visual Studio template to generate a Clean Architecture solution based on EntityFrameworkCore for data access

For the purpose of exploring Bogus library, I used the KissAPI template to generate the project source code and checked into Github. If you want to learn how to scaffold clean architecture solutions, check out my two stories Rapid Prototype Asp.Net Core REST API with KissApi Template and Filter, Sort, Page, and Shape Data in Asp.Net Core REST API with OnionAPI Template.

Task 1 — Clone the project from Github

The source code project is available on Github. The solution consists of four projects that are structured following the Clean Architecture design pattern. Follow the steps below to download and open source code

  1. Clone the repo https://github.com/workcontrolgit/ProofOfConcept
  2. Open the solution with Visual Studio 2019
  3. Set ProofOfConcept.WebApi as the StartUp project
Screenshot of the solution with four projects structured according to Clean Architecture design pattern

Note: If you are interested to see the source code before and after, check out the two branches TutorialStart TutorialEnd.

Task 2 — Add entity classes

In the Clean Architecture, the entities should be placed inside the Core project. Follow the screenshots below to add

  1. Contact entity class: the model of the person who reports the incident
  2. Incident entity class: the model of the Incident record
Screenshot of the Contact.cs class in the Core project

In Contact.cs, line 5 to 11 set up the Contact entity class

Screenshot of the Incident.cs in the Core project

In Incident.cs, lines 6 to 11 set up the Incident entity class

Task 2 — Add the interface class

In the Clean Architecture, the interfaces should be placed inside the Application project. Follow the screenshots below to

  1. Add IIncidentReportRepository class
  2. Update IUnitOfWork class to include IIncidentReportRepository class
Screenshot of the IIncidentReportRepository.cs in the Application project

In IIncidentReportRepository.cs, line 5 inherits from the IGenericRepository class

Screenshot of the IUnitOfWork.cs in the Application project

In the IUnitOfWork.cs, line 8 sets up the IIncidentReportRepository class

Task 3 — Add the repository class

In the Clean Architecture pattern, the repository classes should be placed inside the Infrastructure project. Follow the screenshots below to

  1. Add IncidentReportRepository class
  2. Update UnitOfWork class to include IncidentReportRepository class
  3. Register IIncidentReportRepository class in the Services startup (dependency inject support)
Screenshot of the IncidentRepository.cs class in the Infrastructure project

In the IncidentRepository.cs

  1. Line 17 — instruct Bogus library to generate 100 rows of fake data for all rows selection
  2. Line 27 — instructs Bogus library to generate 100 rows of fake data for pagination support
  3. Line 56–61 sets up the fake data for the Contact entity class, using Bogus API Name and Internet options. For more information on the faked data options, visit https://github.com/bchavez/Bogus
  4. Line 64–69 sets up fake data for IncidentReport using Bogus API Lorem and Date options. Notice line 68 references faker Contact entity class to generate ReportedBy data field.
Screenshot of the UnitOfWork.cs in the Infrastructure project

In UnitofWork.cs, lines 9, 13, and 18 set up IncidentReport class.

Screenshot of ServiceRegistration.cs in Infrastructure project

In ServiceRegistration.cs, line 19 registers IIncidentRepository and IncidentRepository classes.

Task 4— Add controller class

In the Clean Architecture pattern, the controller classes should be placed inside the WebAPI project. Follow the screenshots below to add the IncidentReportController class.

Screenshot of the IncidentsController class in the WebAPI project

In IncidentsController.cs

  1. lines 18–20: provide documentation of the endpoint in Swagger
  2. line 22: gets the query parameters via the RequestParameter class
  3. line 24: via Unit of Work class, passes in query parameters to retrieve

Task 5 — Test API in Swagger

The swagger is the UI for testing WebAPI. In Visual Studio, run the project and it will launch the browser. Follow the screenshots below to explore the Incidents GET endpoint.

Screenshot of the Swagger home page
Screenshot of the Incidents GET endpoint
Screenshot of the JSON output from Incidents > GET

Screencast

Below is a video recording of my computer screen (enjoy the background music) that shows how to

  1. Clone source code from Github
  2. Review clean architecture solution
  3. Code walk-thru of Bogus API to fake data
  4. Access mock Rest API via Swagger

FAQ

  1. Where is the Swagger registered? Open up the Startup.cs in the ProofOfConcept.WebAPI project and search for Swagger keyword. Be sure to review the code in both ConfigureServices and Configure methods.
  2. How to convert mock WebAPI into real WebAPI with connection to backend database? If following Clean Architecture, you just need to refactor the repository classes to point to the actual database. You can use SQLKata to build dynamic SQL. Review the Products WebAPI in the project for example codes to perform REST GET, PUT, POST, DELETE.

Check out related tutorials

  1. Rapid Prototype Asp.Net Core REST API using KissApi Template — use Visual Studio template to generate Clean Architecture solution based on Repository Pattern, Unit of Work, Dapper, SQLKata, and Swagger.
  2. 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

Git Repo

You can find the source code referenced in this story at https://github.com/workcontrolgit/ProofOfConcept

Screenshot of the source code repo in the Github

Summary

In this tutorial, you have learned how to use the Bogus library to simulate backend data. The Bogus library has many options to generate fake data that is resembled production data.

Fake it until you make it!

Aspnetcore
Bogus
Swashbuckle
Recommended from ReadMedium