简单介绍一下背景知识:我们有一个文本文件,其中存储了几个KPI;例如,一行显示为*KPI_One*,因此我读取(并处理)了X行,直到我读取了该KPI的所有数据。这是通过几个KPI完成的。KPI之间没有关系,每个KPI都有不同的格式。
无论如何,我已经在读取文件并相应地处理每一行。问题是有大量的KPI,所以对于每个KPI,必须有一个表、一个存储过程来存储,以及另一个要检索的KPI。在C#代码中,每个KPI都有一个具有结构的类、一个解析字符串的方法、一个插入解析行的方法和一个检索数据的方法。
如你所见,每个KPI都需要6个不同的步骤,并且变得单调乏味。我的问题是:有没有办法将这一切全球化?
谢谢。
发布于 2014-03-29 07:37:19
这里有一个简化整个问题的方法。
one解决方案:
“‘KPI”表列包括:
C#代码:
public enum KPIType
{
None = 0,
KPI_One = 1, // the value corresponds to the Id column values in KPI table.
KPI_Two = 2,
// so on
}
Dictionary<KPIType, Func<string, string>> kpiProcessors =
new Dictionary<KPIType, Func<string, string>>();
// define all the kpi processor delegates.
// the delegate will take string line data from the input file and
// convert into the Struct object for that KPI and serialize this struct
// into JSON.
// note the output string is not same as the input file string. it is JSON
// of the structure for the KPI.
kpiProcessors.Add(KPIType.KPI_One, KPIOneDataProcessorMethodName);
kpiProcessors.Add(KPIType.KPI_Two, KPITwoDataProcessorMethodName);
// KPIOneDataProcessorMethodName is of the form
public string KPIOneDataProcessorMethodName(string inputFileKpiLineData)
{
struct KPI_One_Struct;
// process inputFileKpiLineData to populate KPI_One_Struct
return JsonConvert.SerializeObject(KPI_One_Struct);
}
void ProcessFile()
{
// actual file processing logic.
do
{
KPIType currentKPIType = KPI_One; // read current KPI;
while (string lineData = KPI_Type_Line_Data_From_File)
{
Func<string, string> kpiFunc = kpiProcessors[currentKPIType];
string jsonKpiStructData = kpiFunc(lineData);
SQLHelper.InsertKpiDataLine(currentKPIType, jsonKpiStructData);
}
} while (kpidataisreadfromfile)https://stackoverflow.com/questions/22722462
复制相似问题