Closed vdurante closed 5 years ago
Hi Vitor,
Thanks for your question. The purpose of Faker<T>
is to be used with Domain Objects, DTOs, or other User Type classes. T
is not really for primitive or built-in types like string
or int
. So let's scratch that option.
To generate a list of CPFs, we'd have to use something like this:
var cpfs = Enumerable.Range(0,5)
.Select(i => new Person().Cpf())
.ToList();
cpfs.Dump();
OUTPUT:
386.629.435-27
507.474.505-52
512.237.960-27
571.073.842-52
943.659.996-05
Hope that helps!
Thanks, Brian
:beach_umbrella: :trumpet: Beach Boys - Good Vibrations (Nick Warren bootleg)
@bchavez thank you! That is pretty elegant!
Just in case somebody still needs something to generate a collection of primitive data type :
var fakeBrands = new Faker<string>()
.CustomInstantiator(f => f.Company.CompanyName());
Any workaround for creating List of ints or other primitive types?
@Giorgi, to generate a list of primitive types:
void Main()
{
var f = new Faker();
var values = Enumerable.Range(1, 50)
.Select(_ => f.Random.UInt())
.ToList();
values.Dump();
}
Faker<T>
is generally for class
types; for everyting else, Faker
facade is recommended.
Thanks, is Faker
threadsafe?
Thanks, is Faker threadsafe?
It depends. There is a basic lock
around System.Random
when drawing numbers per Faker
instance. Much of the "thread safety" is also inherited from System.Random
:
You basically lose determinism if two or more threads are operating on the same Faker
instance because OS thread scheduling is non-deterministic.
If you need to generate a large amount of data it is best to partition the data generation range per thread; eg
where each thread has its own instance of Faker
and has a dedicated seed value. The technique is briefly described here:
@bchavez Is there any recommended way for generating BigInteger
?
Hi @Giorgi, there's no standard or recommended way for generating BigInteger
types. It is whatever is based on your needs. Here are a few examples:
void Main()
{
var faker = new Faker();
faker.Random.BigInteger().Dump();
faker.Random.BigIntegerDigits(80).Dump();
}
public static class ExtensionsForRandomizer
{
public static BigInteger BigInteger(this Randomizer r)
{
var data = r.Bytes(30);
var b = new BigInteger(data);
return b;
}
public static BigInteger BigIntegerDigits(this Randomizer r, int digits)
{
var data = r.String2(digits, "0123456789");
var b = System.Numerics.BigInteger.Parse(data);
return b;
}
}
OUTPUT:
-22308249314910248646346984522781668288108116497977709677600309392141845
41351867314669609175092923064604245036485730585133764699088587903417011361966885
I am trying to create e List of CPFs (brazilian IDs). I tried to do the following:
I guess internally, Bogus tries to instantiate a new string
o
and fails to do so. Is there a way to use Bogus to generate list of primitive types?Thanks in advance.