liveBook · Manning

Fat controller

As explained in Grokking Software Architecture

A fat controller is a controller design in which presentation-layer code accumulates excessive business logic and coordination responsibilities.

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

How Grokking Software Architecture covers it

How deep it goes
Explains mixed responsibilities and their costs through an order-controller example, then points to layer boundaries and automated enforcement.
Examples
Uses an order-creation controller that validates input, accesses a database, calculates totals, and sends email.
What you can do after
Diagnose mixed controller responsibilities and place HTTP, business, orchestration, and infrastructure concerns in their respective layers.
How it connects
Pairs the Fat Controller with the Anemic Domain Model and contrasts both with a Rich Domain Model.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.