var field = GetFieldStringValue(columnCode);
if (IsValidDecimal(field))
{
currentRow.Amount = Decimal.Parse(field);
}
else
{
ReportError();
}
最后,我选择了这样一个方法(对于各种类型都有重载):
private void SetPropertyOrReportError(Action<decimal?> e, string columnCode)
{
if (IsValidDecimal(columnCode))
{
var fieldValue = Decimal.Parse(GetFieldStringValue(columnCode));
e(fieldValue);
}
else if (!IsColumRequired(columnCode))
{
e(null);
}
else
{
SetPropertyOrReportError();
}
}
这样,对于每一行,我都会得到如下代码:
var currentRow = new ClaimImportHelper();
SetPropertyOrReportError((e) => currentRow.ClaimantName = e, ClaimantNameColumnCode);
SetPropertyOrReportError((e) => currentRow.CustomerName = e, CustomerNameColumnCode);
SetPropertyOrReportError((e) => currentRow.CustomerProject = e, CustomerProjectColumnCode);
SetPropertyOrReportError((e) => currentRow.ClaimDate = e.Value, ClaimDateColumnCode);
SetPropertyOrReportError((e) => currentRow.ClaimSubmitted = e, ClaimSubmittedColumnCode);
SetPropertyOrReportError((e) => currentRow.ExpenseCategory = e, ExpenseCategoryColumnCode);
SetPropertyOrReportError((e) => currentRow.ExpenseType = e, ExpenseTypeColumnCode);
SetPropertyOrReportError((e) => currentRow.Amount = e.Value, AmountColumnCode);
SetPropertyOrReportError((e) => currentRow.PayeeReference = e, PayeeReferenceColumnCode);
SetPropertyOrReportError((e) => currentRow.Detail = e, DetailColumnCode);
SetPropertyOrReportError((e) => currentRow.TransactionDate = e, TransactionDateColumnCode);
parsedItems.Add(currentRow);
我认为这是更好的,因为它表达了它的意图更好。但是,它仍然不是我希望的那样,我希望能够像这样调用该方法:
SetPropertyOrReportError(currentRow.ClaimantName, ClaimantNameColumnCode);
SetPropertyOrReportError(currentRow.CustomerName, CustomerNameColumnCode);
SetPropertyOrReportError(currentRow.CustomerProject, CustomerProjectColumnCode);
SetPropertyOrReportError(currentRow.ClaimDate, ClaimDateColumnCode);
SetPropertyOrReportError(currentRow.ClaimSubmitted, ClaimSubmittedColumnCode);