这个问题与技术无关,但我正在使用 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); } } } }
将日志记录、事务和异常处理委托给调用者:
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(); } }
我担心通过在我的业务逻辑代码中引入对日志记录、事务等的依赖,我会使其不那么通用。另一方面,UI 代码看起来干净多了。我不能打电话。让我知道我应该考虑哪些其他因素。