A clean, enterprise-grade implementation of the Layered (N-Tier) Architecture Pattern using Python and MySQL.
This project demonstrates how to structure a production-grade Python database application by strictly adhering to the Separation of Concerns (SoC) and Single Responsibility Principle (SRP).
- Presentation / Client Layer (
main.py): CLI entry point handling user I/O, prompts, and formatted feedback. - Business Logic / Service Layer (
service/employee_service.py): Business rule validation and workflow orchestration across all CRUD operations. - Data Access Object Layer (
dao/employee_dao.py): Query formulation,%sparameter binding, transactional demarcation (commit), and object-relational hydration (rowtuple toEmployeeentity). - Infrastructure Layer (
database/connection.py): Connection factory managing MySQL socket lifecycles and environment-based configuration. - Cross-Cutting Model Layer (
model/employee.py,model/product.py): Domain entities / DTOs transferred cleanly across boundaries without leaking relational database schemas.
- Create: Instantiates domain object, delegates through service, issues parameterized
INSERT, and commits transaction. - Read All: Executes
SELECT *, streams rows throughcursor.fetchall(), hydratively transforms tuples intoEmployeeobjects, and closes connections cleanly. - Search (Read by ID): Executes parameterized
SELECT ... WHERE id = %susing(id,)tuple, fetches single row viacursor.fetchone(), and handles null checks (None). - Update: Prepares
UPDATE ... WHERE id = %s, binds(name, salary, id), commits transaction, and evaluatescursor.rowcount. - Delete: Executes parameterized
DELETE ... WHERE id = %s, commits transaction, and verifiescursor.rowcount.
LayeredArchitecture_Main/
├── main.py # Entry point (Presentation Layer)
├── requirements.txt # Project dependencies
├── .env.example # Sample environment configuration
├── .gitignore # Git ignore rules
├── README.md # Project overview & quickstart
├── theory/ # Theory documentation & visual diagrams
│ ├── Architecture.md # Full architecture and theory guide
│ ├── generate_diagrams.py # Diagram generator script (Matplotlib)
│ └── images/ # 300 DPI architectural diagrams
├── model/ # Domain entities (DTOs)
│ ├── employee.py
│ └── product.py
├── service/ # Business logic & orchestration (Service)
│ ├── employee_service.py
│ └── product_service.py
├── dao/ # Database access, SQL CRUD & Hydration (DAO)
│ ├── employee_dao.py
│ └── product_dao.py
├── database/ # MySQL connection factory
│ └── connection.py
└── venv/ # Virtual environment
- Python 3.8+
- MySQL Server running locally
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtCreate the database and target table in MySQL:
CREATE DATABASE test;
USE test;
CREATE TABLE pdemployee1 (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
salary DECIMAL(10, 2) NOT NULL
);Set environment variables (optional, defaults to localhost/root):
cp .env.example .env
# Edit .env with your MySQL credentialspython3 main.pyFor a simple, step-by-step breakdown of your working code and the core theory behind it, refer to theory/Architecture.md:
- Part 1: Your Working Code (How It Runs): Clear step-by-step execution of all 5 CRUD operations.
- Part 2: Core Theory (Made Simple): Easy explanations of the restaurant analogy, DTOs, data hydration,
conn.commit(), and SQL security.




