Filehelper differenciate errors based on Exception Message and manipulate record string

*爱你&永不变心* 提交于 2019-12-12 05:09:26

问题


I am using the FileHelpers library for identifying errors in processing csv file.

var engine = new DelimitedFileEngine<ModelClass>;
        engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;
        List<object> fileEngineResult = engine.ReadFile(fullFileName).ToList();
errorInfo = engine.ErrorManager.Errors;

My ModelClass has the properties corresponding to each column in my csv file with validation attributes

[DelimitedRecord(",")]
[IgnoreEmptyLines]

public class ModelClass
{
    [FieldNotEmpty]
    private string column1;       

    [FieldConverter(ConverterKind.Date, "MM/dd/yyyy")]
    private DateTime datecolumn;

    [FieldNotEmpty]
    private string column3;
}
  1. Now for me any error in the field not empty is critical hence I will have to deal with seperately.
  2. For a non critical error, in this case datecolumn, I need to replace wrong value with null.

I could achieve my 1st requirement using the below code( it is still a crude way)

 List<object> criticalErrorList = new List<object>();
List<object> softErrorList = new List<object>();
foreach (var error in errorInfo)
        {
if (error.ExceptionInfo.InnerException.ToString().Contains("The value is empty and must be populated"))
            {
                criticalErrorList.Add(error);
            }
            else
            {  error.RecordString = //replace recordstring for the error column with null;
                softErrorList.Add(error);
            }
        }
    }

Any idea to achieve 2nd requirement?


回答1:


Reading your question more carefully I see that want you really want is to get a null value when there is an error in your datecolum field.

I wrote this answer first and I'll let it here because I think you can benefit from it too. Maybe you could get a null value if field is missing and also get a null value if field data is wrong.

Initial answer (handling missing values with nullable types.)

Fortunately you don't need to change the record string (it's readonly), but you use something built-in in FileHelpers to achieve that. Change your model so the date column is a nullable DateTime.

[FieldConverter(ConverterKind.Date, "MM/dd/yyyy")]
private DateTime? datecolumn;

Another approach is if you would want to have a default value for the date column. Then you could use another attribute to decorate the datecolumn member:

[FieldNullValue(typeof(DateTime), "01/01/1900")]
[FieldConverter(ConverterKind.Date, "MM/dd/yyyy")]
private DateTime datecolumn;

Updated answer:

Get null value when data is wrong

Converters: This is another feature of FileHelpers. You can read more about it here:

So, let say you need to get a null value if the data is wrong for date column. You'll still need to change your datecolumn field to DateTime? but also you'll need a converter.

public class DateConverter : ConverterBase
    {
        public override object StringToField(string from)
        {
            //if you can't convert to date time.. .return null
            DateTime date;
            if (!DateTime.TryParse(from, out date))
                return null;

            return date;
        }

        public override string FieldToString(object fieldValue)
        {
            return ((DateTime) fieldValue).ToString("MM-dd-yyyy").Replace(".", "");
        }
    }

And in your model class, decorate the field with the converter:

[FieldConverter(typeof(DateConverter))]
public DateTime? datecolumn;

Now you'll get no erros when reading the file and if the date field is wrong in the file, you'll get a null value in the model class.

Hope this helps!



来源:https://stackoverflow.com/questions/39663351/filehelper-differenciate-errors-based-on-exception-message-and-manipulate-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!