I have a requirement where user selects a ReportType from a dropdown and hits download button. Based on his type chosen, the system should generate a report. Right now I have only report type that is QuoteReport. In future I will have other report types like PolicyReport,ClaimReport. Right now I have no idea what will be data-fields in these reports too, But "They all will have at least some common properties" such as ID, and Address
public class QuoteReport
{
public String DeviceType { get; set; }
public String ProductName { get; set; }
public String Description { get; set; }
public String ID { get; set; }
public String Address { get; set; }
}
Now what I am doing is I send reporttype and parameters to fill the report and I have created a switch case to catch type of report being selected.
public string PrepareReport(string selectedReport, List<int> Ids)
{
string response = string.Empty;
try
{
ReportTypeEnum reportTypeEnum;
if (Enum.TryParse(selectedReport, out reportTypeEnum))
{
switch (reportTypeEnum)
{
case ReportTypeEnum.QuoteReport:
response = CreateReportData(Ids,response);
break;
default:
break;
}
}
}
catch (Exception exc)
{
handleException(DOWNLOAD_REPORT, exc);
}
return response;
}
My method CreateReportData fills the fields of QuoteReport class from wcf.
public string CreateReportData(List<int> Ids, string response)
{
List<QuoteReport> quoteReportList = new List<QuoteReport>();
foreach (var Id in Ids)
{
dynamic dynamicEntity;
List<string> devices = proxy.GetData(Id);
for (int i = 0; i < devices.Count; i++)
{
QuoteReport quoteReport = new QuoteReport();
dynamicEntity = JObject.Parse(devices[i]);
quoteReport.Type = dynamicEntity.DeviceTypeString;
quoteReport.ProductName = dynamicEntity.ProductName;
quoteReport.Description = dynamicEntity.Desc;
quoteReport.ID = dynamicEntity.ID;
assetReport.Address = dynamicEntity.Address;
quoteReportList.Add(quoteReport );
}
}
response = JsonConvert.SerializeObject(quoteReportList );
return response;
}
Now I am perplexed how can I make my code more generic. Or should I use some design patterns like Factory to make code adaptable for future needs? How can I make CreateReportData method generic so that it accepts any class type and fills it up?
String -> QuoteReportfunction is useful.