I have a data table and have to validate every field in it. I have refactor this code to this below, but the complexity is 15(!!) Should I make something like dictionary with type as Key and Func as Value?
private bool CheckField(DataRow dataRow, ValidationField validationField)
{
bool result = false;
if (validationField.Requiered)
{
if (validationField.Type == typeof (int))
{
result = this.CheckIntegerAndNotNull(dataRow[validationField.Name].ToString());
}
else if (validationField.Type == typeof (DateTime))
{
result = this.CheckDateTimeAndNotNull(dataRow[validationField.Name].ToString());
}
}
else
{
if (validationField.Type == typeof (int))
{
result = this.CheckIntegerOrNull(dataRow[validationField.Name].ToString());
}
else if (validationField.Type == typeof(DateTime))
{
result = this.CheckDateTimeOrNull(dataRow[validationField.Name].ToString());
}
else if (validationField.Type == typeof(string))
{
result = this.CheckStringOrNull(dataRow[validationField.Name].ToString(),
validationField.MaxLength.Value);
}
else if (validationField.Type == typeof(decimal))
{
result = this.CheckDecimalOrNull(dataRow[validationField.Name].ToString());
}
}
return result;
}
public class ValidationField
{
public Type Type { get; set; }
public string Name { get; set; }
public bool Requiered { get; set; }
public int? MaxLength { get; set; }
}