A fat controller is a presentation- or application-layer class that becomes a junk drawer for responsibilities that belong elsewhere. Instead of mainly handling HTTP requests, it may validate input, calculate totals, access the database, and send email. section 4.7.1
The pile-up has a cost: unrelated changes can break each other, tests become difficult to isolate, and tight coupling makes replacing infrastructure painful. High cyclomatic complexity can also signal that a method has too many paths. section 4.7.1
Listing 4.7 Before (Code That Works, But is a Ticking Time Bomb)
const createOrder = (req, res) => {
const request = req.body;
// 1. Validation Logic
if (!request.Items || request.items.length === 0) {
return res.status(400).send("Order must have items.");
}
// 2. Infrastructure
const dbContext = new MyDbContext(); #A
const customer = dbContext.customers.find(c => #B
c.id == request.customerId);
if (!customer) {
return res.status(400).send("Customer not found.");
}
// 3. Core Business Logic
let total = 0;
for (const reqItem of request.items) {
const dbItem = dbContext.items.find(i =>
i.id === reqItem.itemId);
if (!dbItem) {
return res.status(400).send(
`Item ${reqItem.itemId} not found.`};
}
total += dbItem.price * reqItem.quantity;
}
// 4. Hardcoded Business Rules (Applying Discount)
if (customerType === "Gold") { total *= 0.9; } // 10% discount #C
// 5. Anemic Model Usage & Persistence
const order = new Order();
order.id = Math.floor(Math.random() * 9000) + 1000;
order.total = total,
order.customerEmail = customer.email;
dbContext.orders.push(order);
dbContext.saveChanges();
// 6. External Service Logic (Hidden Side Effects)
const emailService = new SmtpEmailService();
emailService.Send(request.customerEmail, "Order Confirmed!");
return re.status(200).json({
orderId = order.id,
totalPrice = order.total,
customerEmail = order.customerEmail
});
}The order-creation example shows validation, database access, and business calculations accumulating in one controller. section 4.7.1
Keep HTTP concerns in the Presentation Layer, orchestration in the Application Layer, business rules in the Domain Model, and database or email work in Infrastructure. A rich domain model keeps behavior with the data it protects; automated architectural tests can help preserve these boundaries. section 4.7.4 section 4.7.2
