mardi 10 mars 2020

Generics calling different function based on type

I have a problem that I think would benefit from some generics, but I'm having trouble wrapping my head around. I have a FileImporter Class

public class FileImporter<T>
{
     public async Task ImportFile(string filePath)
        {
            StreamReader fileReader;
            String fileLine;

            fileReader = new StreamReader(filePath);
            List<T> entityList = new List<T>();

            if ((fileLine = fileReader.ReadLine()) != null)
            {
                headerNames = fileLine.ToLower().Split(',').ToList();

                try
                {
                    while ((fileLine = fileReader.ReadLine()) != null)
                    {
                        lineInfo = fileLine.Split(',').ToList();

            //Need something here to define a new Entity and load it from the file.  
            //This would of course be unique to each entity type        

                        entityList.Add(entity);
                    }


                    await _context.BulkInsertOrUpdateAsync(entityList);

                }
                catch (Exception)
                {

                    throw;
                }
            }
        }
}

I'm trying to get away from duplicating all the code in the Import File function, because the only difference is adding creating the entity based on the values in the file.

Aucun commentaire:

Enregistrer un commentaire