Manages how an application interacts with its underlying data source, typically a database.
Aims to keep the software components cohesive while maintaining minimum coupling.
Each table
in the DB
has a Class
representing it.
Business model object interacts with the table data gateway class to store and retrieve data from the DB.
Only one instance
of Table Data Gateway can manage the data of all business model object instance of a specified type.
# Gateway interacts with the table Person_Table <--> Person_Gateway # Table/Object Person |-> lastName |-> firstName # Gateway PersonGateway |-> find(id) |-> findForCompany(companyId) # find all person in company, returns Person |-> update(id, lastName, firstName) |-> insert(id, lastName, firstName)
Implementation Code Example
class UserGateway: def find(self, user_id): result = db.query(f"SELECT * FROM users WHERE id = {user_id}") return result def find_all(self): result = db.query("SELECT * FROM users") return result def insert(self, name, email): db.execute(f"INSERT INTO users (name, email) VALUES ('{name}', '{email}')") def update(self, user_id, name, email): db.execute(f"UPDATE users SET name = '{name}', email = '{email}' WHERE id = {user_id}") def delete(self, user_id): db.execute(f"DELETE FROM users WHERE id = {user_id}")