C#中使用Bogus建立模擬數據

原文:CREATING SAMPLE DATA FOR C#
做者:Bruno Sonnino
譯文:C#中使用Bogus建立模擬數據
譯者: Lamond Lu數據庫

背景

在我每次寫技術類博文的時候,常常作的一件事就是建立模擬數據。在每篇博文中,爲了解釋某些概念,我須要建立許多模擬數據。這是一個我在實際中遇到的問題,由於我須要爲個人程序找到一些合適的數據。有些時候,我會從數據庫中找一些數據(Northwind和AdventureWorks都是個人好朋友^.^), 有些時候,我會使用一些現成的Json或者Xml數據,固然有時候我只能本身手動建立一些數據。json

固然以上方案都不完美,也都不穩定,因此每次我都須要探索一些新方式來獲取數據(對於學習來講這很好,可是維護起來確是一種災難)。
最後我找到了Bogus, 一個基於C#的簡單數據生成器。c#

使用Bogus生成模擬數據, 你只須要定義規則並生成數據便可,就是這麼簡單。並且Bogus能夠生成固定數據或者變化數據。這樣一旦你拿到了這些數據,你就能夠把它們序列化成你想要的格式: json, xml,數據庫或者文本文件。dom

生成模擬數據

爲了生成模擬數據,咱們首先須要針對模擬數據建立對應的實體類。這裏咱們能夠建立一個命令行程序,並添加一下兩個類。單元測試

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類,並加入如下方法。ui

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庫的引用。當你運行程序以後,你會得要如下結果。

如上所見,程序生成了一個顧客的數據集,並附帶了每一個顧客的全部訂單信息。

相關文章
相關標籤/搜索