Pas*_*cal 2 asp.net-mvc exception-handling
我正在开发一个asp.net mvc应用程序,其中user1可以删除user2之前刚刚加载的数据记录。User2更改此不存在的数据记录(更新),或者在违反了外键约束的另一个表中对此数据进行插入。
您在哪里捕捉到这种预期的例外?
在asp.net mvc应用程序的控制器中还是在业务服务中?
只是一个旁注:如果它的ForeignKey约束异常告诉用户另一个用户已删除某个父记录,因此他不能创建测试计划,则仅在此处捕获SqlException。但是此代码尚未完全实现!
控制器:
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);
}
Run Code Online (Sandbox Code Playgroud)
要么
商业服务:
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;
}
Run Code Online (Sandbox Code Playgroud)
然后在控制器中:
public JsonResult CreateTestplan(Testplan testplan)
{
Result result = testplanService.CreateTestplan(testplan);
return Json(new { success = result.success, error = result.error }, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
应该检查并正确显示违反外键约束的行为。您可以轻松检查相关表中的行是否存在并显示正确的消息。行更新也可以这样做。服务器返回受影响的行数,因此您知道会发生什么。
即使不进行这些检查,也应捕获SQL异常。对于普通应用程序用户,有关约束违反的消息毫无意义。该消息是针对开发人员的,您应该使用ELMAH或Log4Net库进行记录。用户应该看到类似于“很抱歉。此行可能已被其他用户修改,并且您的操作已变为无效”的消息。如果他向开发人员询问,开发人员应检查日志并查看原因。
编辑
我相信您应该检查服务中的错误。控制器不应该知道数据访问层。对于控制器,将数据存储在SQL数据库还是文件中都没有关系。文件会引发文件访问异常,SQL还有其他异常。管制员不必为此担心。您可以在服务中捕获数据访问层异常,并使用服务层专用类型抛出异常。控制器可以捕获它并显示正确的消息。因此答案是:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1662 次 |
| 最近记录: |