mercredi 18 janvier 2017

how to create a generic class structure

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.

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 paramters 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 Shall 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 up its properties from service.

Aucun commentaire:

Enregistrer un commentaire