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

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 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

Related Topics >>