DEV Community

๐Ÿ“šEnterprise Design Patterns: Table Module with Python Example

๐Ÿ“‘ Table of Contents

  1. Why Table Module Matters
  2. What is Table Module?
  3. Analogy: The School Office
  4. Python Example: Orders Table
  5. When to Use & When to Avoid
  6. Curiosities & Recommendations
  7. GitHub Repo + Automation
  8. Final Thoughts

๐Ÿ”ฅ Why Table Module Matters

In enterprise apps, business logic can live in different places:

  • In the database (stored procedures).
  • In domain models (one class per row).
  • Or in a table module: one class per table.

๐Ÿ‘‰ Table Module keeps all rules for a table in a single place, making code easier to read and maintain when logic is simple.


๐Ÿ“– What is Table Module?

Definition:

A single class that handles the business logic for all rows in a database table or view.

โš–๏ธ Difference with Domain Model:

  • Domain Model โ†’ 1 class per row.
  • Table Module โ†’ 1 class per table.

๐Ÿงฉ Analogy: The School Office

Imagine a school secretaryโ€™s office:

  • Instead of every teacher managing attendance (Domain Model),
  • The office manages attendance records for the whole school (Table Module).

๐Ÿ“Œ Centralized, consistent, and simple.


๐Ÿ’ป Python Example: Orders Table

โŒ Without Table Module (spaghetti logic)

import sqlite3

conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("CREATE TABLE orders (id INTEGER, customer TEXT, total REAL)")
cursor.executemany("INSERT INTO orders VALUES (?, ?, ?)", [
    (1, "Alice", 120.0),
    (2, "Bob", 80.5),
    (3, "Alice", 45.0)
])

# Logic spread everywhere ๐Ÿ˜“
cursor.execute("SELECT SUM(total) FROM orders WHERE customer='Alice'")
print("Alice total:", cursor.fetchone()[0])
Enter fullscreen mode Exit fullscreen mode

โŒ Problem: Logic is scattered in SQL queries all over the app.


โœ… With Table Module

import sqlite3

class OrdersTable:
    def __init__(self, connection):
        self.conn = connection

    def total_sales(self):
        cursor = self.conn.cursor()
        cursor.execute("SELECT SUM(total) FROM orders")
        return cursor.fetchone()[0]

    def sales_by_customer(self, customer):
        cursor = self.conn.cursor()
        cursor.execute("SELECT SUM(total) FROM orders WHERE customer=?", (customer,))
        return cursor.fetchone()[0]

# Setup DB
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
cursor.execute("CREATE TABLE orders (id INTEGER, customer TEXT, total REAL)")
cursor.executemany("INSERT INTO orders VALUES (?, ?, ?)", [
    (1, "Alice", 120.0),
    (2, "Bob", 80.5),
    (3, "Alice", 45.0)
])

# Usage
orders = OrdersTable(conn)
print("Total Sales:", orders.total_sales())
print("Alice Sales:", orders.sales_by_customer("Alice"))
Enter fullscreen mode Exit fullscreen mode

โœ… SRP: All business logic lives in OrdersTable.

โœ… Reusability: Centralized methods.

โœ… KISS: Simple & clean.


๐Ÿค” When to Use & When to Avoid

โœ… Use Table Module when:

  • Business rules are simple.
  • You need reports or aggregations.
  • Tables are the main unit of work.

โŒ Avoid when:

  • Each row has complex behavior.
  • You need polymorphism โ†’ prefer Domain Model.

๐Ÿง  Curiosities & Recommendations

๐Ÿ’ก Did you know?

  • Table Module was a stepping stone before modern ORMs like SQLAlchemy or Django ORM.
  • Many legacy apps still hide Table Modules inside stored procedures.
  • Fowler recommends it for reporting systems.

๐Ÿ‘‰ Pro tip: Use Table Module for data-centric apps, Domain Model for behavior-rich apps.


๐Ÿ“ฆ GitHub Repo + Automation

Repo structure:

enterprise-table-module/
โ”œโ”€ table_module.py
โ”œโ”€ tests/
โ”‚   โ””โ”€ test_table_module.py
โ”œโ”€ requirements.txt
โ””โ”€ .github/
   โ””โ”€ workflows/
      โ””โ”€ ci.yml
Enter fullscreen mode Exit fullscreen mode

requirements.txt

pytest==8.3.3
Enter fullscreen mode Exit fullscreen mode

.github/workflows/ci.yml

name: Python CI

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: '3.11'
      - run: pip install -r requirements.txt
      - run: pytest
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”— GitHub Repository Example


๐ŸŽฏ Final Thoughts

The Table Module pattern is a powerful yet underrated way to structure enterprise apps when logic is simple and table-oriented.

โœจ Remember:

  • Use it for reports and aggregations.
  • Switch to Domain Model when rules grow complex.

โœ๏ธ Your turn! Would you use Table Module in your next project?

Let me know in the comments ๐Ÿ‘‡

Top comments (0)