Wednesday, November 11, 2009

What we c# programmers need : bridging the gap between server side programming and client side programming

I've been using ASP.NET MVC to write a highly dynamic website which extensively uses JQuery, JQuery UI, JQGrid and some other plugins. After several months of programming, there comes a time of reflection on what was done, to look at what could be improved. One of the things I'm still struggling with is the integration of client side code (javascript) with server side code (ASP.NET Mvc). This is what this article is about.

I've been using HTML helpers to hide away javascript for some controls like jqgrid, datetpicker, autocomplete box. Each HTML helper takes in an parameter object, defining how the controls will behave and render. The necessary javascript is generated behind the scenes, and added to the view in one place using a custom HTML helper to render the javascript. This technique works quite well, providing the parameter object is flexible enough to cope with all different scenarios. As long as the parameter object only maps its properties to the properties of the javascript plugin, all is well.

As soon as you need to add more flexibility, which mostly means you will start to implement events on the javascript plugin, things get out of control. Mostly, it's because one or a few pages need some custom behaviour, which is too specific to make it part of the control (thus extending it).

I tried to create a c# scriptbuilder object, to enable me to add custom scripting to a view, but soon, you are recreating a lot of javascript code constructs inside the scriptbuilder. It gets too complicated, and the scriptbuilder is too different from the javascript code, to be convenient.

I also tried ScriptSharp, a project from Nokhil Kothari, which translates c# code into javascript code. For jquery and jquery plugins, you need to build some stub classes (they don't generate code, but are merely there for providing code completion and compile time checking). It's a project with great potential, I like it a lot. But it still has some shortcomings. I've been building a javascript application framework with it, and it works quite well. But the problems arise again when you want to link server side code with client side code..

I must say, ScriptSharp has some enormous advantages for c# developers :
  • compile time checking of code
  • type checking
  • code completion (for the code itself and for the jquery plugins if you write the necessary stub classes)
  • refactoring
  • code navigation
  • etc.. (all the advantages you get from a compiler environment like visual studio).

Some code constructs are a little bit awkward, due to the differences between c# and javascript.
Eg. all jquery code needs to be written using a proxy class : jqProxy.jQuery("#myDiv").css("color","red");
This is not so much a problem. Another example, which took my a while to discovery, to write $(document).ready, you have to write in c# : jqProxy.jQuery(typeof(Document)).ready( ...);
Most of these inconveniences are tollerable, considering the benifits you get.

The major drawback is lack of support and updates. It's been more than a year since the last version was released.

The other major drawback, is linking server side code, with client side code, one of the problems which stops me from extending current use of ScriptSharp. This code to link server side to client side code (using HTML helpers or scriptbuilder classes), tends to clutter server side code. I get a feeling it doesn't belong there, I should be able to separate it more cleanely. One could argue then, why not write all client side code on the client side in pure javascript.

There are two reasons, why I would not like this as a solution to the problem:
  • first of all, there is currently no good IDE for javascript, giving me what I currently have in c#/scriptsharp.
  • secondly, even if I would have a good development environment for javascript, this still leaves me in the cold, for linking server side with client side easily.

Ideally, I would like to write server side code and client side code in c#,  sharing some code to let me write the integration between them. Eg. the parameter object for my plugins would be a class I can use server side and client side.

One could see it as some kind of mix between MVC and Webforms, where the c# code for the client could be seen as a form of code behind for my view (but only and ONLY for writing display related code, NO business code!!). I realize that what I'm describing is kind of what Silverlight does. But I'm not convinced of Silverlight yet.

So I welcome any good ideas, links to other projects, which can help me in solving this problem... ASP.NET MVC is currently my choice of server side solution, so any ideas should work well with it!

No comments:

Post a Comment