1

現在、プロジェクトのリファクタリングを行っていますが、よくわからないことの 1 つは、ビジネス検証エラーの処理方法です。

現在、xVal ライブラリの RulesException クラスを使用していますが、このライブラリは開発されていないので、それに応じてコードを更新する必要があると思います。ドメイン モデルでのメソッドの例を次に示します。

 public virtual void AddComment(ProfileComment comment)
        {
            // ensure account is active before sending a message
            if (comment.FromAccount.AccountStatus != Enums.Common.AccountStatus.Active) throw new RulesException("", "Your account is not active");

            // throw exception if user has blocked this user
            if (HasUserBeenBlocked(comment.FromAccount)) throw new RulesException("", "User has blocked this action being performed.");

            TotalComments++;
            Comments.Add(comment);
        }

次に、次のようにコントローラーレベルでこの例外をキャッチします。

// process adding new comment
        try
        {
            accountTasks.PostProfileComment(user, account, profileComment);
        }
        catch (RulesException ex)
        {
            ex.AddModelStateErrors(ModelState);
        }

        if (!ModelState.IsValid)
        {
            return Json(ModelState.Errors());
        }

代替として何をお勧めしますか、それとも私が持っているものに固執することをお勧めしますか?

4

1 に答える 1

1

あなたと同じように、私たちは MVC3 と xVal の削除に備えたいと考えていました。独自の RulesException、ErrorInfo クラス、およびフレームワークに追加された拡張メソッド AddModelStateErrors を作成することで、依存関係を削除しました。

このように、xVal を削除して名前空間を解決するだけで済みます。リファクタリング ツールがあれば、これは簡単です。

私はこれらのクラスの要点を持っています。

于 2011-06-25T18:13:45.020 に答える