terminal

codeando_simple

terminal

menu

terminal

search_module

guest@codeandosimple: ~/system/search $ grep -r "" .

Press [ENTER] to execute search

Status

Engine: Ready

Database: Online

Index: V2.1.0_LATEST

bash -- cat table-data-gateway.md
guest@codeandosimple: ~/enterprise/patterns $ cat table-data-gateway.md

Table Data Gateway_

// "Success usually comes to those who are too busy to be looking for it"

A Table Data Gateway is a design pattern that acts as an intermediary between the application and a database table.

Unlike the Row Data Gateway, which manages one row at a time, the Table Data Gateway provides methods to perform operations on the whole table set... such as retrieving multiple rows, inserting a new row, or deleting rows based on certain criteria.

This pattern encapsulates SQL queries and database operations, providing a simple API for business logic to interact with the database.

# When to use it?

This pattern is ideal for applications that need to perform table-level operations, such as searching for record sets, updating multiple rows at once, or inserting records without the need to manipulate individual entities.

# Pros

  • verified

    Simplicity

    Provides a simple interface to interact with the database, making the code cleaner.

  • verified

    Centralized Access

    Concentrates database operations in a single place.

  • verified

    Separation of Concerns

    Allows clearly separating data access logic from the rest of the application.

# Cons

  • warning

    Less Abstraction

    May not offer as many features or abstractions as an object-relational mapper.

  • warning

    Potential Duplication

    If various parts of the application perform similar operations, there could be duplication.

# Detailed Example in Java

Let's see how a Table Data Gateway could be implemented for a Persons table in a database:

PersonsGateway.java

public class PersonsGateway {
    // Database connection, omitted for simplicity

    public List<Person> findAll() {
        // Returns all people in the table
        return executeQuery("SELECT * FROM persons");
    }

    public List<Person> findByName(String name) {
        // Returns people matching the name
        return executeQuery("SELECT * FROM persons WHERE name = ?", name);
    }

    public void insert(String name, String email) {
        // Inserts a new person into the table
        executeUpdate("INSERT INTO persons (name, email) VALUES (?, ?)", name, email);
    }

    public void delete(int id) {
        // Deletes a person from the table by ID
        executeUpdate("DELETE FROM persons WHERE id = ?", id);
    }

    // executeQuery and executeUpdate methods would be implemented here...
}

# Conclusions

The Table Data Gateway provides a structured and efficient way to manage access to database tables, facilitating common operations without complicating the application design. Frameworks like .NET make common use of the Table Data Gateway.