The Row Data Gateway (sometimes written as Row Data Gateway) is a software design pattern used in object‑relational mapping (ORM) and data‑access layers. In this pattern, each instance of a class corresponds to a single row in a database table, and the object contains methods for retrieving, inserting, updating, and deleting that specific row. The pattern encapsulates the SQL statements needed for these operations within the object itself, thereby isolating database access logic from the rest of the application.
Key Characteristics
| Aspect | Description |
|---|---|
| Scope | One object per database row. |
| Responsibility | The object knows how to persist itself (CRUD operations). |
| Encapsulation | SQL queries are hidden inside the object's methods; callers interact with the object through its public interface. |
| Lifetime | Typically short‑lived; objects are created when data is fetched and disposed after use. |
| Coupling | Tightly couples the domain model to the relational schema, which can make schema changes propagate to the code. |
Relationship to Other Patterns
- Table Data Gateway: Operates at the table level, providing a single gateway class that handles all rows. Row Data Gateway works at a finer granularity, one object per row.
- Active Record: Similar in that objects carry persistence logic, but Active Record usually combines business logic with persistence, whereas Row Data Gateway focuses solely on data access.
- Data Mapper: Separates persistence logic from domain objects entirely; the Data Mapper translates between in‑memory objects and database rows, unlike Row Data Gateway where the object itself performs the translation.
Typical Use Cases
- Applications with relatively simple domain models where the overhead of a full Data Mapper is unnecessary.
- Situations where direct, row‑level manipulation is required, such as batch processing or reporting tools.
- Legacy systems that need a modest abstraction over raw SQL without extensive redesign.
Example (Pseudo‑code)
public class EmployeeRowGateway {
private long id;
private String name;
private String department;
// Constructor for a newly created row
public EmployeeRowGateway(String name, String department) {
this.name = name;
this.department = department;
}
// Load an existing row
public static EmployeeRowGateway findById(long id) {
ResultSet rs = executeQuery("SELECT * FROM employees WHERE id = " + id);
if (rs.next()) {
EmployeeRowGateway emp = new EmployeeRowGateway(
rs.getString("name"),
rs.getString("department")
);
emp.id = rs.getLong("id");
return emp;
}
return null;
}
public void insert() {
executeUpdate("INSERT INTO employees (name, department) VALUES ('"
+ name + "', '" + department + "')");
this.id = getGeneratedKey();
}
public void update() {
executeUpdate("UPDATE employees SET name = '" + name
+ "', department = '" + department + "' WHERE id = " + id);
}
public void delete() {
executeUpdate("DELETE FROM employees WHERE id = " + id);
}
// Getters and setters omitted for brevity
}
In this example, each EmployeeRowGateway instance represents a single row in the employees table and provides methods to insert, update, or delete that row.
Advantages
- Clear mapping between object and database row.
- Encapsulation of SQL, reducing duplication of query strings across the codebase.
- Ease of understanding for developers familiar with relational databases.
Disadvantages
- Tight coupling to the database schema; schema changes may require changes to many classes.
- Limited expressiveness for complex domain logic, as the pattern focuses on data access rather than business behavior.
- Potential performance impact if many row objects are instantiated individually rather than using batch operations.
See Also
- Table Data Gateway
- Active Record pattern
- Data Mapper pattern
- Repository pattern
- Object‑relational mapping (ORM)