cdo*_*ner 3 c# asp.net business-logic
这个问题与技术无关,但我正在使用C#和ASP.NET,并将其用于伪代码.哪种方法更好,为什么?
封装日志记录,事务和异常处理:
protected void Page_Load(object sender, EventArgs e) {
SomeBusinessClass.SomeBusinessMethod();
}
public class SomeBusinessClass {
public void SomeBusinessMethod() {
using (TransactionScope ts = new TransactionScope()) {
doStuff();
ts.Complete();
}
catch (Exception ex) {
LogError("An error occured while saving the order", ex);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)将日志记录,事务和异常处理委派给调用者:
protected void Page_Load(object sender, EventArgs e) {
using (TransactionScope ts = new TransactionScope()) {
try {
SomeBusinessClass.SomeBusinessMethod();
ts.Complete();
}
catch (Exception ex) {
LogError("An error occured while saving the order", ex);
}
}
}
public class SomeBusinessClass {
public void SomeBusinessMethod() {
doStuff();
}
}
Run Code Online (Sandbox Code Playgroud)我担心通过在业务逻辑代码中引入日志记录,事务等依赖关系,我会使它不那么通用.另一方面,UI代码看起来更清晰.我不能打电话.让我知道我应该考虑的其他因素.
事务:业务层的核心问题,因此它应该绝对处理(尽管您可以通过工作单元实现集中事务处理).
更新:我不再同意这一部分.通常,控制器,演示者或其他顶级调用者是处理事务的最佳位置(在洋葱架构中) - 在许多情况下,这是定义逻辑工作单元的地方.
异常处理:在每个层中根据需要使用 - 但只有在您可以实际执行某些操作时才在业务层中使用它(而不仅仅是记录它).如果您的基础结构支持,则在UI层中使用全局处理程序.
记录:在需要的任何层中使用跟踪或信息记录,仅记录顶层的异常.