Saturday, April 19, 2008

How JavaScript Stirs Up The Three-Tier Architecture

A Place for Everything and Everything in it's Place

When it comes to designing web applications I thought I knew where everything should go. I thought that business logic should go in the "logic tier", and that user interface stuff should go in the "presentation tier", and that the data would be stored in the "data tier" (see three-tier Architecture). If I had been asked, "Where does JavaScript fit into all this?", I would have said that it is useful for providing basic client-side validation a la, "Customer name cannot be blank.".

Enter JavaScript

Within in the last six months or so, I've discovered two things that have shaken up my three-tiered perspective.

First, JavaScript (or at least the way it is being used) has matured quite a bit. Now those who have been following JavaScript carefully for a while will be able to calculate how deep my head has been in the sand from this last statement.

Second, a number of powerful JavaScript libraries/toolkits have been produced (e.g. JQuery, Dojo, Script.aculo.us, Prototype, GWT, etc.). I'm sure that some of these libraries have been around for a long time, but I am only discovering them now. And I'm having a great time playing with them.

The Power of JavaScript

These JavaScript libraries go a lot further than just telling the user that the "Customer name cannot be blank." They provide serious event handling, cool effects on top of JavaScript's ability to create objects with methods and properties. With all of these features, you could potentially move all three tiers of the three-tier architecture into the browser client. At least you could if you don't mind the total lack of persistence. If you want persistence, then you do have to communicate with a server (unless you find that storing your data in cookies works for your application).

Where Does Everything Go Now?

So now I'm confused. I like being able to split things up into pieces and then put the pieces somewhere. If I try to split up my web application according to its tiers, I find that JavaScript can play in all three tiers although it's typically only used in the browser. So we have some dissonance between the physical tiers and how client-side JavaScript can be used to augment each of the three tiers:
  • JavaScript can create and manipulate objects just like the data tier.
  • JavaScript can run business logic on the client side. This can reduce round trips to the server.
  • JavaScript can present the data to the user in all sorts of ways.
When JavaScript deals with all three parts of the three-tier architecture some interesting results occur:
  • The logical tiers are now split across the physical tiers (the client and the various types of servers).
  • Code may be duplicated in different places in different languages. Customer validation in the client would be in Java while Customer validation on the business logic tier would be done in Java or C#. Copy/Paste Programming is bad but try to imagine Copy/Translate/Paste Programming.
  • Thanks to JavaScript debuggers that can be embedded in a browser, users can inspect, the DOM and make changes to it. This has a number of security implications. A JavaScript application can't always trust its own DOM.

Simplifying the Question

Let me back up for a minute. Should JavaScript be used in all three tiers (on the client side)? I think that it's safe to say JavaScript should not generally be used in the client-side data tier. The data tier is all about persistence and JavaScript has some significant limitations in the persistence department. It's very clear that JavaScript is a very natural fit for use in the presentation tier. This leaves only the question of whether or not JavaScript should be used in the logic tier? And if so, how?

I think that JavaScript brings a lot of benefits to the client-side logic tier. Unfortunately it can only do so safely if the server mirrors that business logic and distrusts the client-side logic. Since JavaScript is so dynamic users can tamper with client-side code and make the business logic do anything they want.

Code Generation to the Rescue?

I wonder if code generation is a partial solution to this. The book, "The Pragmatic Programmer" suggested that if code/data does need to be duplicated, then there should be one source representation of that code or data and all duplicates should be generated from it.

The wonderful thing about JavaScript is that even if client-side code and server side code are generated from a single source, they will always be in synch because the client is always downloading the most recent version of the JavaScript.

Conclusion

I'll spend some more time thinking about this and then write up some thoughts. I'd be very interested in hearing how you approach this.

Thanks.

2 comments:

Unknown said...

Hey I really enjoyed this. I mean it. I'm planning on building my own cms. I'm also a javascript junky, so I'm trying to figure out exactly how much of a role javascript should play in the application architecture.

To answer your question, I think that javascript has certain drawbacks to seo( search engines generally aren't prepared to crawl it ), so I don't think it should be used overly in the logic tier. But it's efficiency, and ability to reduce calls to the server are remarkable, and it wouldn't surprise me if the whole search engine thing changes in the very near future.

As you've observed, javascript has grown into a beast, and it's advantages are unbelievable.

Unknown said...

Hey I really enjoyed this. I mean it. I'm planning on building my own cms. I'm also a javascript junky, so I'm trying to figure out exactly how much of a role javascript should play in the application architecture.

To answer your question, I think that javascript has certain drawbacks to seo( search engines generally aren't prepared to crawl it ), so I don't think it should be used overly in the logic tier. But it's efficiency, and ability to reduce calls to the server are remarkable, and it wouldn't surprise me if the whole search engine thing changes in the very near future.

As you've observed, javascript has grown into a beast, and it's advantages are unbelievable.