0
我有一个数据表,并且必须验证其中的每个字段。我已经将此代码重构为以下代码,但复杂度为15(!!)我应该将字典的类型设置为Key并将Func设置为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; }
}
此问题似乎是题外话题,因为它是关于审查工作代码。尝试http://codereview.stackexchange.com。 – CodeCaster
_“我应该做类似字典的字典作为键和功能值?” - 是的,你可以做到这一点。 – CodeCaster