I am refactoring Python code that have 20 methods. Each of the method updates different database fields. All of these methods repeat some of the code that relates to opening database connection and committing changes to it. The parameters being passed to all of these methods have some common parameters and some not. The number of parameters being passed to these methods varies in numbers. Each of the method builds appropriate SQL command (search/query) for itself, formats its database name, thatcan vary, and opens a database connection. Some of the methods prototypes are listed below.
- updatelogintime(app, session, request, ...)
- disable_user_login(app, request)
- getPgdbTableStruct(app, tablename, session)
- getPgDbTables(app, session, userAdmin)
- getPgData(app, tablename, session, thisOffset, filterResponse, limitToMax)
- pg_delete(app, tablename, session, sqlCommand, theRow)
- pg_insert(app, tablename, session, theValues) ...
My goal is to simplify the code and avoid repeating some of the code in each method. I wonder if command pattern is OK to use or not.
I am not clear on what will be Invoker and Receiver object. Where does the common code goes (in base command or concrete command)? Do I place building of each of the sql command string code, which is unique per method, in a Receiver object class?
I am also wondering if there is another solution that is elegant and simple. Thanks.
Aucun commentaire:
Enregistrer un commentaire