2

user1 が user2 によって直前にロードされたデータ レコードを削除できる asp.net mvc アプリケーションを開発しています。User2 は、この存在しないデータ レコードを変更 (更新) するか、外部キー制約に違反している別のテーブルにこのデータを挿入しています。

そのような予想される例外をどこでキャッチしますか?

asp.net mvc アプリケーションのコントローラーまたはビジネス サービスで?

補足: ここで SqlException をキャッチするのは、別のユーザーが特定の親レコードを削除したため、テスト計画を作成できないことをユーザーに伝えるために、ForeignKey 制約の例外である場合のみです。しかし、このコードはまだ完全には実装されていません!

コントローラー

  public JsonResult CreateTestplan(Testplan testplan)
  {
   bool success = false;
   string error = string.Empty;

   try
  {
   success = testplanService.CreateTestplan(testplan);
   }
  catch (SqlException ex)
   {
   error = ex.Message;
   }
   return Json(new { success = success, error = error }, JsonRequestBehavior.AllowGet);
  }

また

ビジネスサービス:

public Result CreateTestplan(Testplan testplan)
        {
            Result result = new Result();
            try
            {
                using (var con = new SqlConnection(_connectionString))
                using (var trans = new TransactionScope())
                {
                    con.Open();

                    _testplanDataProvider.AddTestplan(testplan);
                    _testplanDataProvider.CreateTeststepsForTestplan(testplan.Id, testplan.TemplateId);
                    trans.Complete();
                    result.Success = true;
                }
            }
            catch (SqlException e)
            {
                result.Error = e.Message;
            }
            return result;
        }

次にコントローラーで:

public JsonResult CreateTestplan(Testplan testplan)
      {
       Result result = testplanService.CreateTestplan(testplan);      
       return Json(new { success = result.success, error = result.error }, JsonRequestBehavior.AllowGet);
      }
4

1 に答える 1

5

Foreign key constraint violation should be checked and displayed properly. You can easily check if rows in related table exist and show proper message. The same can be done with row updates. Servers return number of rows affected, so you know what happens.

Even if you don't make these checks, you should catch SQL exceptions. For average application user, message about constraint violation means nothing. This message is for developer and you should log it with ELMAH or Log4Net library. User should see message similar to "We are sorry. This row has been probably modified by another user and your operation has become invalid." and in case he asks developer about it, developer should check logs and see the cause.

EDIT

I believe you should check errors in service. Controller should not be aware of data access layer. For controller, it doesn't matter if you store data in SQL database or in files. Files can throw file access exception, SQL has other ones. Controller shouldn't worry about it. You can catch data access layer exceptions in service and throw exception with type dedicated for service layer. Controller can catch it and display proper message. So the answer is:

public class BusinessService 
{
    public Result CreateTestplan(Testplan testplan)
    {
        Result result = new Result();
        try
        {
            using (var con = new SqlConnection(_connectionString))
            using (var trans = new TransactionScope())
            {
                con.Open();

                _testplanDataProvider.AddTestplan(testplan);
                _testplanDataProvider.CreateTeststepsForTestplan(testplan.Id, testplan.TemplateId);
                trans.Complete();
                result.Success = true;
            }
        }
        catch (SqlException e)
        {
            ....log in ELMAH or Log4Net using other logging framework...
            throw new ServiceException("We are sorry. Your operation conflicted with another operation in database. It has been cancelled.");
        }
        return result;
    }
}
于 2012-07-02T20:14:12.663 に答える