This issue is technology agnostic, but I am working with C# and ASP.NET and will use this for the pseudo code. Which is the better approach, and why?
Encapsulate logging, transaction and exception handling:
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); } } } }
Delegate logging, transaction and exception handling to the caller:
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(); } }
I am concerned that by introducing dependencies on logging, transactions, etc in my business logic code, I make it less generic. On the other hand, the UI code looks so much cleaner. I can't make the call. Let me know what other factors I should consider.