c# - 没有记录 - 抛出异常的地方

标签 c# asp.net-mvc architecture business-logic-layer

我有业务逻辑层和数据库层( Entity Framework )。例如,我从 DB 接收了一些数据。

数据库层:

public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
    SmartphonePhotographerResponseManage response = (from i in db.SmartphonePhotographerResponses
                                                     where i.RequestID == RequestID
                                                     select new SmartphonePhotographerResponseManage()
                                                     {
                                                         ResponseID = i.ID,
                                                         FormattedAddress = i.EditorialPixlocateRequest.FormattedAddress
                                                     }).FirstOrDefault();
    return response;
}

BL 层(这是最简单的例子,BL 层的意义——只是将结果从数据库“扔”到客户端(在我的例子中是 ASP.NET MVC,但没关系。当然,BL 方法可以有任何额外的逻辑) :

    public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
    {
        return _repository.ResponseManage(RequestID);
    }

它工作正常。但是如果记录不存在,我想抛出我自己的异常(即记录被删除,但用户有指向他的书签的链接):

public class RecordNotFoundException<T> : Exception
{
    public RecordNotFoundException(T ID) : base(String.Format("No Records for passed ID={0}", ID.ToString()))
    {
    }
}

我有 2 种 throw 方式: 1. DB层:

public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
    SmartphonePhotographerResponseManage response = (from i in db.SmartphonePhotographerResponses
                                                     where i.RequestID == RequestID
                                                     select new SmartphonePhotographerResponseManage()
                                                     {
                                                         ResponseID = i.ID,
                                                         FormattedAddress = i.EditorialPixlocateRequest.FormattedAddress
                                                     }).FirstOrDefault();
    if (response == null)
        throw new RecordNotFoundException<int>(RequestID);
    return response;
}

或者在BL层:

public SmartphonePhotographerResponseManage ResponseManage(int RequestID)
{
    var response = _repository.ResponseManage(RequestID);
    if (response == null)
        throw new RecordNotFoundException<int>(RequestID);
    return response;
}

然后在客户端(例如 ASP.NET MVC 的 Controller )捕获此异常并以适当的方式处理它。两种方法都可以,但抛出此类异常的位置在哪里更合乎逻辑?

编辑: 另外,当我想编辑/删除记录时,很难在 BL 中抛出这个异常。 IE。我有代码:

public async Task AcceptOrDeclineFileAsync(int ElementID, bool accept, string smsSid)
{
    var element = (from i in db.SmartphonePhotographerResponseElements where i.ID == ElementID select i).FirstOrDefault();
    if (element == null)
        throw new CommonLibrary.RecordNotFoundException<int>(ElementID);

    element.ApprovedByEditorial = accept;
    element.SmsSID = smsSid;
    await db.SaveChangesAsync();
}

如果我不在 DB 层抛出异常,我会在 BL 中得到常见的异常类型(我假设是 NullReferenceException)。也许,这就够了?任何其他情况,我什么时候可以获得 NullReferenceException?

最佳答案

我会将异常放入您的业务逻辑错误中。数据库层应该简单地报告它返回的内容;在这种情况下什么都没有。这只是一个问题,因为您的业务逻辑需要它。因此,将错误抛在那里而不是在较低级别。

关于c# - 没有记录 - 抛出异常的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487031/

相关文章:

c# - 我可以通过编程方式更改我发送的电子邮件中的字体和字体大小吗?

c# - 列表正在复制同一行

asp.net-mvc - FormsAuthentication.SetAuthCookie

architecture - 记录事件驱动的架构

azure - Azure 中多个部署环境之间的相互通信策略

c# - 覆盖 GetHashCode()

c# - 从 Dapper QueryMultiple 中的第一个选择访问结果(多对多)

c# - 错误 : your connection to this site is not private Asp. 网络。 MVC5

c# - 在数据表的特定列下添加一行

database - 基于位置的水平可扩展约会应用程序数据库模型