Introduction
Every software application needs data, and once in production, the data will flow in from the users, but during development, you'd need to rely on yourself to add random data records to your database or an in-memory structure to see what's the best way to view the data, how's your code performing with small and large data sets. So data is essential during development, and manually added data can create a little of bit of overhead that is to disregard the time consumed by doing this task.
In this post, I'll teach you how you can get unlimited mock data for your that you can read into you applications with only couple lines of code.
Table Of Content
1: Designing the Data Model
2: Customizing the Mock Data
3: Prepping Up the Data File
4: Reading the File From the Application
Designing the Data Model
Picture this:
You're building an employees management system, and you need to visualize how the data should look like on the client, or you need to have like multiple employee records in your Employees database table OR document. One way is to build a form that you can use to insert some random data and see how that plays out, but that's slow and redundant. Let's see how we can fix this.
Start by creating an Employee
class, and add to it the properties you want, for me, I'll be using the following model:
public class Employee
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? EmailAddress { get; set; }
public decimal Salary { get; set; }
public byte Age { get; set; }
}
That's good enough for the purpose of this post, now that our data model is ready, we can move to the next step.
Customizing the Mock Data
To generate the mock data, we'll use a website called Mockaroo, this website allows us to generate mock data for free, and in various formats with lots of options to customize the shape of the data to fit our desired outcome.
So head to Mockaroo, and configure the data model fields to match that of the class you created earlier, for me, it looks like this:
💡 Note: Make sure to change the format to JSON before you download the mock data file.
Once you're done, click generate data and the file will be downloaded for you.
Prepping Up the Data File
Once the file is downloaded, you need to copy it to the root directory of your project, the project type doesn't matter, for simplicity, I'll use a console application but don't worry, this also works with other projects and even with other programming languages if you figure out a way to read the data into the app.
When you copy the file, if you're using Visual Studio for Windows or Mac, then there's an option you get by right clicking the file that says Copy to Output Directory, however, if you're using Visual Studio Code, you can do this by editing the .csproj
file directly, to do this, add the following code to the project file:
<ItemGroup>
<None Update="employees.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
💡 NOTE: I renamed the mock data file to employees.json for clarity, I think it's a better practice than to use the default name of the file.
Ok and that's the data file all set, we can now read its content into the application.
Reading the File From the Application
To read the JSON file, we first have to read its content into a string, then deserialize this string into a strongly-typed C# collection of objects, to do that, write the following code:
var jsonString = File.ReadAllText("employees.json");
var employees = JsonSerializer.Deserialize<List<Employee>>(jsonString);
The final code should look something like this:
And there you have it, 1,000 model objects ready to be used in your application, you can either use them in-memory like in the previous case, or seed them to a database if you like.
That's about it for this post, I hope you enjoyed it and learned something new along the way!