views:

107

answers:

3

Sometimes in order to make our application more responsive and fast, we use client JS.

Are there any best practices of effective porting C# code to JS? Things that come to my mind are:
- parameterizing scripts
- generating scripts
- dynamic or static script linking
- usual workarounds for typical cases as validation.
- synchronizing modifications,
- criteria when to double logic, when to have only client or only server way.

Thanks in advance. UPD. I asked not about tools mostly - but about patterns. I'm ready to write bit by bit, but with concern to general rules if such exist.

A: 

For your point 'criteria when to double logic, when to have only client or only server way', I'd do this for things such as validation. Do it once on the client, so the user gets instant feedback, but also do it on the server to make it secure and to cater for users without javascript.

Chris
A: 

One option to porting C# code to JavaScript would be to use Script#, which is a cross-compiler of C# to JavaScript.

That said I'm not a fan of having lots of business logic in JavaScript, as you run the risk of your business logic being hacked (ie - skipped if someone uses a JS debugger). It also makes for problems if JavaScript is turned off.

Slace
+2  A: 

this is currently unsolved problem. you will either have to do it twice (once in javascript and the second time on the server)

or...

  • you could rewrite your application in silverlight (so you can reuse the same classes on client and server)
  • if you don't want to rewrite in silverlight, you could still run .NET code using silverlight plugin in webbrowser and interop with html but that's really evil and probably will be also slow
  • use script# to convert your c# code into javascript (unsupported)
  • use jsc (similar to script# but converts MSIL instead of C#)
  • project Volta (Microsoft's answer to GWT), but it's probably dead as silverlight fits better into microsoft's plans.

it looks like you've got lots of choice but none of these solutions are good enough and many are simply tricky to implement without running into problems. I would just recommend to write javascript on client by hand where necessary bit by bit - I know this is not the answer you are looking for, because we all want to follow rule "once and only once" but sometime it's not possible.

lubos hasko
Lots of good options there!
Chris