Click here to Skip to main content
Click here to Skip to main content

Improving the design patterns of web applications with AJAX

By , 27 Nov 2006
 

Contents

Introduction

Where does AJAX stand in our web application architecture? In this article, we talk about incorporating AJAX in our web application design patterns, and we see a different approach to some of the most commonly used scenarios - authentication and data binding - in web development.

Figure 1: A model and two views developed based on the AJAX-MVC pattern (see the Examples section)

Background

In the early days of the World Wide Web, the first release of HTTP (version 0.9) contained a single command: GET [filename]. There wasn’t really much into a web application: a collection of static HTML documents. Data and display styles were hard-coded into the page, and there was no defined approach to dynamically alternate the page data or style upon a request. To standardize the development of web pages later on, W3C introduced Cascading Style Sheets and HTTP 1.0 standards. These standards allowed for structured layout of HTML documents, and added headers to HTTP communications, which significantly improved the ability of web servers to manage web applications.

The growing demand for dynamic web pages caused server-side scripting to be widely implemented as we know it today. Web developers needed a technique to manipulate the content of the page and to embed their data and style before sending it out to the client. They knew how to do it in stand-alone applications; they used to display and format their data where the output was the computer screen or the printer. To do this in a web application, all they had to do was “render” an HTML version of their content and then hand it over to the web server to be sent to the client. In spite of all the improvements and new standards that have been introduced since then, this approach remains the underlying technique for web applications: rendering the pages at the server and flushing it to the client.

On other hand, to control the behavior of HTML document elements, different client-side scripting languages were created. In 1995, Netscape, who had already adopted Java, created the first version of JavaScript, called "LiveScript". Basically, it was a loosely typed scripting version of Java for both server-side and client-side scripting, which later became the ECMAScript standard. The server- side scripting ability of JavaScript never received that much attention but JavaScript still remains the main client-side scripting language to program the behavior of a web page in client browsers. However, JavaScript is mostly used to perform limited operations bounded to the page HTML elements, and never was a main player in our web application design – as a matter of fact, you could disable JavaScript and keep on using the web application - but, the wide spread use of AJAX has changed it all.

The MVC design pattern in web development

ASP.NET web application design pattern

To start, let us review how our web applications are structured in ASP.NET. By default, ASP.NET implements the “page controller” pattern, a variant of a popular OOP design pattern called the Model View Controller. [1]&[2]

Figure 2: Page Controller structure [1]

The implementation of the MVC design pattern by the Java web application framework, “Struts”, and ASP.NET has made MVC one of most commonly used design patterns in web development. As the "Elements of Reusable Object-Oriented Software" book has defined MVC [3], the reason why MVC has gained a great deal of popularity is because it offers decoupling of presentation (View) and data (Model). That is exactly what developers dreamed about for their web applications: modifying the data or the presentation of the web application without impacting the other layer. Additionally, with the MVC design pattern, you could have several Views for a Model or different Models for a single View.

Figure 3: MVC :A Model and three Views [3]

Nevertheless, this flexibility comes at a cost, which is that of tightly coupling the View-Controller and Controller-Model. The Controller loosely couples the View and the Model but itself is tightly coupled by either one. This means we cannot arbitrarily change the Model, View, and Controller unless we agree upon an interface at a higher level. Despite the great flexibility that MVC can offer, the complexity of implementing an MVC design pattern can be a slow down for most web development projects.

In a web application that is designed based on the MVC (Observer) pattern, when an event is fired at the page, the View class notifies the Controller class (code-behind). Based on the event, the Controller dispatches the event to other classes, retrieves or updates the Model, and notifies the View to be updated. In order to have this working properly, we need to implement a series of common interfaces on all Views and Controllers to enable them to interact with each other. This involves a lot of programming, not to mention that your controls will be specific to your project, and you will not be able to use third party controls (ASP.NET controls, for instance) without going through a lot of hard work.

Another approach to MVC pattern in web applications

At the same time that developers were wrestling with complex MVC implementations in their web applications, another approach silently achieved the advantages of MVC without suffering from its disadvantages.

I am sure all of us have seen the orange icon of RSS feeds here and there. Personally, I have seen CodeProject article feeds on many different websites, presented in many different formats, along with feeds coming from other sources.

Different Views for the same Model and one View for different Models; isn’t it what we wanted to achieve with the MVC design pattern? Well, if all we wanted to display in our web page were RSS feeds, the answer would have been yes, but the more important question is, can this be extended to cover all our application needs?

To answer this question, let us have a look at how RSS works. RSS (Rich Site Summary / Really Simple Syndication) is an XML document that heavily relies on metadata (data about data) [4]. RSS is built based on the Resource Description Framework (RDF). RDF is a component of Semantic Web projects started by W3C that promotes data should be presented in a way that can be processed by an automated software at a lower level before it is presented to a human being[5]. If you open an RSS document, there is little chance you are able to read it, but any RSS reader can easily display and update the RSS feeds for you in any presentation format that you would like.

Back to our web application, we couple our Views and Controllers with a technique similar to what RDF suggests. In this approach, our Views are smart enough to display the data returned by the Controller by reading the metadata, without being directly instructed on how data should be displayed. The Controller solely controls the data-driven events, and the details on how the data is displayed is decided by the View with no intervention from the Controller. In fact, each View has a JavaScript Controller, but the loosely typed-scripting nature of JavaScript makes it much more flexible. In other words, instead of re-rendering our pages on each call, we store the markup document and the style of our page in a static HTML file. With the help of AJAX, the markup document calls the Controller class and fills/updates its data. For example, in an email web application, the page itself does not change after being called by different users, but the embedded data of the page is what makes a user’s inbox different from the others. Whenever we need to update or add a new View, we update or add a new HTML document with zero impact on the controller.

Figure 4: A new approach to MVC with AJAX

Examples developed based on the AJAX-MVC approach: Authentication, DataGrid, Calendar, and more

In this section, I show you some examples of basic authentication and data binding with JavaScript prototype-based controls. This will help you to realize how efficient the new approach is compared to traditional server controls. All the control rendering is performed at the client, and the server load is dramatically decreased. I will add the source code to this article after I do a serious clean up and make it readable :) - the code is in JavaScript, so you should know how to see it anyway. Sometime in the future, in another article, we will talk about how to write JavaScript controls from scratch.

How to view these examples: Make sure you have the latest version of your browser. These examples are optimized for Firefox 1.5 +, IE 7.0 +, and Opera 9.0 or above. You can find the full collection of the samples at The Morning Outline.

Conclusion

Object oriented programming has become the methodology of choice for web applications. JSP, followed by ASP.NET, and recently PHP 5.x, have built in support for OOP implementations. OOP design patterns can offer great advantages such as re-usability, modularity, and maintainability, but it may not be obvious how our web application can benefit from these features.

"During the space race back in the 1960's, NASA was faced with a major problem. The astronaut needed a pen that would write in the vacuum of space. NASA went to work. At a cost of $1.5 million they developed the "Astronaut Pen". Some of you may remember. It enjoyed minor success on the commercial market.

The Russians were faced with the same dilemma. They used a pencil."-An old rumor with an important lesson.

Sometimes, we use our common sense for designing an application and neglect a defined design pattern. This negligence can cause significant over-structuring across the project and diminish OOP benefits, especially when we deal with large scale projects. In some scenarios, UpdatePanels in instant AJAX frameworks are an example of over-structuring in a web application, where the page goes through a complete lifecycle on each call, when in reality, it does not benefit our application.

References

Appendix

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shahin__
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAjax is becoming more popular.. [modified]membergggmarketing10 Sep '11 - 3:25 
Day by day, Ajax is becoming more popular. For making chat room, Collect data fast from a server there is no way without using ajax. Thanks for sharing this article.

GeneralUnable to download samplesmemberMember 33139537 Aug '08 - 6:47 
Hi Shahin__,
 
Great article, but I was unable to download the samples from the Morning Outline. How could i actually get the sample? Thanks
GeneralGood Workmemberrilov7 Jul '07 - 4:21 
Article is very nice..
 

GeneralThank youmembervolkan.ozcelik8 Dec '06 - 15:38 
Months ago I was thinking about A paradigm shift in MVC just like you.
 
I knew MVC conceptually but simply could not "get it". the strict nature of MVC and transient and stateless nature of the web was not quite fitting together in my mind.
 
I am from a OO/Java background and I believe I'm good add playing with objects, polymorhpism, encapsulation and all the stuff like that. However MVC is something deeper than OO.
 
And there was only one way to understanding it: **getting your hands dirty**.
 
Hence I've carved my thoughts into an actual web application.
 
I've read your article with great attention because it's explaining the things I was trying to discover some time ago in a more -how to say- comprehensive manner.
 
deja vu!
 
But the core idea is the same: This article demonstrates me, once again, the true way MVC should be implemented in contemporary web applications.
 
The idea is simple, stable and logical.
 
Thanks again for the article.
 

GeneralRe: Thank youmemberShahin__9 Dec '06 - 19:34 
I am happy that it was helpful for you. I wrote this article to link SOA (Service Oriented architecture) with what we already know about web development. I think it is much easier to move over to SOA from the familiar 3 tier architecture than introducing SOA as a new concept. SOA is the future of web development but unfortunately nowadays it is hard to find an article on SOA without getting overwhelmed by all the jargons that make it sound like it is something totally out of this world.
 
Cheers!
 
Shahin
 

 

 
Rebuild all succeeded!

Questionr u perspolice?membersubai4 Dec '06 - 18:06 
nice work thx but abiete Smile | :) Poke tongue | ;-P
 
I Wish the Life Had CTRL-Z

GeneralNice WorkmemberT.D. Ryan30 Nov '06 - 0:59 
Well done and well written article on MVC theory. I fully agree with your comments on Update Panel and full life cycle callbacks. Thanks...
 
BTW, have you built that browser yet?D'Oh! | :doh:
 
Go Noles!
GeneralGood start...memberJon Rista29 Nov '06 - 17:56 
You have a good start to an article here, but I think it could be fleshed out some more. The description of your AJAXified MVC is pretty light, and I'd like to see some more in-depth information on specific implementation details. It would be nice if the examples were packaged and downloadable, too, in case your site goes down or gets overloaded anytime in the future.
AnswerRe: Good start...memberShahin__29 Nov '06 - 20:30 
Hi Jon
I am planning to write a second article and dedicate it to the implementation of AJAX-MVC pattern. Honestly with such a short amount of time I got on my hands and considering the volume of material it takes there was no way to extend this article. There are some other points I want to add as well. For instance you can change the controller’s platform, or combine it e.g. using a combination of J2EE and .NET in the same application as long as the structure of metadata is preserved. I have a dream of converting this into a new platform- too bad I have a job to go to every day!
 
Here is the outline of the upcoming article, I saved it 3 days ago and still it is empty!
 
1 Introduction
2 Backgrounds
1.1 Server controls
1.2 Server controls lifecycle
1.3 XML Web services and Javascript controls
1.4 Comparison
3. Implementation
3.1 Introduction to Object Oriented JavaScript
3.2 implementing a sample control
3.2.1 Implementing the JavaScript control
3.2.2 Implementing an ASP.NET XML web service (the data controller class)
3.2.3 Implementing a JAX-WS web service (the data controller class)
3.2.4 consuming the web services in the Javascript control
4. Conclusion
 


 
Rebuild all succeeded!

GeneralRe: Good start...memberJon Rista30 Nov '06 - 14:21 
Ah, I completely understand the time thing. I'm always starting articles, only to finish them a month later, and finally proof-read and post them here another month later. :\ Its a vicious cycle. Wink | ;) I have three articles or article updates in the works right now...trying to figure out when I'll actually get around to posting them.
 

BTW, the topics for the next article look intriguing. Especially the OO javascript...I always love absorbing new oo javascript tricks, and it would be cool to see a javascript control.
GeneralThis is my take on AjaxmemberNirosh27 Nov '06 - 22:44 
Hi,
 
Ajax is getting famous and nice to see code project article are getting added explaining it..
 
But when I think of Ajax, I wonder why browser cannot build a way to communicate with the web server without refreshing the web page. I thought it is easier, than doing it on a higher layer like using a script..
 
That will standardize the technology, where with script there will have lots of issues on performance and effeciency. That will also stop the same development happen recursively at every other solution provider base.
 
I don't like AJAX, since it I think is not the optimum way of doing that ..
 
I mean,, I like the browser application to do the refreshing behind the seen, not a script runs on the browser to do it..
 
I like to hear a IE version X.0 is released with AJAX enabled.. so that the web developer does not have to do their home work by writing browser compatible complex java scripts. The browser himself load the page behind the seen and apply changes to the currently viewing web page on every request/ refresh..
 
In other word, You have the old page and new page and the browser himself compare the two and apply changes as appropriates.. no refreshing of the page .. the browser also indicate when it is processing something in the background, user can / should be able to make several request while some are being processed.. I think the future of AJAX will be much simpler, if browser application himself takes the burden than throwing it to us mean the web sites developers
 
That is my take on Ajax.. So let me see how you respond to this Smile | :)

 
L.W.C. Nirosh.
Colombo,
Sri Lanka.

AnswerRe: This is my take on Ajaxmemberefattal28 Nov '06 - 6:13 
From your point of view, what is the advantage of your imagined technology if the browser has to download the whole page just to compare the contents of the old and and the new page ? Confused | :confused: Why wouldn't it display directly the newx content ?
 
I don't think Ajax is just a way to avoid flickering. It is much more. When your application manipulates custom data and behaviour, the browser can't do very much for you, not more than if you need to control the exact behaviour of your controls.
 
On the other hand, many effective Ajax frameworks can help you not to rewrite the whole code.
 
Ajax is not a solution, it is a new way of thinking the web.
GeneralRe: This is my take on AjaxmemberNirosh28 Nov '06 - 16:27 
efattal wrote:
if the browser has to download the whole page just to compare the contents of the old and and the new page ? Why wouldn't it display directly the newx content ?

 
mm .. you got me..Cry | :(( That is the weaker most part of the post.. You are attacking exactly that, when I say page I would have said that the portion of the page that needed to be updated..
 
efattal wrote:
I don't think Ajax is just a way to avoid flickering.

 
I am not telling so either..
 

efattal wrote:
When your application manipulates custom data and behaviour, the browser can't do very much for you, not more than if you need to control the exact behaviour of your controls.

 
Incredibly wrong.. if the browser cannot do, explain me how a script will do??
 
efattal wrote:
On the other hand, many effective Ajax frameworks can help you not to rewrite the whole code.

 
On the other hand if browser is doing it, you don't need to write any code at all..
 
efattal wrote:
Ajax is not a solution, it is a new way of thinking the web.

 
Agreed, evolvements will happen step by step, but if you put your first step toward the wrong direction then... you know what happen
 
L.W.C. Nirosh.
Colombo,
Sri Lanka.

AnswerRe: This is my take on AjaxmemberShahin__28 Nov '06 - 9:15 
To your surprise, what you suggest is what the first generation of browsers (clients) looked like, and very soon they failed and they were replaced by W3C standard approach.
 
Cheers!
 
Rebuild all succeeded!

GeneralRe: This is my take on AjaxmemberNirosh28 Nov '06 - 16:32 
Shahin__ wrote:
what you suggest

 
Can you please elaborate a bit about the way you understood me..
 
Shahin__ wrote:
very soon they failed

 
That is how you see it, I see it as they created the second generation IMHO
 
L.W.C. Nirosh.
Colombo,
Sri Lanka.

AnswerRe: This is my take on AjaxmemberShahin__28 Nov '06 - 19:08 
I think you are confused about the concept of a web application. The browser is not supposed/ allowed to alter/delete/update the content of a web page without being programmed to do so. As the name implies, it “browses” the pages in a neutral state in respect to the behavior of the page. We as developers need a programming tool to program the behavior of the page to define a set of events that cause an update when certain conditions are satisfied. See what you said:
 

Nirosh wrote:
"I like to hear a IE version X.0 is released with AJAX enabled.. so that the web developer does not have to do their home work by writing browser compatible complex java scripts.”

 

How is this possible for IE to fix the cross-browser incompatibility issues not having control over other browsers behavior? Just to remind you, computing exists outside of IE and Windows. There are millions of people who even don’t know/ care what version IE is at. That is why we have web standards.
 
Cheers!
 
Shahin

 

 

 
Rebuild all succeeded!

GeneralRe: This is my take on Ajax [modified]memberNirosh28 Nov '06 - 19:32 
Shahin__ wrote:
I think you confused about the concept of a web application.

Correct me.. Smile | :) but with valid facts pls.
 
Shahin__ wrote:
The browser is not supposed/ allowed to alter/delete/update the content of a web Page without being programmed to do so.

Of course not, and show me a place where I said it..
 
Shahin__ wrote:
As the name implies, it “browses” the pages in a neutral state in respect to the behavior of the page.

Does it mean that browser should not do a certain level of tuning for better user experience.. as an example think of the gif download format option "Interlaced GIF[^]"
 
Shahin__ wrote:
We as developers need a programming tool to program the behavior of the page to define a set of events that cause an update when certain conditions are satisfied. See what you said:

Programmer should program the page, the point is well taken.. But not starting from Assembly.. today the programmers are provided with pre-built libraries.. and also framework and also application blocks.. why?? That is how you standardize the technology and also support rapid application development (which is the need today).
 

Shahin__ wrote:
How is this possible for IE to fix the cross-browser incompatibility issues not having control over other browsers behavior?

Not me, I think You are confused about what I am telling.. I left rest of the para since it is written based on a wrong assumption..
 
Again let me explain what I am telling here..
 
First Pls Refer To:http://www.google.com/webhp?complete=1&hl=en[^]
 
Here....
 
If you do this using Ajax
A script is detecting what user types and seamlessly a call is passed to the server side program where the server responds to it with some suggestions
 
If you do this using a program module inside the Browser
Programmer gets input box (with enhanced programmability) which they can program it in a way so that browser detect the key type event. There based on the way it was programed the browser made the call to a specific server side program (on behalf of the script) and let user see the suggestions
 
Hope this is clearer

 

 

-- modified at 1:40 Wednesday 29th November, 2006
 
L.W.C. Nirosh.
Colombo,
Sri Lanka.

GeneralRe: This is my take on AjaxmemberShahin__28 Nov '06 - 20:42 
If you were around quite long enough it would have been easier for you to understand why what you are suggesting failed years ago.
I tell you why. Let’s develop a browser based on what you said
 

Nirosh wrote:
If you do this using a program module inside the Browser
Programmer gets input box (with enhanced programmability) which they can program it in a way so that browser detect the key type event. There based on the way it was programed the browser made the call to a specific server side program (on behalf of the script) and let user see the suggestions

 
So the browser implements a module (class?) for every HTML elements – Back to 90’s MS vs. Netscape- First off what functionality would you add to each control? This could be a never ending list (=huge browser) but let’s say we have a an agenda and we develop a framework with our defined methods and properties – take it or leave it approach- Ok stop right here. This breaks the compatibility with every single website that does not like /know our framework and the web sites developed on our framework with other browsers. Well that’s "their problem" right? If people really started to moan and it bothered us we may look at other frameworks to find a common ground and take it to W3C to standardize it. We end up being where we are right now. The people who created NPAPI had your idea – much better presented- but it was never taken seriously in enterprise applications.
 
Cheers!
 
Shahin
 
Rebuild all succeeded!

GeneralRe: This is my take on AjaxmemberNirosh28 Nov '06 - 21:19 
You seems like a matured professional but in this case.. you are shooting off target. I can understand that some thing, which you think I am telling, has failed, but it is not what I am telling.
 
Shahin__ wrote:
The people who created NPAPI had your idea – much better presented- but it was never taken seriously in enterprise applications.

 
NAPI - Netscape Plugin Application Programming Interface, which is a plugin that handles certain content types (e.g. "audio/mp3"). How you relate this with my points. We are not discussing about displaying a certain content on the browser..
 
Here I am talking about a browser that does what Ajax Script is doing..
 
There is nothing complex over here, what you have to do is .. You need to identify an event that request a update to a specific segment or part of the web page and seamlessly talk to the server end and acquire the result and update the segment. This is not rocket science because if a script runs on the browser can do this, why cannot the browser himself do this? I guess the issue here is that browser vendors not willing to take the burden even though it is the optimum path.. LOL..
 
I think that I don’t need to explain how a script is doing this. It is used the browser API. There the complexity will come with AJAX as you have to write cross browser compatible complex scripts..

 
By the way what is this "Rebuild all succeeded!" all about? Big Grin | :-D
 
L.W.C. Nirosh.
Colombo,
Sri Lanka.

GeneralRe: This is my take on AjaxmemberJon Rista29 Nov '06 - 17:53 
I'd stop while your ahead, man. Wink | ;) Either Nirosh is somewhat young, or he is very, very ill-informed, but he doesn't seem to know what he's talking about. I'd let it rest before it spins out of control and other people get involved...could get messy (I have a few hard-ball retorts I'm trying very hard to keep under wraps myself right now). Smile | :)
GeneralRe: This is my take on AjaxmemberNirosh30 Nov '06 - 1:22 
Jon Rista wrote:
Either Nirosh is somewhat young

That make me very very angry.. A Man who is younger than me telling that I am very very young Mad | :mad: huh!!
 
Jon Rista wrote:
very ill-informed

Kinda feel so..
 
Jon Rista wrote:
he doesn't seem to know what he's talking about.

.. should consider this seriously
 
Jon Rista wrote:
I have a few hard-ball retorts I'm trying very hard to keep under wraps myself right now

OK..
 
Let me see.. I will run through AJAX again.. I gotta feeling that I am not understanding this correctly..
 
Lastly let me also says that those hints energize me.. Thanks for them too Cool | :cool:

 
L.W.C. Nirosh.
Colombo,
Sri Lanka.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 27 Nov 2006
Article Copyright 2006 by Shahin__
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid