/// <summary> /// Given a filename, creates a suitable ImportFile object and Importer. /// </summary> /// <param name="importFilename"></param> /// <returns>An Importer, or null if the type of data in the source could not be determined.</returns> public static Importer <T> Create(string importFilename) { var ext = Path.GetExtension(importFilename).TrimStart('.').ToLower(); ImportFile impFile = ext == "csv" ? (ImportFile) new CsvImportFile(importFilename) : ext.StartsWith("xl") ? new XlsImportFile(importFilename) : null; return(impFile != null ? new Importer <T>(impFile) : null); }
/// <summary> /// Constructs an Importer object, and sets up the Target and Import fields. /// </summary> /// <param name="importFile">The source data</param> public Importer(ImportFile importFile) { _importFile = importFile; TargetFields = GetTargetFields().ToList(); ImportFields = ImportField <T> .Create(importFile).ToList(); // If importing directly into a DbSet, this attempts to determine the primary key property // by looking for a property with the Key attribute. It can alternatively be set manually // in the ImportTargetField object. var keyField = TargetFields.FirstOrDefault(f => f.Prop.CustomAttributes.Any(a => a.AttributeType == typeof(KeyAttribute))); if (keyField != null) { keyField.IsKeyField = true; } // Tries to guess which import fields match which target properties. Guess(); }
/// <summary> /// Creates a set of ImportField objects from the input source. /// </summary> /// <param name="inputData"></param> /// <returns></returns> public static IEnumerable <ImportField <T> > Create(ImportFile inputData) { return(inputData.FieldNames.Select(f => new ImportField <T>(f))); }