0

現在、アプリケーションのカスタムフィールドオブジェクトを検証する必要があります。簡単に言うと、各Fieldオブジェクトは、フィールドの検証に関する情報と、フィールドの値で構成されます。フィールドを一括で検証しているので、現在、検証クラスがあり、各検証のメソッドがあります。必須フィールドの場合、次のようになります。

      private void RequiredFields()
      {
            foreach (Field field in allFields)
            {
                if ((field.Required == true) && (field.Value == string.Empty))
                {
                    field.isValid = false;
                }
            }
      }

今私の問題は、検証を抽象化する必要があると感じていることです。したがって、次のように言う代わりに、

if ((field.Required == true) && (field.Value == string.Empty))

...検証クラスを追加して、値を受け入れ、これに変換します。

if (!validater.RequiredFields(field.Required, field.Value))

これを行うと、フィールドオブジェクトを使用せずに検証クラスを再利用できるようになり、ユニットテストも向上します...しかし、それは不必要な抽象化レイヤーのようであり、繰り返しもあります。 。覚えておいてください、これはすべての検証の中で最も簡単です。

提案?

4

1 に答える 1

2

独自の検証を担当する Field オブジェクトを作成しないのはなぜですか?

class Field
{
    public bool Required { get; }
    public string Value { get; set; }

    // assuming that this method is virtual here
    // if different Fields have different validation logic
    // they should probably be separate classes anyhow and
    // simply implement a common interface of inherit from a common base class.
    public override bool IsValid
    {
        get { return Required && Value != String.Empty; }
    }
}
于 2009-08-17T20:37:27.327 に答える