ASP.NET - 在presentation层显示业务层的错误 [英] ASP.NET - displaying business layer errors in the presentation layer

查看:107
本文介绍了ASP.NET - 在presentation层显示业务层的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前在ASP.NET应用程序中我发展,基本验证(即必填项)正在在presentation层完成,使用验证器和的ValidationSummary。这是因为在ValidationSummary为我工作的伟大专门会显示多个错误消息(假设多重校验器设置为无效)。

Currently in the ASP.NET application I'm developing, basic validations (ie required fields) are being done in the Presentation Layer, using Validators and a ValidationSummary. This is working great for me specifically since the ValidationSummary will display multiple error messages (assuming multiple Validators are set to invalid).

我也有在业务层正在做一些验证 - 由于其复杂性(和数据服务层的依赖​​),我宁愿没有让他们在presentation层。但是,我不知道送这些回presentation层显示给用户的最佳方式。我最初的考虑是发回一个列表<串> 失败的验证信息,然后动态地创建一个CustomValidator控件(因为显然你只能绑定一次错误发送给一个验证控制)对于每个错误在ValidationSummary显示当有任何。

I also have some validations being done in the business layer - due to their complexity (and data service layer reliance) I'd rather not keep them in the presentation layer. However, I'm not sure the best way to send these back to the presentation layer for display to the user. My initial consideration is to send back a List<string> with failed validation messages and then dynamically create a CustomValidator control (since apparently you can only bind one error message to one Validator control) for each error to show in the ValidationSummary when there are any.

我假设我不是第一个遇到这个问题,所以我想知道如果任何人有这个任何建议。

I'm assuming I'm not the first one to come across this issue, so I'm wondering if anyone has any suggestions on this.

谢谢!

推荐答案

基本上有两种方法可以做到这一点:无论是从业务层传回错误code /对象,或者抛出异常。您也可以两者结合起来。

There are essentially two ways to do this: either by passing back an error code/object from your business layer, or throw out an exception. You can also combine both.

有关一个例子,你可以看看的SQLException类。当您发送SQL到SQL Server,它运行一个查询解析器解析首先你的SQL。如果看到语法错误,那么它会抛出一个SQLEXCEPTION并终止查询。有可能是在查询中多个语法错误。所以SqlExeption类有包含错误列表的Errors属性。然后,您可以通过在你的presentation层该列表中列举格式化你的错误信息,可能与一的CustomValidator。

For an example, you can take a look SqlException class. When you send a SQL to SQL Server, it runs a query parser to parse your SQL first. If it sees syntax error, then it will throw out a SqlException and terminate the query. There may be multiple syntax errors in your query. So SqlExeption class has an Errors property that contains a list of errors. You can then enumerate through that list in your presentation layer to format your error message, probably with a CustomValidator.

您也可以简单地只返回错误列表未抛出异常。例如,你可以有你的函数返回一个列表的情况下,至少有一个发生了错误,并在情况下返回null调用成功。或者你也可以通过列表作为参数给你的函数。他们都很好,这一切都取决于你觉得哪种方式更方便。抛出一个异常的优点是它会立即开卷多个呼叫帧,这样你就不必在各个层面检查返回值。例如,如果函数A调用函数B,B又调用函数C,C看到不对劲,那么如果让C返回一个错误对象(或错误code),则B必须有code,检查是否ç返回一个错误,并传递错误code /值回,A必须检查它,以及----你需要检查它在各个层面。在另一方面,如果你只是让C到抛出一个异常,那么code去直接异常处理程序。你不必在各个层面上检查返回值。

You can also simply just return the error list without throwing an exception. For example, you can have your function to return a List in case at least one error occurred and return null in case the call was successful. Or you can pass List as an argument into your function. They are all fine, it all depends on which way you feel is more convenient. The advantage of throwing out an exception is it unwinds multiple call frames immediately, so you don’t have to check return value on every level. For example, if function A calls function B , B calls function C, C sees something wrong, then if let C to return an error object (or error code), then B has to have code to check whether C returned an error and pass that error code/value back, A have to check it as well ---- you need to check it on every level. On the other hand, if you just let C to throw an exception, then the code goes straight to the exception handler. You don’t have check return values on every level.

这篇关于ASP.NET - 在presentation层显示业务层的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆