本文主要是介绍using Bogus,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
using Bogus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
List<Customer> cc = GetCustomerGenerator().Generate(20);
foreach (var item in cc)
{
Console.WriteLine(item.ZipCode);
}
}
public static Faker<Customer> GetCustomerGenerator()
{
// https://www.cnblogs.com/ljknlb/p/15862473.html
var ordergenerator = new Faker<Order>("zh_CN")
.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>("zh_CN")
.RuleFor(c => c.Id, Guid.NewGuid())
.RuleFor(c => c.Name, f => f.Person.LastName + f.Person.FirstName)
.RuleFor(x => x.JobTitle, x => x.Name.JobTitle())
.RuleFor(x => x.JobDescription, x => x.Name.JobDescriptor())
.RuleFor(x => x.JobArea, x => x.Name.JobArea())
.RuleFor(x => x.Word, x => x.Lorem.Word())
//Sentence
.RuleFor(x => x.Sentence, x => x.Lorem.Sentence())
.RuleFor(x => x.Age, x => x.Random.Int(0, 120))
.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.Person.LastName + f.Person.FirstName)
.RuleFor(c => c.VehicleManufacturer, f => f.Vehicle.Manufacturer())
.RuleFor(x => x.CreateOn, x => x.Date.Past(1))
.RuleFor(x => x.Review, x => x.Rant.Review())
.RuleFor(x => x.Balance, x => x.Finance.Amount(0, 1000))
.RuleFor(c => c.Orders, f => ordergenerator.Generate(f.Random.Number(10)).ToList());
//var personFaker = new Faker<Memmber>()
// .RuleFor(x => x.ID, x => x.Random.Guid())
// .RuleFor(x => x.NO, x => x.Random.Long(100000000000, 999999999999))
// .RuleFor(x => x.Name, x => x.Person.LastName + x.Person.FirstName)
// .RuleFor(x => x.Sex, x => x.Random.Enum<Sex>())
// .RuleFor(x => x.Email, x => x.Person.Email)
// .RuleFor(x => x.Phone, x => x.Person.Phone)
// .RuleFor(x => x.Age, x => x.Random.Int(0, 120))
// .RuleFor(x => x.Balance, x => x.Finance.Amount(0, 1000))
// .RuleFor(x => x.CreateOn, x => x.Date.Past(1));
return customerGenerator;
}
}
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 int Age { get; set; }
public DateTime CreateOn { get; set; }
public decimal Balance { get; internal set; }
public object VehicleManufacturer { get; internal set; }
public object Review { get; internal set; }
public object JobTitle { get; internal set; }
public object JobDescription { get; internal set; }
public object JobArea { get; internal set; }
public object Word { get; internal set; }
public object Sentence { get; internal set; }
}
public class Order
{
public Guid Id { get; set; }
public DateTime Date { get; set; }
public Decimal OrderValue { get; set; }
public bool Shipped { get; set; }
}
}
这篇关于using Bogus的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!