C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
原文:CREATING SAMPLE DATA FOR C#
作者:Bruno Sonnino
譯文:C#中使用Bogus創(chuàng)建模擬數(shù)據(jù)
譯者: Lamond Lu
背景
在我每次寫(xiě)技術(shù)類博文的時(shí)候,經(jīng)常做的一件事就是創(chuàng)建模擬數(shù)據(jù)。在每篇博文中,為了解釋某些概念,我需要?jiǎng)?chuàng)建許多模擬數(shù)據(jù)。這是一個(gè)我在實(shí)際中遇到的問(wèn)題,因?yàn)槲倚枰獮槲业某绦蛘业揭恍┖线m的數(shù)據(jù)。有些時(shí)候,我會(huì)從數(shù)據(jù)庫(kù)中找一些數(shù)據(jù)(Northwind和AdventureWorks都是我的好朋友^.^), 有些時(shí)候,我會(huì)使用一些現(xiàn)成的Json或者Xml數(shù)據(jù),當(dāng)然有時(shí)候我只能自己手動(dòng)創(chuàng)建一些數(shù)據(jù)。
當(dāng)然以上方案都不完美,也都不穩(wěn)定,所以每次我都需要探索一些新方式來(lái)獲取數(shù)據(jù)(對(duì)于學(xué)習(xí)來(lái)說(shuō)這很好,但是維護(hù)起來(lái)確是一種災(zāi)難)。
最后我找到了Bogus, 一個(gè)基于C#的簡(jiǎn)單數(shù)據(jù)生成器。
使用Bogus生成模擬數(shù)據(jù), 你只需要定義規(guī)則并生成數(shù)據(jù)即可,就是這么簡(jiǎn)單。而且Bogus可以生成固定數(shù)據(jù)或者變化數(shù)據(jù)。這樣一旦你拿到了這些數(shù)據(jù),你就可以把它們序列化成你想要的格式: json, xml,數(shù)據(jù)庫(kù)或者文本文件。
生成模擬數(shù)據(jù)
為了生成模擬數(shù)據(jù),我們首先需要針對(duì)模擬數(shù)據(jù)創(chuàng)建對(duì)應(yīng)的實(shí)體類。這里我們可以創(chuàng)建一個(gè)命令行程序,并添加一下兩個(gè)類。
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; } }
在你創(chuàng)建好以上兩個(gè)實(shí)體類之后,你就可以來(lái)添加倉(cāng)儲(chǔ)來(lái)獲取模擬數(shù)據(jù)了。為了使用Bogus, 你可以使用Nuget將Bogus庫(kù)添加到你的項(xiàng)目中。
Install-Package Bogus
下面我們就可以來(lái)添加一個(gè)倉(cāng)儲(chǔ)類來(lái)獲取模擬數(shù)據(jù)了。這里我們添加一個(gè)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); }
這里的第三行代碼,我們?yōu)镽andomizer.Seed屬性指定一個(gè)固定的隨機(jī)種子,因此每次生成的數(shù)據(jù)都是一樣的。如果你不希望每次都生成固定的數(shù)據(jù),你可以去掉這行代碼。
這里我們?yōu)橛唵魏涂蛻魯?shù)據(jù)的生成定義了規(guī)則,然后我們調(diào)用了Generate方法來(lái)生成模擬數(shù)據(jù)。就是這么簡(jiǎn)單。
如上所見(jiàn),Bogus提供了許多類來(lái)生成數(shù)據(jù)。例如Company類可以用來(lái)生成公司模擬數(shù)據(jù),例如公司名稱。你可以使用這些生成的數(shù)據(jù)作為你程序的模擬數(shù)據(jù),這些數(shù)據(jù)有3種使用場(chǎng)景
- 單元測(cè)試的模擬測(cè)試數(shù)據(jù)
- 設(shè)計(jì)階段的模擬數(shù)據(jù)
- 原型的模擬數(shù)據(jù)
但是我確信,你能發(fā)現(xiàn)更多的使用場(chǎng)景。
這里為了使用這些數(shù)據(jù),你可以在Main方法中加入以下代碼
static void Main(string[] args) { var repository = new SampleCustomerRepository(); var customers = repository.GetCustomers(); Console.WriteLine(JsonConvert.SerializeObject(customers, Formatting.Indented)); }
這里我們將模擬數(shù)據(jù)轉(zhuǎn)換成了Json字符串,所以這里你需要添加對(duì)Newtonsoft.Json庫(kù)的引用。當(dāng)你運(yùn)行程序之后,你會(huì)得要以下結(jié)果。
如上所見(jiàn),程序生成了一個(gè)顧客的數(shù)據(jù)集,并附帶了每個(gè)顧客的所有訂單信息。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
講解.NET環(huán)境下繪制模糊數(shù)學(xué)中隸屬函數(shù)分布圖
講解.NET環(huán)境下繪制模糊數(shù)學(xué)中隸屬函數(shù)分布圖...2007-11-11C#具名參數(shù)(Named Parameters)的使用
在C#中,具名參數(shù)是一種在方法調(diào)用中使用參數(shù)名稱來(lái)指定參數(shù)值的技術(shù),本文主要介紹了C#具名參數(shù)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01.NET C#利用ZXing生成、識(shí)別二維碼/條形碼
ZXing是一個(gè)開(kāi)放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫(kù),它包含了聯(lián)系到其他語(yǔ)言的端口。這篇文章主要給大家介紹了.NET C#利用ZXing生成、識(shí)別二維碼/條形碼的方法,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒。2016-12-12