简体   繁体   English

在三层系统中引发异常的地方

[英]where to throw exception in a 3 tier system

I currently have a system with the following structure: Website -> WCF -> Database 我目前有一个具有以下结构的系统:网站-> WCF->数据库

the WCF portion when called, calls the BLL which in turn calls Entity Framework to get the data it needs and sends back to Website. WCF部分在被调用时调用BLL,后者又调用Entity Framework以获取其所需的数据并发送回网站。 Currently I have putting my try catch statements in the WCF service, so if any error occurs in Entity Framework or in the BLL the service gets it and throws a FaultException back to the Website. 当前,我已将try catch语句放入WCF服务中,因此,如果在Entity Framework或BLL中发生任何错误,则该服务将其获取并向网站抛出FaultException。 The Website does not to any exception handling, I am using ELMAH so I just get an email with the error and the user is redirected to the error page. 该网站没有任何异常处理,我使用的是ELMAH,因此我只收到一封包含错误的电子邮件,并将用户重定向到错误页面。

Is this a good place to place the try catch blocks, or should they be in the BLL layer, or website as well? 这是放置try catch块的好地方,还是应该将它们放置在BLL层或网站中? I don't want to handle the errors, just let them get pushed up so the user gets the error page and I get an email with what occurred. 我不想处理错误,只是让它们被推高,以便用户获得错误页面,然后我收到一封包含发生情况的电子邮件。

Exception blocks shouldn't be placed only in a specific tier. 异常块不应仅放置在特定层中。 Exceptional case may happen in each tier, and each tier may handle if it's capable . 每一层都可能发生特殊情况,并且每一层都可以处理(如果有能力的话)

Anyway exceptions should be as "pleasant" as possible to the end user, and it seems that you covered this part good. 无论如何,对于最终用户而言,异常应该尽可能“令人愉悦”,并且看来这部分内容很好。

Most likely that the BLL tier should catch exceptions , if the Entity Framework code will throw one (which may happen), and there is no "need" to go back all the way to the WCF service (ie the BLL tier is capable of handling this exception). 如果Entity Framework代码将抛出一个异常 (可能发生),并且没有“需要”一直返回到WCF服务(即BLL层能够处理),则BLL层很可能应该捕获异常 。此例外)。

I asked the same question a couple of years back. 几年前,我问了同样的问题。

Place Try Catch in Business Logic or User Interface 将尝试捕获置于业务逻辑或用户界面中

Ideally, if you catch exception in Database layer, you want to throw to UI layer. 理想情况下,如果您在数据库层中捕获异常,则想扔到UI层。

In other words, you want to log exception in UI layer. 换句话说,您想在UI层中记录异常。 Let UI layer decide whether the exception message need to be displayed to user or just display generic error message. 让UI层决定是需要向用户显示异常消息还是仅显示一般错误消息。

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

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