Consider the following small model :
public class Product
{
public long Id { get; set; }
public int Amount { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public double NettoPrice { get; set; }
public Company Supplier { get; set; }
}
public class Company
{
public long Id { get; set; }
public Person Contact { get; set; }
public Address Location { get; set; }
}
public class Person
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address Residence { get; set; }
}
public class Address
{
public string City { get; set; }
public string PostalCode { get; set; }
public string Street { get; set; }
public int HouseNumber { get; set; }
}
Then using the GeneratorRepository like so :
new GeneratorRepository()
.Random<Product>();
Returns f.i. :
{Product}
Int64 Id = 96
Int32 Amount = 43
String Description = m[wt`t\`f\g\u]vxugrg\\fdta
Double Price = 53,4536713443015
Double NettoPrice = 32,9169867219948
Company Supplier =
null
Composing the Random Value
Revisiting the RepositoryGenerator’s With method, we use it to inform the repository of the other classes that are needed for generation :
new GeneratorRepository()
.With<Address>()
.With<Person>()
.With<Company>()
.Random<Product>();
And we get f.i. :
{Product}
Int64 Id = 21
Int32 Amount = 85
String Description = rtywrzZmr[]f
Double Price = 16,9683141712883
Double NettoPrice = 79,5729548519351
Company Supplier =
Int64 Id = 6
Person Contact =
Int64 Id = 56
String FirstName = kuxwqlgsok^sd`c`ucz]\ouzi]xvpp`fhZmgxd[al^\hvtguu`ej\iwvzxhmok]
String LastName = esecnvbkbrlylg]op\[idpcz^qgZvhlZ[]\]i^ayxubdxyclttxmjnopje_wib
Address Residence =
String City = t`kwieZ_lu\kihy[_gu`\kzjhhgn^ydjp`h^f[tat
String PostalCode = erh\`bkdzjn\lc`[i[xg[mklvczto^mnfi_a[ea
String Street = \tzuvnn_xe
Int32 HouseNumber = 56
Address Location =
String City = cglsavawbqovwZp]]yz^tsdampqbotvdludzwfZ`^ewe_^xu
String PostalCode = k[yfhyk\d_k_ybived[qp`rytziwz\irvd^rwx]wtxl`i[_\gjutw^bddZl
String Street = in]bqsco]qyg`_xsokisufiibovr\
Int32 HouseNumber = 83
Ignoring properties
In case we would like the id properties of the classes to be ignored we could define it specifically for every type :
new GeneratorRepository()
.With<Address>()
.With<Person>(gen => gen.Ignore(val => val.Id))
.With<Company>(gen => gen.Ignore(val => val.Id))
.Random<Product>(gen => gen.Ignore(val => val.Id));
Or we could use a With
convention that uses the name of the property.
new GeneratorRepository()
.With<long>(mi=> mi.Name == "Id", val => 0)
.With<Address>()
.With<Person>()
.With<Company>()
.Random<Product>();
‘mi’ stands for MemberInfo, so for each property generated of type long and of which the Name equals Id, we generate a zero.
Both giving the same result :
{Product}
Int64 Id = 0
Int32 Amount = 67
String Description = ldl_hifv
Double Price = 40,0705830343396
Double NettoPrice = 85,4157260276451
Company Supplier =
Int64 Id = 0
Person Contact =
Int64 Id = 0
String FirstName = ]e\q]trnkqu]alb\qhpx[^
String LastName = muhsmjyv[zj
Address Residence =
String City = ^]u`jxk[
String PostalCode = r[`x]tpgnecy^ivpZ_]lut^rh^zwa_zk]lvsavi
String Street = bhgna_znykjsgjsyxiizut`ifsvmn]h^pgcgfkjc_eyp
Int32 HouseNumber = 64
Address Location =
String City = ]ntwusgjvibyb
String PostalCode = cfzu]f\z`flgy`tiuju_obtqbor`
String Street = t^hb\zsf]lb^^ggdhu`pfor]\tZmbmiowhf
Int32 HouseNumber = 22
More conventions : Formatting sample
What if we would like our generated doubles that are named like price to be rounded :
new GeneratorRepository()
.With<double>(mi => mi.Name.ToLower().Contains("price"),
val => Math.Round(val, 2))
.Random<Product>();
Gives us f.i. :
Int64 Id = 87;
Int32 Amount = 10;
String Description = onqpc`^g`nwysvuhadckub_^ddhgd[]p[hwhdtnxiilmvnvd^qihmm[fi;
Double Price = 57,37;
Double NettoPrice = 34,69;
Company Supplier =
null
Ofcourse in our case, the precondition is valid for both doubles, so let’s just drop it :
new GeneratorRepository()
.With<double>(val => Math.Round(val, 2))
.Random<Product>();
Gives a similar result.