How to use Bogus to create simulated data in C#
Bogus, a simple data generator based on C#. To use Bogus to generate simulation data, you only need to define the rules and generate data, it's that simple. And Bogus can generate fixed data or changing data. So once you get the data, you can serialize it into the format you want: json, xml, database or text file.
Generate simulation data
In order to generate simulation data, we first need to create the corresponding entity class for the simulation data. Here we can create a command line program and add two classes.
public class Customer { public Guid Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string City { get; set; } public string Country { get; set; } public string ZipCode { get; set; } public string Phone { get; set; } public string Email { get; set; } public string ContactName { get; set; } public IEnumerable<Order> Orders { get; set; } }
public class Order { public Guid Id { get; set; } public DateTime Date { get; set; } public Decimal OrderValue { get; set; } public bool Shipped { get; set; } }
After you create the above two entity classes, you can add a warehouse to obtain simulated data. In order to use Bogus, you can use Nuget to add the Bogus library to your project.
Install-Package Bogus
Related tutorials: C# Video tutorial
Next we can add a warehousing class. Get simulation data. Here we add a SampleCustomerRepository
class and add the following methods.
public IEnumerable<Customer> GetCustomers() { Randomizer.Seed = new Random(123456); var ordergenerator = new Faker<Order>() .RuleFor(o => o.Id, Guid.NewGuid) .RuleFor(o => o.Date, f => f.Date.Past(3)) .RuleFor(o => o.OrderValue, f => f.Finance.Amount(0, 10000)) .RuleFor(o => o.Shipped, f => f.Random.Bool(0.9f)); var customerGenerator = new Faker<Customer>() .RuleFor(c => c.Id, Guid.NewGuid()) .RuleFor(c => c.Name, f => f.Company.CompanyName()) .RuleFor(c => c.Address, f => f.Address.FullAddress()) .RuleFor(c => c.City, f => f.Address.City()) .RuleFor(c => c.Country, f => f.Address.Country()) .RuleFor(c => c.ZipCode, f => f.Address.ZipCode()) .RuleFor(c => c.Phone, f => f.Phone.PhoneNumber()) .RuleFor(c => c.Email, f => f.Internet.Email()) .RuleFor(c => c.ContactName, (f, c) => f.Name.FullName()) .RuleFor(c => c.Orders, f => ordergenerator.Generate(f.Random.Number(10)).ToList()); return customerGenerator.Generate(100); }
In the third line of code here, we specify a fixed random seed for the
Randomizer.Seed
property, so the data generated is the same every time. If you don't want to generate fixed data every time, you can remove this line of code.
Here we define rules for the generation of order and customer data, and then we call the Generate
method to generate simulation data. It's that simple.
As you can see above, Bogus provides many classes to generate data. For example, the Company
class can be used to generate company simulation data, such as company name. You can use these generated data as simulation data for your program. These data have three usage scenarios
- Simulation test data for unit testing
- Simulation data for the design phase
- Simulation data of prototype
But I am sure that you can find more usage scenarios.
In order to use these data here, you can add the following code to the Main
method
static void Main(string[] args) { var repository = new SampleCustomerRepository(); var customers = repository.GetCustomers(); Console.WriteLine(JsonConvert.SerializeObject(customers, Formatting.Indented)); }
Here we convert the simulation data into a Json string, so here you need Add a reference to the Newtonsoft.Json
library. After you run the program, you will get the following results.
#As you can see above, the program generates a customer data set with all order information for each customer.
The above is the detailed content of How to use Bogus to create simulated data in C#. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

Guide to Web Services in C#. Here we discuss an introduction to Web Services in C# with technology use, limitation, and examples.

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.
