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.
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
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!