Monday, April 6, 2009

Applying MVC Correctly: Where does it fit?

When someone talks about Presentation layer patterns, the first pattern that comes to mind is MVC (Model-View-Controller). Although MVC might be well known in its name, however, I found out that it is a little vague in applicability and understanding to a lot of developers.

Who Am I? Where do I belong?
Is MVC and architectural style or just a composite design pattern? MVC is misapplied and misplaced in the application stack. MVC is a full application architecture style and not confined to the Presentation layer as I often see. If you think about it, MVC seems to be an early evolution of the ubiquitous three-layered architecture. The Model is the "data access layer", the Controller is the "business logic layer" and the View is the "presentation layer".

The cleanest application for MVC (Passive) that I've seen thus far and agree with is in Web Applications (ISAPI filters, HTTP handlers, ASP MVC) and not Rich Internet Applications or Rich Clients.

This is because the gist of MVC that makes it different from the Layered Architecture style is the emphasis that the Controller is the interceptor of requests. I think MVC is the perfect fit as a Web Application architctural style because the controller intercepts the call and creates the appropriate web page to be sent back to the client. This is because in Web Applications, and I don't mean here rich internet applications, the request is sent to the server to send back the UI with the data.

Other than that, especially in Rich Clients, I don't recommend the MVC architecture style. It is a misfit. In almost every rich client implementation of MVC that I've seen, it was always misunderstood and confined to the presentaion layer. I could not find one rich client implementation that was the canonical MVC. When I ask the designers about their implementation they would always say this is MVC but with this modification or that twist...

It is In the Interceptor
A lot of ambiguity has surrounded MVC on rich clients that many variants had to evolve such as Active MVC, MVP, Supervising Controller MVP etc... In my opinion, the main reason for that is because in rich clients there's a paradigm shift. Whereas in the canonical MVC, the Controller is the interceptor of user requests, here, in rich clients, it is the view that is the interceptor. A major architectural driver shift which calls for a different style.

In addition, with the advent and stabilization of the more mature Layered and SOA Architecture style, MVC seems redundant, a misnomer or out of place. It only serves the purpose to emphasize that there's an interceptor here and it is the controller. In the absence of a Service layer, some people find this useful.

Presentation Layer Architectural Pattern
Instead of trying to force MVC on rich client application and rich internet applications, in my next blog, I will put together an architectural pattern for the Presentation layer that is optimized for the following quality attributes in this order:

1- Maintainability
2- Reusability
3- Testability
4- Usability