π Table of Contents
- Why Table Module Matters
- What is Table Module?
- Analogy: The School Office
- Python Example: Orders Table
- When to Use & When to Avoid
- Curiosities & Recommendations
- GitHub Repo + Automation
- 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β¦
π Table of Contents
- Why Table Module Matters
- What is Table Module?
- Analogy: The School Office
- Python Example: Orders Table
- When to Use & When to Avoid
- Curiosities & Recommendations
- GitHub Repo + Automation
- 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])
β 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"))
β
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
requirements.txt
pytest==8.3.3
.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
π 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 π