zoukankan      html  css  js  c++  java
  • 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库的引用。当你运行程序之后,你会得要以下结果。

    如上所见,程序生成了一个顾客的数据集,并附带了每个顾客的所有订单信息。

  • 相关阅读:
    【常见Web应用安全问题】13、Blind SQL/XPath injection
    【常见Web应用安全问题】14、Google Hacking
    【常见Web应用安全问题】14、Google Hacking
    更新和插入新纪录
    Webservice调用方式:axis,soap详解
    Webservice:客户端用https
    hdu 1711(KMP入门题)
    poj 3349(hash判重)
    poj 2486(树形DP+背包)
    zoj 1588(桥)
  • 原文地址:https://www.cnblogs.com/lwqlun/p/10785239.html
Copyright © 2011-2022 走看看