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


原文:CREATING SAMPLE DATA FOR C#
作者: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庫的引用。當你運行程序之后,你會得要以下結果。

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM