The design question is how atomic should a business-level method be?

This problem is technological agnostic, but I am working with C # and ASP.NET and will use it for pseudocode. Which approach is better and why?

  • Encapsulation of registration, transaction processing and exceptions:

    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);
                }
            }
        }
    }
    
  • Delegation of registration, transaction processing and exceptions for 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 am making this less general. On the other hand, the interface code looks much cleaner. I can not call. Let me know what other factors I should consider.

+3
source share
3 answers

: -, ( ).

: . , - (a la ) - , .

: , - - ( ). , .

: , .

+5

:

protected void Page_Load(object sender, EventArgs e) {
 new SomeBusinessClass(_logger, _dbcontext, _exceptionhandler).SomeBusinessMethod();
}

protected void Page_Load(object sender, EventArgs e) {
  _mybusinessclass.SomeBusinessMethod();
}

_mybusiness IoC, _logger, _dbcontext _exceptionhandler. _exceptionhandler, "new RedirectExceptionHandler (this)",

protected void Page_Load(object sender, EventArgs e) {
  _mybusinessclass.SomeBusinessMethod(new RedirectExceptionHandler(this));
}

. , IoC ASP.NET, , MVC.

- Aspect Oriented Programming . ( www.sharparchitecture.net) - , [Transaction] .

+2

, UI , .

+1
source

Source: https://habr.com/ru/post/1723380/


All Articles