Monday, December 20, 2010

JavaScript and generic server side services

For awhile I'm trying to figure out a nice and elegant way to combine a server side code and JavaScript-Ajax code without the need for writing the tedious service layer(Handler page, web service, etc.) and the parallel JavaScript functions.

This came up, after watching various teams doing the same over and over again. And over time breaking their own code as the system keeps growing, sometimes can't find where's what and on which side, and frustration of not having a solid interface where all developers from the same or different teams write to as one.
* for some reason the song "imagine" by John Lennon is playing in my head...

The idea is to write a server side code, which can be "registered" to the client side code in such a way that the client side developer will have all the intended JavaScript code written for her or him and he or she will only be needed to write invocation or binding code to the "registered" code. The server side code documentation can be used to guide the client side developer.

I am assuming the code is written in an object oriented language and it is used in an OOP fashion with a reflection ability.

The solution will require the following components:
* One or more classes which perform service operations(getting stuff, saving it, etc.)

* An interface which all service classes are required to implement

* Registration utility code

* Service Proxy

The following will describe a close to pseudo-code implementation.
This description is a skeleton to a much more complex and smart systems which can be built upon.

Any class needed to be exposed to the client side, will need to implement an interface which will describe all the required functionality for the registration part to work.

for example:

Interface IClientScript
function ignoreService()
// the implementation of this method will need to return a list of methods
// which should be excluded from client side exposure

Service Class
The service will need to implement the client side registration interface.

for example:

Class Stuff implements IClientScript
// just happy empty constructor

function getStuff(arg1){
// this method will fetch some stuff from the db and will return it

function saveStuff(arg1, arg2){
// this method will save some stuff to the db and will return Boolean result

function ignoreService(){
// this is an implementation method of IClientScript interface. it will return
// a list of methods not to be registered to the client

Registration Utility Code
The registration utility code, should get classes which implements the needed interface and expose the service methods using reflection (in a very simple example, only the public methods).
The registration code will generate the client script code, while taking into consideration namespaces, client side libraries, etc.

for example:

function register(name, args){
// this function will take name of a class, will make sure it implements the needed
// interface, read all the methods which needs to be exposed and generate a
// JavaScript code

A generated JavaScript code could look like:

window.NS1 = window.NS1 || {};
window.NS1.ServiceProxy = window.NS1.ServiceProxy || {};
window.NS1.ServiceProxy.url = "";
window.NS1.ServiceProxy.invoke = function(options){
// get details from the options argument and use your favorite way to send
// the request to the service proxy
window.NS1.Stuff = window.NS1.Stuff || {};
window.NS1.Stuff.getStuff = function(arg1){
  "data": arg1,
  "success": successfn,
  "failure" : failurefn});

Service Proxy
The service proxy is an http request handler. This component analyze the request, use reflection to load the necessary service (class), invoke the service method and send back a result message(text, json, etc.) which will be handled by the JavaScript callback function.

1 comment:

Related Posts Plugin for WordPress, Blogger...