Programmers are lazy. We love tools that automate our lives. Why not automate data?

A time-consuming but necessary part of a programmers job is seeding data into a database. Demonstrations, load performance, styling, testing, all need a way to generate data. A rudimentary way of doing this is by hand. Spending days, sometimes weeks, inputting data into a system until it represents a realistic go-live scenario.

Once you begin to realise doing it by hand is inefficient, you decide to create a seeding application that does it for you. One of the fundamental problems of seeding, and a general problem in computer science in general, is generating random data. You spend time understanding how to randomize dates, generate lorem ipsum text, create default images, and so on.

After a while, you again realise that building this seeding application is taking a long time. You realise that seeding must be a problem many other programmers face? You search the internet on a quest to make it easier. You find Bogus.

What is Bogus?

The quote Brian Chavez provides, aptly describes Bogus:

Bogus is a simple and sane fake data generator for .NET languages like C#, F# and VB.NET

Bogus helps create seeding applications. Bogus sorts out all the annoyances with randomising data, and allows you to generate classes again and again.

Bogus is based on faker.js, another framework that helps generate massive amounts of fake data in node.js and browsers.

Bogus provides many default types of data. As you expect, the simple data types in C# are all covered, such as datetime, bool, int, etc. Bogus also provides an API that is situational. For example, Bogus has APIs that allow you to generate data for Commerce scenarios, such as product names, category names, pricing, and so on.

Generating Blog Data

This blogging website uses Entity Framework. With Entity Framework, you have to create entities. Entities represent tables within your database, and are the interaction point for inserting data into it. The following example shows the entity used by this website for a blog post:

Entities in entity framework are plain old CLR objects (POCO), that is, very simple objects with no logic code or inheritance.

With this class, we will tell Bogus a few rules in order to generate a sample blog post:

On the first line, we have used the main feature of the Bogus framework: the faker. The faker is used to generate fake objects. Here we can see that we have define multiple rules for generating a blog post.

The rule syntax uses a fluent interface. A fluent interface is a way to chain method calls for configuration. It is classically used within the builder design pattern.

The first rule defines that the Id of the blog post should be the currnet index of a faker. When you generate a blog post, this index is incremented.

The second rule shows off lorem ipsum generation. You can specify if you want words, sentences, paragraphs, and you can also describe how many you want to generate. This is great for when you want to create some demonstration data for showcasing.

Another interesting rule is the last:

This is using another faker object to generate tags for the blog post. Here is the rest of the fakers:

This demonstrates that fakers can be chained, as all a faker is, is a way to generate data. So why not use fakers with other fakers to fake some data for your fake data? ;)

You can also create custom logic post-generation. As shown in the example, we are writing to the console on every generation of a blog post.

The final piece of the puzzle is to actually generate a blog post. With the new faker class finally configured, we can call generate:

We can then serialise this object into JSON and write it to the console:

With approximately 40 lines of code, I have created a blog post data generator. This is the true power of Bogus.

The generate method also accepts a number. You can create lists of blog posts at a time:

Or you can create lazily evaluated data with IEnumerable:

The limitations now with your seeding application. The hard part has been done for you, and now it should be a simple task of add this to entity framework’s dbcontext, and saving it:

Summary

Seeding is a necessary evil of our every day job. With Bogus, it becomes slightly easier. Whether you are wantings to populate an eCommerce site, a blog, or even something that has nothing to do with websites such as performance analysis.

Bogus is available as a .NET Standard 2.0 Nuget package. This means that you can use Bogus with virtually all flavours of .NET. You could use it with Xamarin to generate data on your mobile, or with desktop applications that require gigabytes of data. More importantly, you can create one seeding generator for your data, and share it throughout your entire application suite.

A fluent interface into a world of random data, Bogus is a mandatory tool for any programmer.

Thanks for reading, I hope this helps. The Bogus API is available on the readme here.