私は、ユーザーがドロップダウンからReportTypeを選択し、ダウンロードボタンを押す必要があります。選択したタイプに基づいて、システムはレポートを生成する必要があります。今はQuoteReportというレポートタイプしかありません。将来私はPolicyReport、ClaimReportのような他のレポートタイプを持っています。今私はこれらのレポートでもデータフィールドが何になるか分かりません。私は、レポートの種類をキャッチするスイッチケースを作成している今、私は何をやっている私はレポートを埋めるためにreporttypeと偶然にを送信し、汎用クラス構造を作成する方法
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; }
}
が選択されています。
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メソッドCreateReportDataは、wcfからQuoteReportクラスのフィールドを埋めます。
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;
}
私のコードをもっと一般的にするにはどうすればいいですか?または、将来のニーズに適応するコードを作るために、Factoryのようないくつかのデザインパターンを使用しますか?どうやってCreateReportDataメソッドをジェネリックにして、どんなクラス型でも受け入れ、サービスからそのプロパティを埋め込むことができます。
現在の状態では、SOの定義があまりにも広すぎます。実際にあなたのユースケースを知るまでは、リファクタリングを待つことをお勧めします。単一のケースのジェネリックコードを書くことは難しく、他のケースを導入するときには役に立たない可能性があります。 –
@AlexeiLevenkov構造を作成するためにデザインパターンを使用する必要があります – pankaj
スイッチを使用しないでください - 異なるタイプを動的に作成できるようにするには、 'Dictionary>'(または同様のもの)を使用してください。また、決して 'catch(Exception exc) 'をコードしてはいけません。**あなたは、**あなたが特別に回復できるエラーを捕まえてください**。 –
Enigmativity