tags:

views:

45

answers:

1

I am writing an csv import engine for a system I'm working on and I wanted to avoid the kind of code that looks like (repeated for 10's of columns):

var field = GetFieldStringValue(columnCode);
if (IsValidDecimal(field))
{
    currentRow.Amount = Decimal.Parse(field);
}
else
{
    ReportError();
}

In the end I opted for a method that looks like this (with overloads for various types):

    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();
        }
    }

That way for each row I end up with code a bit like this:

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);

Which I think is better since it expresses its intent better. However, it's still not as I'd like it to be, I'd rather be able to call the method like this:

SetPropertyOrReportError(currentRow.ClaimantName, ClaimantNameColumnCode);
SetPropertyOrReportError(currentRow.CustomerName, CustomerNameColumnCode);
SetPropertyOrReportError(currentRow.CustomerProject, CustomerProjectColumnCode);
SetPropertyOrReportError(currentRow.ClaimDate, ClaimDateColumnCode);
SetPropertyOrReportError(currentRow.ClaimSubmitted, ClaimSubmittedColumnCode);

The problem I'm having is that I can't get my head around how to write an Expression that will take a property. How can I do this?

A: 

You can't, basically.

The expression currentRow.ClaimantName, when used as a method argument, will always represent the result of evaluating the getter for the property.

If we ever get the infoof operator, there might be a better approach - but for the moment, I think your lambda option is the best one. Note that you don't need the parentheses around the parameter though - for example:

SetPropertyOrReportError(e => currentRow.Detail = e, DetailColumnCode);
Jon Skeet