Bogus Library for Fake Data In ASP.NET Core WebAPI
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
- What are the filter, paging, sorting, and shaping parameters?
- How to pass those parameters to the WebAPI? Via Header, Query or URL path?
- 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.