简体   繁体   English

如果搜索失败,我应该返回什么结果?

[英]What result I should return for unsuccessfully search?

I have a simple RESTFull service which can gets and search some post entities. 我有一个简单的RESTFull服务,可以获取和搜索一些帖子实体。 My project have three common layers (ORM, DAL, BLL). 我的项目有三个常见层(ORM,DAL,BLL)。 Should I use try/catch expressions in my controllers or I need to do it on lower layers such as DAL or BLL? 我应该在控制器中使用try / catch表达式,还是需要在较低层(如DAL或BLL)上执行此操作? I want to check executing of my operations in DAL layer and then check for null values in controllers in order to returns bad status code. 我想在DAL层检查我的操作的执行,然后检查控制器中的空值,以便返回错误的状态代码。 Is there a more elegant way for this? 这有更优雅的方式吗?

This is one of my methods: 这是我的方法之一:

[HttpGet]
public IHttpActionResult Details(int id)
{
    BllTask task;

    try
    {
        task = taskService?.GetById(id);
    }
    catch(Exception exc)
    {
        // change type of exception
        // handle it 
        // log it
        return InternalServerError();
    }

    return Json(task);
}

You should never use a try and catch statement in a controller, the controller should be very simple, also known as a thin controller. 你永远不应该在控制器中使用try和catch语句,控制器应该非常简单,也称为瘦控制器。 If the controller includes complex logic, which then requires a try and catch statement to wrap it, you are doing something wrong. 如果控制器包含复杂的逻辑,然后需要try和catch语句来包装它,那么你做错了。

See the following link for why a thin controller is important . 请参阅以下链接, 了解瘦控制器的重要性

The try and catch expressions should be used in the ORM, DAL and BLL layers, this is because you want to catch exceptions early, if there is enough context to make sense of the exception. 应该在ORM,DAL和BLL层中使用try和catch表达式,这是因为如果有足够的上下文来理解异常,您希望尽早捕获异常。

Then, either handle the exception if you can, or rethrow if required, in exceptional circumstances you can ignore the exception, rather than allowing exceptions unwind all the way to the controller, allowing your software to provide a reliable service. 然后,如果可以,则处理异常,或者在需要时重新处理,在特殊情况下,您可以忽略异常,而不是允许异常一直展开到控制器,允许您的软件提供可靠的服务。

For exceptions you decide not to handle, you should log them, as a comment in the code provided suggests you will, allowing you to review the logs to see if there is a reoccurring problem, and fix issues where required. 对于您决定不处理的异常,您应该记录它们,作为提供的代码中的注释,允许您查看日志以查看是否存在重复出现的问题,并在需要时修复问题。

See the following on when to catch early or late . 有关何时赶早或晚赶时间请参阅以下内容。

The presentation layer should then, either display a message saying no search results were found, as the response it got was the expected response, or if an exception unwinds all the way to the presentation layer, the presentation layer should display an error message. 然后,表示层应显示一条消息,表示没有找到搜索结果,因为它获得的响应是​​预期的响应,或者如果异常一直展开到表示层,则表示层应显示错误消息。

Update 更新

As you have now provided a code example, in the case of the code provided, you would be better off removing the try catch statement, it is pointless. 正如您现在提供的代码示例,在提供的代码的情况下,您最好删除try catch语句,这是没有意义的。 Instead, the following would have the same outcome, if the other layers followed the conventions mentioned in the links: 相反,如果其他层遵循链接中提到的约定,则以下内容将具有相同的结果:

[HttpGet]
public IHttpActionResult Details(int id)
{
    BllTask task = taskService?.GetById(id);
    return Json(task);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM