/// <summary> /// Description: Return Instance Of POCO Classes Based On DTO Format /// </summary> /// <param name="dtoFormat">Enum For DTO Format</param> /// <param name="censusDAO">Instance Of CensusDAO</param> /// <returns>Returns Object Based On DTOFormat</returns> public object GetDTO(DTO dtoFormat, CensusDAO censusDAO) { if (dtoFormat.Equals(DTO.INDIA_CENSUS)) { return(new IndianCensus(censusDAO.State, censusDAO.Population, censusDAO.AreaInSqKm, censusDAO.PopulationDensity)); } if (dtoFormat.Equals(DTO.INDIA_STATE_CODE)) { return(new IndianStateCode(censusDAO.State, censusDAO.SrNo, censusDAO.TIN, censusDAO.StateCode)); } return(new USCensus(censusDAO.State, censusDAO.StateCode, censusDAO.Population, censusDAO.WaterArea, censusDAO.HousingUnits, censusDAO.HousingDensity, censusDAO.LandArea, censusDAO.AreaInSqKm, censusDAO.PopulationDensity)); }
/// <summary> /// Description: Load Census data From CSV File And Return Dictionary Of String as Key And Census DAO as Value. /// </summary> /// <typeparam name="T"> Generic Type </typeparam> /// <param name="headers"> CSVFile Headers </param> /// <param name="csvFilePath"> CSVFile Path </param> /// <returns> Dictionary Of String As key And CensusDAO As Value </returns> public Dictionary <object, CensusDAO> ReadCSVFile <T>(string headers, string csvFilePath) { if (!File.Exists(csvFilePath)) { throw new CSVFilesReaderException("File Not Found", CSVFilesReaderException.CSVReaderExceptionType.FILE_NOT_FOUND); } if (Path.GetExtension(csvFilePath) != ".csv") { throw new CSVFilesReaderException("Incorrect File Format", CSVFilesReaderException.CSVReaderExceptionType.INCORRECT_FILE_FORMAT); } string[] csvFileData = File.ReadAllLines(csvFilePath); if (csvFileData[0] != headers) { throw new CSVFilesReaderException("Incorrect Headers", CSVFilesReaderException.CSVReaderExceptionType.INCORRECT_HEADER); } foreach (string record in csvFileData.Skip(1)) { if (!record.Contains(",")) { throw new CSVFilesReaderException("File Contains Wrong Delimiter", CSVFilesReaderException.CSVReaderExceptionType.INCORRECT_DELIMITER); } } using (var readers = new StreamReader(csvFilePath, Encoding.Default)) using (var csv = new CsvReader(readers, System.Globalization.CultureInfo.CurrentCulture)) { return(csv.GetRecords <T>().ToList().ToDictionary(x => x.GetType().GetProperty("State").GetValue(x, null), x => CensusDAO.GetDAO(x))); } }