C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
原文:CREATING SAMPLE DATA FOR C#
作者:Bruno Sonnino
譯文:C#中使用Bogus創(chuàng)建模擬數(shù)據(jù)
譯者: Lamond Lu
背景
在我每次寫技術類博文的時候,經(jīng)常做的一件事就是創(chuàng)建模擬數(shù)據(jù)。在每篇博文中,為了解釋某些概念,我需要創(chuàng)建許多模擬數(shù)據(jù)。這是一個我在實際中遇到的問題,因為我需要為我的程序找到一些合適的數(shù)據(jù)。有些時候,我會從數(shù)據(jù)庫中找一些數(shù)據(jù)(Northwind和AdventureWorks都是我的好朋友^.^), 有些時候,我會使用一些現(xiàn)成的Json或者Xml數(shù)據(jù),當然有時候我只能自己手動創(chuàng)建一些數(shù)據(jù)。
當然以上方案都不完美,也都不穩(wěn)定,所以每次我都需要探索一些新方式來獲取數(shù)據(jù)(對于學習來說這很好,但是維護起來確是一種災難)。
最后我找到了Bogus, 一個基于C#的簡單數(shù)據(jù)生成器。
使用Bogus生成模擬數(shù)據(jù), 你只需要定義規(guī)則并生成數(shù)據(jù)即可,就是這么簡單。而且Bogus可以生成固定數(shù)據(jù)或者變化數(shù)據(jù)。這樣一旦你拿到了這些數(shù)據(jù),你就可以把它們序列化成你想要的格式: json, xml,數(shù)據(jù)庫或者文本文件。
生成模擬數(shù)據(jù)
為了生成模擬數(shù)據(jù),我們首先需要針對模擬數(shù)據(jù)創(chuàng)建對應的實體類。這里我們可以創(chuàng)建一個命令行程序,并添加一下兩個類。
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)建好以上兩個實體類之后,你就可以來添加倉儲來獲取模擬數(shù)據(jù)了。為了使用Bogus, 你可以使用Nuget將Bogus庫添加到你的項目中。
Install-Package Bogus
下面我們就可以來添加一個倉儲類來獲取模擬數(shù)據(jù)了。這里我們添加一個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屬性指定一個固定的隨機種子,因此每次生成的數(shù)據(jù)都是一樣的。如果你不希望每次都生成固定的數(shù)據(jù),你可以去掉這行代碼。
這里我們?yōu)橛唵魏涂蛻魯?shù)據(jù)的生成定義了規(guī)則,然后我們調(diào)用了Generate方法來生成模擬數(shù)據(jù)。就是這么簡單。
如上所見,Bogus提供了許多類來生成數(shù)據(jù)。例如Company類可以用來生成公司模擬數(shù)據(jù),例如公司名稱。你可以使用這些生成的數(shù)據(jù)作為你程序的模擬數(shù)據(jù),這些數(shù)據(jù)有3種使用場景
- 單元測試的模擬測試數(shù)據(jù)
- 設計階段的模擬數(shù)據(jù)
- 原型的模擬數(shù)據(jù)
但是我確信,你能發(fā)現(xiàn)更多的使用場景。
這里為了使用這些數(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字符串,所以這里你需要添加對Newtonsoft.Json庫的引用。當你運行程序之后,你會得要以下結果。
如上所見,程序生成了一個顧客的數(shù)據(jù)集,并附帶了每個顧客的所有訂單信息。
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對我們的支持。
上一篇:C#多線程中的異常處理操作示例
欄 目:C#教程
本文標題:C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4765.html
您可能感興趣的文章
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務開啟應用程序的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10c# ArrayList的使用方法小總結
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說明


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文