Skip to main content

Eric Spiegelberg on Avoiding MVC Controller Bloat

Posted by editor on November 5, 2009 at 5:57 AM PST

We've just published Eric Spiegelberg's new article, "Using a Service Delegate to Avoid MVC Controller Bloat". Eric is a Minneapolis-based Java EE consultant. This is the sixth article he has contributed to java.net.

In his new article, Eric addresses the problem of "controller bloat" -- by which he means the tendency for code that does not belong in the controller in an MVC application to creep into it, as developers rush to get the job done, take what appear to be short cuts, and generally get lax about rigorously applying the principles of separation of concerns.

while the creators of frameworks and tools must invest a great deal of attention and focus on design, application developers can afford to be more lax. A classic example of the design gap is what I call controller bloat. In the case of web applications, controller bloat occurs when application developers violate separation of concerns and put non-web service code directly into their web MVC framework's controllers.

If you've been involved with software engineering for a while, say, at least five years, you're familiar with the problems that violation of separation of concerns creates. The code becomes difficult to read, difficult to maintain, and the possibility of its reuse is significantly diminished because multiple functions are embedded into, interwoven within, a single block of code. This situation can exist within the realm of pretty much any type of software development, not just in the Java MVC world.

Eric provides example code that illustrates the problem, using Spring MVC and a web MVC controller. It's not that the example code won't "work" -- sure, it would perform the function it was designed to perform. As Eric explains:

While the above code sample is functional, it does exhibit a major design problem: service code (i.e., your business logic) is unnecessarily located within the controller itself. The majority of the code within the handleRequestInternal method has nothing to do with the web tier and therefore violates separation of concerns. This leads to a handful of cascading disadvantages. Because the code lives in a class directly depending on the web tier, it can't be easily reused within non-web-based applications. Next, just as the code is difficult to reuse outside of a web-based application, it is difficult to unit test. While there is a plethora of options such as using mock objects or sophisticated strategies such as hot deploying code to running application servers and automated remote unit tests, that fact that your service coded is coupled to your presentation tier complicates both its testing and its reuse.

Eric goes on to discuss, and illustrate via code snippets, a solution: namely, creating a service delegate, so that the service code is extracted from the controller. This service delegate then becomes a reusable service, because it's now decoupled from your MVC controller. Furthermore, your web service tier itself gains flexibility:

Your MVC selection no longer needs to be a strategic or organization-wide decision because switching, or even using a separate MVC framework per application, is now trivial. Each application you create now has the freedom and flexibility to use the best framework for the given requirements and technical circumstances.

The thing about this situation that often trips up junior programmers is that mixing service code with web code doesn't necessarily seem inefficient, if you're just focussing on the specific task you need to get done today. But, a project lead or architect necessarily takes the longer-term view. Code written today isn't just for today. A year from now, you don't want to have to reinvent the same wheel you invented nine months ago and last week.

Eric cites additional benefits for the development team:

It doesn't scream for your attention, but another subtle benefit is that this design makes it easier for new team members or less experienced developers to join and contribute to your project. For a multitude of reasons, not all developers can jump head first into an existing system and make substantial contributions right away. By having your layers and your complexity cleanly separated, you can have a particular developer work on an individual area where they are strongest or most comfortable, while they gain experience with or knowledge of a different part of the system.

Eric concludes "Using a Service Delegate to Avoid MVC Controller Bloat" with:

While sample code demonstrating the design used Spring Web MVC for a Java based web application, the concepts presented universally apply to all types of applications as well as software created in any language for any platform. Because any time you emphasis good design, the end result is that your code will be easier to understand, develop, test, reuse, and maintain.


In Java Today, Java Champion Adam Bien wonders about the Sudden Death of Enterprise Projects?:

It seems like the huge, data driven, projects are either somehow completed, or were replaced by standard solutions. Especially in tougher times, even big enterprises are interesting in saving money and build software in more pragmatic ways. These are actually good news. Often (almost always) you can implement the same functionality with only a fraction of the originally planned resources, patterns, architecture, with a few passionated developers...

Joe Darcy posted Project Coin: Anatomy of adding strings in switch to javac:

Earlier this week, I happily pushed an implementation of Project Coin's title="Project Coin: Proposal for Strings in switch">strings in switch language feature into a repository being used for JDK 7 milestone 5. JDK 7 binaries with strings in switch will be available for downloading in due course. The javac compiler uses the standard compiler architecture of having successive phases and adding strings in switch required modifications to several links in the chain of phases...

The first PayPal developer's conference is now closed. Frank Sommers published an informative report on one of the key events from the confernce, in his article PayPal Releases Open Payment Platform and APIs:

PayPal is best-known as a provider of electronic payments for e-commerce Web sites. Lately, the company has been working on a developer platform and associated APIs that allow deep integration of sophisticated payment processing into applications as well. Consumer-facing applications are a special target of this platform, especially after Apple announced that it would allow embedded payment integration into iPhone applications. PayPal unveiled its new developer platform at its Innovate 2009 Developer Conference...


In today's Weblogs, I'm leading with two interesting posts from outside of java.net. The first is Geerjan Wielenga's Performance Management Analysis on the NetBeans Platform:

Yet another NetBeans Platform application is a commercial application offered by Exie, in Norway. "Exie provides People-Driven Performance Management solutions architected for widespread adoption by companies serving dynamic markets. Exie drives financial outcomes and accountability across the company and thus effectively involve the entire organisation in the overall strategy. By making performance management available to everyone across the organisation, you create an organisational culture continuously striving to improve performance." ...

Second is Kirill Grouchnikov's Control alignment in Substance 6.0:

Last week i have written about improving the visuals of text components, comboboxes and spinners in the 6.0dev branch of Substance look-and-feel library. Today, it's time to talk about the micro-design of these components - aligning perceived boundaries, text baseline and other visual elements of user input controls...

Jean-Francois Arcand says that he'll See you in Europe in November:

I'm will be leaving Québec next week for talks at JFall, Paris JUG, Prague JUG and Devoxx. The talks will focus on Atmosphere and it's spin off (Grail Plug In, Akka, PrimesFaces) and a little bit of Grizzly's work-in-progress Servlet Container. First, I will stop by Amsterdam November 11 and talk about Atmosphere at JFall 2009. Next I will stop by Paris JUG November 12 for an al-in-French talk on Atmosphere (I means pure French from Québec)...


In the Forums, dwsubc wondered about Possibility of no-render zones?: "Hi, I just started in java 3D and I am wondering if it is possible to set up 3D zones where all objects are invisible? For instance, any part of any object with a positive Y-coordinate would be invisible. I tried this with a BoundingBox..."

amit717077 has an XML Validation error - Schema validation with xsi:type ignoring Namespace: "I have xsd which has extension point and the extended xsd defines the derived type.
Now I created the xml file which has the type as ..."

And skja is seeing the jMaki feedreader widget not working out of box: "Any tutorial available for feedreader widget to make it work? I drag and drop the widget to my index.jsp and run it in the server and nothing happened. Any idea? Is there any thing to be updated in the following widget code ??? ..."


Our current Spotlight is the java.net ks2009 open source project: "KS - 2009®" is the world's 1st Karnaugh Map Solver for handheld devices. Karnaugh Maps are used to normalize complex digital circuits to reduce the requirements and complexity of hardware while implementing Digital Logic circuits. Mobile devices being so easy to access, this software will be a boon for all those digital circuit designers who are very often confronted with the problem to normalize complex digital circuits, and find normalization procedure using Karnaugh maps manually, very time consuming and difficult.


This week's java.net Poll asks What's your view of the JCP's role in guiding Java's future? The poll will run through Thursday.


Our current "(Not So) Stupid Questions" topic for discussion is Does Java Speak for Itself? It was suggested at Oracle OpenWorld that Java indeed does speak for itself. But, what does that statement mean? Does it have any truth? Register your view by posting a comment.

Our Feature Articles include Eric Siegelberg's Using a Service Delegate to Avoid MVC Controller Bloat, which describes how to maintain separation of concerns and avoid MVC controller bloat through the use of service delegates. We're also featuring a Java Tech guest column by Marina Kamahele: "Transparent" Panel - Mixing Heavyweight and Lightweight Components.


The latest Java Mobility Podcast is Java Mobile Podcast 90: Augmented Reality: Excerpts from the JavaOne 2009 Augmented Reality session with Kenneth Andersson and Erik Hellman of Sony Ericsson.


Current and upcoming Java Events:

Registered users can submit event listings for the java.net Events Page using our events submission form. All submissions go through an editorial review before being posted to the site.


Archives and Subscriptions: This blog is delivered weekdays as the Java Today RSS feed. Also, once this page is no longer featured as the front page of java.net it will be archived along with other past issues in the java.net Archive.

-- Kevin Farnham

O'Reilly Media

Related Topics >>