Replies: 2 comments 3 replies
-
No problems here; ran your code 1 million times and no exception and ran in about 6 seconds: void Main()
{
var keyFaker = new Faker<Key>()
.RuleFor(k => k.AgentClientUserId, f => f.Random.Number(10000000, 99999999).ToString());
var demographicFaker = new Faker<Demographic>()
.RuleFor(d => d.Gender, f => f.PickRandom(new[] { "M", "F" }))
.RuleFor(d => d.FirstName, (f, a) => f.Name.FirstName(a.Gender == "M" ? Name.Gender.Male : Name.Gender.Female))
.RuleFor(d => d.MiddleInitial, f => f.Random.Bool(0.7f) ? null : f.Lorem.Letter().ToUpper())
.RuleFor(d => d.LastName, f => f.Name.LastName())
.RuleFor(d => d.Suffix, f => f.Random.Bool(0.9f) ? null : f.Name.Suffix())
.RuleFor(d => d.DateOfBirth, f => f.Date.Between(new DateTime(1965, 1, 1), new DateTime(2003, 6, 10)))
.RuleFor(d => d.SocialSecurityNumber, f => f.Random.Number(100000000, 999999999).ToString());
var addressAddFaker = new Faker<AddressAdd>()
.RuleFor(add => add.AddressType, f => f.PickRandom("7091", "7092", "7093"))
.RuleFor(add => add.AddressLine1, f => f.Address.StreetAddress())
.RuleFor(add => add.AddressLine2, f => f.Address.SecondaryAddress())
.RuleFor(add => add.City, f => f.Address.City())
.RuleFor(add => add.State, f => f.Address.StateAbbr())
.RuleFor(add => add.ZipCode, f => f.Address.ZipCode())
.RuleFor(add => add.Country, f => "USA")
.RuleFor(add => add.FromDate, f => f.Random.Bool(0.8f) ? (DateTime?)null : f.Date.Between(new DateTime(2010, 1, 1), new DateTime(2015, 12, 31)))
.RuleFor(add => add.ToDate, (f, d) => d.FromDate == null ? (DateTime?)null : f.Random.Bool(0.8f) ? null : d.FromDate.AddYears(1));
var addressFaker = new Faker<MyAddress>()
.RuleFor(a => a.AddressAdd, f => addressAddFaker.Generate());
var personFaker = new Faker<Person>()
.RuleFor(a => a.Key, f => keyFaker.Generate())
.RuleFor(a => a.Demographic, f => demographicFaker.Generate())
.RuleFor(a => a.Address, f => addressFaker.Generate());
var persons = personFaker.Generate(1000000);
"done".Dump();
}
public class Key{
public string AgentClientUserId;
}
public class Demographic
{
public string Gender;
public string FirstName;
public string MiddleInitial;
public string LastName;
public string Suffix;
public DateTime DateOfBirth;
public string SocialSecurityNumber;
}
public class AddressAdd
{
public string AddressType;
public string AddressLine1;
public string AddressLine2;
public string City;
public string State;
public string ZipCode;
public string Country;
public DateTime FromDate;
public DateTime ToDate;
}
public class MyAddress
{
public AddressAdd AddressAdd;
}
public class Person{
public Key Key;
public Demographic Demographic;
public MyAddress Address;
} Perhaps your error has something to do with your domain models that you didn't post. Lastly, I would not use The benefit of using Break up the code above with Thanks, |
Beta Was this translation helpful? Give feedback.
-
To further explain, here's the same problem but using void Main()
{
var f = new Faker();
var people = Enumerable.Range(0, 5)
.Select(_ => CreatePerson(f))
.ToList();
people.Dump();
}
public Key CreateKey(Faker f)
{
var k = new Key{};
k.AgentClientUserId = f.Random.Number(10000000, 99999999).ToString();
return k;
}
public Demographic CreateDemographic(Faker f)
{
var d = new Demographic();
d.Gender = f.PickRandom(new[] { "M", "F" });
d.FirstName = f.Name.FirstName(d.Gender == "M" ? Name.Gender.Male : Name.Gender.Female);
d.MiddleInitial = f.Random.Bool(0.7f) ? null : f.Lorem.Letter().ToUpper();
d.LastName = f.Name.LastName();
d.Suffix = f.Random.Bool(0.9f) ? null : f.Name.Suffix();
d.DateOfBirth = f.Date.Between(new DateTime(1965, 1, 1), new DateTime(2003, 6, 10));
d.SocialSecurityNumber = f.Random.Number(100000000, 999999999).ToString();
return d;
}
public AddressAdd CreateAddressAdd(Faker f)
{
var add = new AddressAdd();
add.AddressType = f.PickRandom("7091", "7092", "7093");
add.AddressLine1 = f.Address.StreetAddress();
add.AddressLine2 = f.Address.SecondaryAddress();
add.City = f.Address.City();
add.State = f.Address.StateAbbr();
add.ZipCode = f.Address.ZipCode();
add.Country = "USA";
add.FromDate = f.Random.Bool(0.8f) ? (DateTime?)null : f.Date.Between(new DateTime(2010, 1, 1), new DateTime(2015, 12, 31));
add.ToDate = add.FromDate == null ? (DateTime?)null : f.Random.Bool(0.8f) ? null : add.FromDate?.AddYears(1);
return add;
}
public MyAddress CreateMyAddress(Faker f){
var ma = new MyAddress();
ma.AddressAdd = CreateAddressAdd(f);
return ma;
}
public Person CreatePerson(Faker f)
{
var p = new Person();
p.Key = CreateKey(f);
p.Demographic = CreateDemographic(f);
p.Address = CreateMyAddress(f);
return p;
}
public class Key
{
public string AgentClientUserId;
}
public class Demographic
{
public string Gender;
public string FirstName;
public string MiddleInitial;
public string LastName;
public string Suffix;
public DateTime DateOfBirth;
public string SocialSecurityNumber;
}
public class AddressAdd
{
public string AddressType;
public string AddressLine1;
public string AddressLine2;
public string City;
public string State;
public string ZipCode;
public string Country;
public DateTime? FromDate;
public DateTime? ToDate;
}
public class MyAddress
{
public AddressAdd AddressAdd;
}
public class Person
{
public Key Key;
public Demographic Demographic;
public MyAddress Address;
} And when you do this you get a lot better compiler help. When I converted this code to Maybe the Again, I cannot stress enough; when writing large complex hierarchical object graphs, it is 9 times out of 10 always better to use the |
Beta Was this translation helpful? Give feedback.
-
Big fan of Bogus.Faker! I think it's an awesome way to seed DBs with lots of realistic data for testing purposes.
I'm trying to create fake data for a somewhat complicated XML structure
There's a lot more to this. For example, we would be trying with adding a variable number of ADDRESS/ADD (Home Address, Work Address, etc.), but I'm not even making it this far when trying to run locally. I'm mapping the class and property names to the XML properties so it's not always a 1:1 matchup on naming. Here's what I'm trying:
If I re-run the same code, sometimes I get a NullReferenceException on demographicFaker. Sometimes I get it on addressFaker.
addressFaker NullReferenceException stack on
.RuleFor(a => a.Address, f => addressFaker.Generate())
demographicFaker NullReferenceException stack on
.RuleFor(a => a.Demographic, f => demographicFaker.Generate())
I have seen in other Issues that Brandon Chavez has responded to that it's not a good idea to create in loops or "on the fly." What would be the recommended way to do this?
Beta Was this translation helpful? Give feedback.
All reactions