-
Notifications
You must be signed in to change notification settings - Fork 1
Data Generation
From my experience for most line of business applications there is no such thing like an "empty database". Even such a database already requires some initial records to be present. Also, it is desirable to be able to generate a "production like" test database from the code that evolves with release cycles, having compiler checked type safety, instead of often breaking SQL scripts. (By the way, who says the store needs to be SQL?)
Backend.Fx provides a pattern abstraction I call DataGeneration
. When creating a tenant, you can initialize this tenant using some generator classes. Two classes of databases can bu loaded this way: the DemoDatabase
and the ProductionDatabase
. To distinguish, for which database the generator class is meant, the two marker interfaces IDemoDataGenerator
and IProdDataGenerator
are available.
A typical generator looks like this:
public class SupplierGenerator : InitialDataGenerator, IDemoDataGenerator
{
private readonly IEntityIdGenerator idGenerator;
private readonly IRepository<Supplier> supplierRepository;
public SupplierGenerator(IEntityIdGenerator idGenerator, IRepository<Supplier> supplierRepository)
{
this.idGenerator = idGenerator;
this.supplierRepository = supplierRepository;
}
public override int Priority
{
get { return 1; }
}
protected override void GenerateCore()
{
for (int i = 0; i < 10; i++)
{
Supplier supplier = new Supplier(idGenerator.NextId(), GenerateSupplierName(), TestRandom.Next(5));
supplierRepository.Add(supplier);
}
}
protected override void Initialize()
{ }
protected override bool ShouldRun()
{
return supplierRepository.Any();
}
private string GenerateSupplierName()
{
switch (TestRandom.Next(3))
{
case 0: return Names.Family.Random() + " " + Names.LegalEntityTypes.Random();
case 1: return Names.Cities.Random() + " " + Names.LegalEntityTypes.Random();
case 2: return Names.Family.Random() + " & " + Names.Family.Random() + " " + Names.LegalEntityTypes.Random();
default: return "";
}
}
}
To make demo data generation more enjoyable, you might want to check out the namespace Backend.Fx.RandomData
.