/// <summary> /// Generates a number of random records, checking to see if they violate a key before saving /// </summary> public void GenerateUnique <TModel>(IDbConnection connection, int recordCount, Action <TModel> create, Func <IDbConnection, TModel, bool> exists, Action <TModel> save) where TModel : new() { _rndFormatted = new RandomFormattedString(_rnd); for (int i = 0; i < recordCount; i++) { if (_cancellationToken.IsCancellationRequested) { break; } TModel record = new TModel(); int attempts = 0; const int maxAttempts = 100; do { attempts++; if (attempts > maxAttempts) { throw new Exception($"Couldn't generate a random {typeof(TModel).Name} record after {maxAttempts} tries. Exception was thrown to prevent infinite loop."); } create.Invoke(record); } while (exists.Invoke(connection, record)); save.Invoke(record); } }
public void Generate <TModel>(int recordCount, Action <TModel> create, Action <IEnumerable <TModel> > save) where TModel : new() { _rndFormatted = new RandomFormattedString(_rnd); List <TModel> records = new List <TModel>(BatchSize); int recordNum = 0; for (int i = 0; i < recordCount; i++) { if (_cancellationToken.IsCancellationRequested) { break; } recordNum++; TModel record = new TModel(); create.Invoke(record); records.Add(record); if (recordNum == BatchSize) { save.Invoke(records); records.Clear(); recordNum = 0; } } if (_cancellationToken.IsCancellationRequested) { return; } // any leftovers save.Invoke(records); }
protected override string Append() { var rndString = new RandomFormattedString(Random) { Format = "-000" }; return(rndString.GetData()); }