Home Projects Portfolio Dashboard Export PDF Log in
Java

Refactoring Controller Logic in Cine-App: A Step Toward Clean Architecture

In the world of Java development, our controllers often become the dumping ground for everything—business logic, data parsing, and even external service calls. I recently took a hard look at the controller layer in Cine-App, a project focused on movie management, and realized that my controllers were becoming increasingly bloated.

The Problem of Bloated Controllers

When a controller starts handling more than just the incoming request and the outgoing response, you hit a 'fat controller' bottleneck. In Cine-App, I noticed that the logic responsible for routing and state management had become tightly coupled with the application's underlying domain models.

This makes testing difficult. If you want to verify your business logic, you end up having to instantiate the entire web request context just to check a simple calculation or data transformation.

Refactoring the Workflow

To address this, I initiated a series of changes to the controller architecture. The goal was simple: decouple the request processing from the core business rules.

Here is what that looks like in practice:

// Before: Controller managing everything
@RestController
public class MovieController {
    public ResponseEntity<Movie> updateMovie(Long id, MovieData data) {
        // Business logic mixed with request handling
        if (data.isValid()) {
            // Database operations here
            return ResponseEntity.ok(movieService.save(id, data));
        }
        return ResponseEntity.badRequest().build();
    }
}

// After: Controller delegates to cleaner services
@RestController
public class MovieController {
    private final MovieProcessor processor;

    public ResponseEntity<Movie> updateMovie(Long id, MovieData data) {
        return ResponseEntity.ok(processor.execute(id, data));
    }
}

By pushing the logic into a dedicated processor class, the controller remains thin and focused. It now only cares about mapping inputs and translating service results into appropriate HTTP responses.

The Takeaway

Refactoring isn't just about deleting code; it's about shifting responsibilities. By thinning out the controllers in Cine-App, we gained two major benefits:

  1. Improved Testability: We can now unit test the logic without mocking entire HTTP Servlet contexts.
  2. Increased Readability: New team members can understand the API surface area at a glance without parsing lines of business logic.

When your controllers start growing, don't just add comments—add a layer of abstraction. Your future self will appreciate the clean separation of concerns.


Generated with Gitvlg.com

Refactoring Controller Logic in Cine-App: A Step Toward Clean Architecture
Gustavo Plaza

Gustavo Plaza

Author

Share: