Home
06 Exercise: Architectural Layer Classification
January 2026 (858 Words, 5 Minutes)
exercises
chapter-06
architecture
layered-architecture
separation-of-concerns
Introduction
In a layered architecture, code is organized into horizontal layers where each layer has a specific responsibility:
- Presentation Layer: User interface, HTTP routes, UI callbacks, HTML/JSON rendering
- Business Layer: Domain logic, validation rules, calculations, business entities
- Data Access Layer: Database queries, repositories, object-relational mapping
- Database: Persistent storage (tables, schemas)
Learning Objectives:
- Identify which architectural layer a code snippet belongs to
- Understand the responsibilities of each layer
- Recognize patterns that indicate layer membership
- Justify layer classification decisions
Instructions:
- Study the Layer Responsibilities reference
- Read each code snippet carefully
- Classify each snippet by layer and justify your answer
- Check your answers against the solution
Total Points: 16 Time: ~15 minutes
Reference: Layer Responsibilities
| Layer | Responsibility | Typical Code |
|---|---|---|
| Presentation | Display data, handle user input | Routes, templates, callbacks, HTML |
| Business | Domain logic, rules, calculations | Services, validators, domain models |
| Data Access | Persistence operations | Repositories, SQL queries, ORM |
| Database | Data storage | Tables, indexes, schemas |
Key principle: Dependencies should only point downward (Presentation → Business → Data Access → Database).
Code Snippets
Study the following Python code snippets carefully.
# Snippet A
def validate_email(email: str) -> bool:
return "@" in email and "." in email.split("@")[1]
# Snippet B
@app.route('/users/<id>')
def get_user(id):
user = user_service.find(id)
return render_template('user.html', user=user)
# Snippet C
def save_user(user: User) -> None:
cursor.execute("INSERT INTO users VALUES (?, ?)", (user.id, user.name))
# Snippet D
def calculate_discount(order: Order) -> float:
if order.total > 100:
return order.total * 0.1
return 0.0
# Snippet E
def render_order_summary(order: Order) -> str:
return f"""
<div class="order">
<h2>Order #{order.id}</h2>
<p>Total: €{order.total:.2f}</p>
</div>
"""
# Snippet F
class OrderRepository:
def find_by_customer(self, customer_id: str) -> list[Order]:
rows = self.db.execute(
"SELECT * FROM orders WHERE customer_id = ?",
(customer_id,)
)
return [Order(**row) for row in rows]
# Snippet G
@dataclass
class Order:
id: str
customer_id: str
items: list[OrderItem]
@property
def total(self) -> float:
return sum(item.price * item.quantity for item in self.items)
# Snippet H
@callback(Output('chart', 'figure'), Input('dropdown', 'value'))
def update_chart(selected_id):
data = service.get_analysis(selected_id)
return create_figure(data)
Task: Classify Code Snippets (16 points)
For each code snippet (A-H), identify which architectural layer it belongs to and provide a brief justification (1-2 sentences).
Your Answer:
| Snippet | Layer | Justification |
|---|---|---|
| A | ||
| B | ||
| C | ||
| D | ||
| E | ||
| F | ||
| G | ||
| H |
(1 point per correct layer, 1 point per valid justification = 16 points total)
Solution
Show Solution
Correct Answers
| Snippet | Layer | Justification |
|---|---|---|
| A | Business | Validation logic with no UI or database concerns. Pure business rule for email format. |
| B | Presentation | HTTP route handling with @app.route decorator; renders a template for display. |
| C | Data Access | Direct SQL execution (INSERT statement) for persisting data. |
| D | Business | Domain calculation logic (discount rules based on order total). |
| E | Presentation | HTML generation/rendering; creates visual output for the user. |
| F | Data Access | SQL query execution with object mapping (rows → Order objects). Repository pattern. |
| G | Business (Domain Model) | Entity class with embedded business logic (total calculation using items). |
| H | Presentation | UI callback handling (Dash framework); coordinates between UI input and service. |
Grading Notes
Layer Classification (8 points):
- Award 1 point for each correctly identified layer
- Common mistakes:
- Snippet A: Students may say “Data Access” because it processes data, but it’s validation logic (Business)
- Snippet G: Students may say “Data Access” because it’s a dataclass, but it contains business logic (total property)
Justification (8 points):
- Award 1 point for each valid justification that:
- Correctly identifies the key characteristic placing it in that layer
- Shows understanding of layer responsibilities
- Award 0.5 points for partially correct justifications (right direction but vague)
Alternative acceptable justifications:
| Snippet | Alternative Justifications |
|---|---|
| A | "Contains input validation rules" |
| B | "Web framework route that handles HTTP requests" |
| C | "Directly interacts with database via SQL" |
| D | "Implements pricing/discount business rules" |
| E | "Generates user-facing HTML output" |
| F | "Encapsulates database queries in repository class" |
| G | "Domain entity with calculated property" or "Core business object" |
| H | "Handles UI events and triggers service calls" |