Say we have a view page where users perform actions in multiple steps, some of these actions would involve DB operations with transactions (the snippets are in pseudo-code like from):
View 1:
Step 1:
...
Step 2:
...
Call ActionX
Step 3:
...
ActionX: # can be called from different points in the view
...
Begin Transaction
DB ops
End Transaction
Say we want to log the user progress in the view (or save some states) in a persistent manner in the DB, so that if the program/app is interrupted we can re-establish the state to the last user action inside a view:
View1:
Step 1:
...
Logger.log('view1-step1-done')
Step 2:
...
Call ActionX
Logger.log('view1-step2-done')
Step 3:
...
Logger.log('view1-step3-done')
Now ideally, we would want to have the persistent logging and the DB based transactional actions to be both done in the same DB transaction, because we don't want to create an inconsistent state where an action is performed but is not reflected in the log state, or vice versa.
The question is: how to achieve the said goal with minimum inter-dependency and SOC. Some suggestions for example:
View 1:
Step 1:
...
Step 2:
...
Begin Transaction
Call ActionX # begin/end transaction statements will be removed from ActionX
Logger.log('view1-step3-done')
End Transaction
Step 3:
...
Don't like this because the handling of transactions belongs naturally in the action itself.
Or, another one:
View 1:
Step 1:
...
Step 2:
...
Call ActionX (logMsg='view1-step3-done')
Step 3:
...
ActionX (logMsg):
...
Begin Transaction
DB ops
Logger.log(logMsg)
End Transaction
Don't like this one either. The log message handling doesn't belong in the actions.