作者: Bruno Sonnino
译文: C# 中使用 Bogus 创建模拟数据
译者: Lamond Lu
背景
在我每次写技术类博文的时候, 经常做的一件事就是创建模拟数据. 在每篇博文中, 为了解释某些概念, 我需要创建许多模拟数据. 这是一个我在实际中遇到的问题, 因为我需要为我的程序找到一些合适的数据. 有些时候, 我会从数据库中找一些数据 (Northwind 和 AdventureWorks 都是我的好朋友 ^.^), 有些时候, 我会使用一些现成的 JSON 或者 xml 数据, 当然有时候我只能自己手动创建一些数据.
当然以上方案都不完美, 也都不稳定, 所以每次我都需要探索一些新方式来获取数据 (对于学习来说这很好, 但是维护起来确是一种灾难).
最后我找到了 Bogus, 一个基于 C# 的简单数据生成器.
使用 Bogus 生成模拟数据, 你只需要定义规则并生成数据即可, 就是这么简单. 而且 Bogus 可以生成固定数据或者变化数据. 这样一旦你拿到了这些数据, 你就可以把它们序列化成你想要的格式: JSON, xml, 数据库或者文本文件.
生成模拟数据
为了生成模拟数据, 我们首先需要针对模拟数据创建对应的实体类. 这里我们可以创建一个命令行程序, 并添加一下两个类.
- 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; }
- }
在你创建好以上两个实体类之后, 你就可以来添加仓储来获取模拟数据了. 为了使用 Bogus, 你可以使用 Nuget 将 Bogus 库添加到你的项目中.
Install-Package Bogus
下面我们就可以来添加一个仓储类来获取模拟数据了. 这里我们添加一个 SampleCustomerRepository 类, 并加入以下方法.
- 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);
- }
这里的第三行代码, 我们为 Randomizer.Seed 属性指定一个固定的随机种子, 因此每次生成的数据都是一样的. 如果你不希望每次都生成固定的数据, 你可以去掉这行代码.
这里我们为订单和客户数据的生成定义了规则, 然后我们调用了 Generate 方法来生成模拟数据. 就是这么简单.
如上所见, Bogus 提供了许多类来生成数据. 例如 Company 类可以用来生成公司模拟数据, 例如公司名称. 你可以使用这些生成的数据作为你程序的模拟数据, 这些数据有 3 种使用场景
单元测试的模拟测试数据
设计阶段的模拟数据
原型的模拟数据
但是我确信, 你能发现更多的使用场景.
这里为了使用这些数据, 你可以在 Main 方法中加入以下代码
- static void Main(string[] args)
- {
- var repository = new SampleCustomerRepository();
- var customers = repository.GetCustomers();
- Console.WriteLine(JsonConvert.SerializeObject(customers,
- Formatting.Indented));
- }
这里我们将模拟数据转换成了 JSON 字符串, 所以这里你需要添加对 Newtonsoft.JSON 库的引用. 当你运行程序之后, 你会得要以下结果.
如上所见, 程序生成了一个顾客的数据集, 并附带了每个顾客的所有订单信息.
来源: https://www.cnblogs.com/lwqlun/p/10785239.html