I need to upload a report to other different system, the system have their own predefined structure of the reports which they accept. Those report on our system is generated on the basis of the code which the user selects.
User does not care about the column they wanted to export on the reports, they only need to select the code
from the UI. Our system will generate, download the right format of the CSV with reference to code
and that will be used as report to upload to another system.
The headers and the number of columns on the CSV files differ by the type of code user selects.
How i have approached by far:
public ActionResult GetFileResult(string code)
{
var record = new Employee().GetEmployeeData(code);
var csvResult = GetCSVResult(code, record);
return csvResult;
}
private string GetCSVResult(string code, List<Employee> employees)
{
//How can i model here the GetCSVResult to convert
//the List of employees to CSV with refrence to code
//the value on the code will determine which format to used for the csv result.
}
Here in GetCSVResult
method i can use multiple if
or switch-case
statement to call the different method which has it's own implementation of converting list to CSV, but there are at least 20 different CSV configuration which will lead to multiple if statement and a lot of method.
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string DOB { get; set; }
public string StartDate { get; set; }
public string SSN { get; set; }
public bool PreviousEmployee { get; set; }
public List<Employee> GetEmployeeData(string code)
{
return new List<Employee>();
}
}
The format for the initial data are in the following structure. i.e the value returned from GetEmployeeData
FirstName | LastName | SSN | StartDate | DOB | PreviousEmployee
------------------------------------------------------------------------------------------
Jane | Smith | 111111121 | 01/03/2018 | 01/01/1983 | true
John | Smith | 111111111 | 01/01/2018 | 01/01/1970 | true
Jeff | Smith | 111111122 | 01/03/2018 | 01/01/1983 | false
Now i have to convert this data into CSV(or some into XML) files with different file configuration.
Like Examples:
Some Format could be like:
Format 1:
FirstName,LastName,SSN,AppStartDate,AppDOB
Jane,Smith,111111121,01/03/2018,01/01/1983
John,Smith,111111111,01/01/2018,01/01/1970
Jeff,Smith,111111122,01/03/2018,01/01/1983
Format 2:
EMP_FIRST_NAME,EMP_LAST_NAME,EMP_SSN,EMP_DOB,EMP_JOB_START_DATE,PREV_EMPLOYED_BY_EMPLOYER
Jane,Smith,111111121,01/03/2018,01/01/1983,Y
John,Smith,111111111,01/01/2018,01/01/1970,Y
Jeff,Smith,111111122,01/03/2018,01/01/1983,N
The format in the Examples differ with the type of Code
. The format are in structure defined by the value on the code.
So, if there are any suggestion for some generic way to this type of scenario? or Any suggestion for some pattern?
For converting to CSV i am looking at this thread
https://medium.com/@utterbbq/c-serializing-list-of-objects-to-csv-9dce02519f6b