private void LoadDataFiles() { var dataFiles = new List <TestDataFile>(); // Load the data foreach (var file in Directory.GetFiles(_configuration.DataFolder, "*.txt")) { var dataFile = new TestDataFile(file); dataFile.EntityName = _configuration.GetEntityName(Path.GetFileNameWithoutExtension(file)); dataFiles.Add(dataFile); } // Load the dependent tables for each file (i.e. what must be imported before they can be imported) foreach (var file in dataFiles) { foreach (var part in file.PartNames) { // TODO: Allow mapping table and column names var typeName = ""; if (part.Contains(".")) { typeName = part.Split('.')[0].TrimStart('-'); } else if (part.EndsWith("ID") || part.EndsWith("Id")) { var fieldName = _configuration.GetFieldName(file.EntityName, part); typeName = _configuration.GetEntityNameForField(file.EntityName, fieldName); if (typeName == fieldName) { typeName = fieldName.Substring(0, fieldName.Length - 2); } typeName = typeName.TrimStart('-'); } if (!string.IsNullOrEmpty(typeName)) { var dep = dataFiles.FirstOrDefault(f => f.EntityName == typeName); if (dep != null) { file.Dependencies.Add(dep); } } } } // Sort so that dependent files are processed first _dataFiles.AddRange(TopologicalSort(dataFiles, f => f.Dependencies)); }
private void ImportData(TestDataFile dataFile) { var entityType = GetEntityType(dataFile.EntityName); var entities = new List <object>(); foreach (var line in dataFile.Lines) { var createMethod = typeof(Database).GetMethod("Create", Type.EmptyTypes).MakeGenericMethod(entityType); var entity = createMethod.Invoke(_db, null); foreach (var propName in line.Parts.Keys) { // TODO: Allow specifying primary key name // Skip ID because it gets set automatically by the database // Howevever, we might like to specify it in the data file so that we can easily refer to what entity has what ID if (propName.Equals("ID", StringComparison.InvariantCultureIgnoreCase)) { continue; } if (!_configuration.ShouldMapField(dataFile.EntityName, propName)) { continue; } var value = line.Parts[propName].Replace("\\n", Environment.NewLine); if (propName.Contains(".")) { // It's a link to another entity, so we have to find that entity var entityPropName = propName.Split('.')[0]; var otherPropName = propName.Split('.')[1]; SetEntityValue(entityType, entity, entityPropName, otherPropName, value); } // TODO: Need configuration options: else if (propName.EndsWith("ID") || propName.EndsWith("Id")) { var fieldName = _configuration.GetFieldName(dataFile.EntityName, propName); var entityPropName = fieldName.Substring(0, fieldName.Length - 2); var otherPropName = fieldName.Substring(fieldName.Length - 2); SetEntityValue(entityType, entity, entityPropName, otherPropName, value); } else { // It's a property value, so set it var fieldName = _configuration.GetFieldName(dataFile.EntityName, propName); SetPropertyValue(entityType, entity, fieldName, value); } } entities.Add(entity); // Save changes after each entity so that we the IDs are inserted in the same order as the data file // That means we can refer to things by their IDs in the data files try { _db.Save(entity); } catch (Exception ex) { throw new Exception($"Error adding {entityType}", ex); } _configuration.OnEntityAdded(_db, dataFile.EntityName, entity, line); // Save changes again, just in case the user did something in _configuration.OnEntityAdded, above try { _db.Save(entity); } catch (Exception ex) { throw new Exception($"Error saving {entityType}", ex); } } _configuration.OnEntitiesSaved(_db, dataFile.EntityName, entities, dataFile); }
/// <summary> /// Called when a set of entities has been saved to the database. /// </summary> /// <param name="db">The database.</param> /// <param name="entityName">Name of the entity.</param> /// <param name="entities">The entities.</param> /// <param name="file">The file.</param> public virtual void OnEntitiesSaved(Database db, string entityName, List <object> entities, TestDataFile file) { }