Skip to main content

SharpKit C# to Javascript Convertor

 

Recently I discovered SharpKit whilst looking at an online IDE called CodeRun.

SharpKit is described as “a Web Toolkit that enables you to write C# and convert it to JavaScript during compilation.”

There are a number of other similar tools around that try and achieve the goal of writing in a Static language and then converting it to JavaScript.

Some of those include: 

I can’t speak for jsc or Script # but I’ve had a good look at GWT and although I can see the benefits it reminded me a bit of the ASP.NET WebForms model in that the high level of abstraction has a big learning curve.

The SharpKit feature list is very impressive.

This is the first time I have tried SharpKit so I’m going to do a very simple walkthrough and how to get started.

Installation

Grab the Download from the SharpKit website and run the Installer.

As SharpKit is a commercial product they require you to Register before Downloading, but don’t be put off by this.

After installing you will get the following folders.

  • C:\Program Files\SharpKit\Addin
  • C:\Program Files\SharpKit\Assemblies
  • C:\Program Files\SharpKit\Samples
  • C:\Program Files\SharpKit\Templates
  • C:\Program Files\SharpKit\Utils

The Samples folder contains really good examples of each project type so is the best way to get started.

Project Structure

In my ignorance I assumed that you could just reference the DLLs and away you go, however this was an incorrect assumption. You actually have to use one of the SharpKit Visual Studio project templates.

I really don’t like this and would expect that there should be a Command-Line interface that you can call in the Post-Build event to do the conversion. Correct me if I’m wrong but I couldn’t find any such Utility.

sharpkit_projects

 

Defining the Class

When you create a class that you want to be exported to JavaScript you have to annotate it with the JsType attribute. This defines how you want the Class to be converted at Build-Time.

There are 4 distinct modes you can use

  • CLR – Adds .NET Support to the Client including LINQ
  • Global – Exports all function as global static functions
  • JSON – Not exported but treats as a JSON object when instantiated
  • Prototype – Exported as a function with an instance constructor

If you want to use jQuery then you need to inherit from jQueryContext.

The other 3rd party libraries following the same pattern and inheriting a base class.

Below is a simple example:

    [JsType(JsMode.Global, "common.js")]
    public class common : jQueryContext
    {
        public static void btn_click()
        {
            jQuery("#div1").show(1000, onShowEnded); 
        }
        static void onShowEnded()
        {
            jQuery("#div1").css("color", "red"); 
        }
    }

As you can see that you have to specify the physical name of the file in the JSType Attribute and the file will be generated relative to where the CSharp file is located.

The generated JavaScript for the above class looks like this:

//@AutoGenerated
function btn_click()
{
    $("#div1").show(1000, onShowEnded);
}
function onShowEnded()
{
    $("#div1").css("color", "red");
}

The code that’s generated is of a high standard. There’s not much else to show you on this as it’s fairly self-explanatory, check out the How-Tos for more detailed information.

3rd Party Library Support

Whilst you can use SharpKit to write raw JavaScript there’s not a lot of point these days with so many great libraries around.

SharpKit comes with very comprehensive support for Libraries and currently supports the follwing:

  • jQuery
  • jQTouch
  • jQuery UI
  • YUI
  • ExtJS
  • GoogleMaps
  • ASP.NET AJAX

It’s yet to be seen how they manage to keep up with library changes.

Pricing

At the time of writing they have a Freemium pricing model.

SharpKit Lite – Free version with a limit of 1000 lines of code
SharpKit Pro - $649 / Developer


Having a free version is good but if you’re considering a Tool of this sort at all then it’s likely you’re doing some heavy JavaScript development so will very quickly go over 1000 LOC.

At $649 I think it is pretty good value, however it would likely be too much for lone developers or Open Source projects.
What would be nice is an Open Source Project License which is heavily discounted or better yet free. This could be combined with an affiliate badge linking back to SharpKit that you need to display on your website, similar to Kentico

Pros

All in all I was pretty impressed with this Tool and it shows great promise. Here’s what I think are good about it:

  • Comprehensive 3rd Party Library support
  • .NET CLR Support
  • Good Documentation & Support
  • Generates Clean JavaScript Code

 

Cons

However there were a few things I do not like about it.

  • Forced to use the SharpKit Visual Studio Project Template
  • No Visual Studio 2010 Support
  • Pricing
  • Heavy use of Attributes
  • No Command-Line tool to perform the conversion

 

Conclusion

As a Software Architect I’ve never been able to decide on the best way for JavaScript to be organized in Web Projects and usually leave it up to the Lead Web Developer who’s going to be working with it on a daily basis.

However this inevitably leads to lot’s of scattered JavaScript throughout the project and it quickly becomes a maintenance headache.

So to me the idea of writing C# and have it converted to JavaScript at build-time is very attractive indeed from both a maintenance and testability point of view. However I’m thinking that in practice it’s only really going to help .NET Developers with basic JavaScript knowledge as more advanced JavaScript developers would feel highly constrained by losing the Dynamic goodness of the language.

For those reasons I’m not entirely sold on SharpKit and similar tools just yet, however it’s definitely worth investigating further and I’m probably going to use this on the next piece of non-trivial JavaScript that needs writing.
Stay tuned for more on that.

I’m keen to know your thoughts on this and similar tools, so feel free to comment.

Till next time. 

Popular posts from this blog

ASP.NET MVC Release Candidate - Upgrade issues - Spec#

First of all, great news that the ASP.NET MVC Release Candidate has finally been released.  Full credit to the team for the hard work on this.  You can get the download here  However this is the first time I have had upgrade issues.  Phil Haack has noted some of the issues here   If like me you have lot's of CTP's and Add-Ins then you might experience some pain in Uninstalling MVC Beta on Vista SP1  This is the list of Add-Ins / CTP's I had to uninstall to get it to work  Spec# PEX Resharper 4.1  Sourcelinks ANTS Profiler 4   Can't say I'm too impressed as it wasted over an hour of my time.  As it turned out Spec# turned out to be the offending culprit, it's forgiveable to have issues with a third party product but a Microsoft one? Guess no-one on the ASP.NET team has Spec# installed. 

Freeing Disk Space on C:\ Windows Server 2008

  I just spent the last little while trying to clear space on our servers in order to install .NET 4.5 . Decided to post so my future self can find the information when I next have to do this. I performed all the usual tasks: Deleting any files/folders from C:\windows\temp and C:\Users\%UserName%\AppData\Local\Temp Delete all EventViewer logs Save to another Disk if you want to keep them Remove any unused programs, e.g. Firefox Remove anything in C:\inetpub\logs Remove any file/folders C:\Windows\System32\LogFiles Remove any file/folders from C:\Users\%UserName%\Downloads Remove any file/folders able to be removed from C:\Users\%UserName%\Desktop Remove any file/folders able to be removed from C:\Users\%UserName%\My Documents Stop Windows Update service and remove all files/folders from C:\Windows\SoftwareDistribution Deleting an Event Logs Run COMPCLN.exe Move the Virtual Memory file to another disk However this wasn’t enough & I found the most space was

Consuming the SSRS ReportExecutionService from a .NET Client

  I’ve just finished writing a nice wrapper which internally calls the SSRS ReportExecutionService to generate reports. Whilst it was fairly simple to implement there has been some major changes between 2005 and 2008 and the majority of online and documentation is based on the 2005 implementation. The most important change is that the Report Server and Report Manager are no longer hosted in IIS which will be a welcomed change to Sys Admins but makes the security model and hosting model vastly different. So far I’ve yet to figure out how to allow Anonymous Access, if anyone knows how to do this leave a comment and it will be most appreciated. Getting Started To get started you’ll want to add a service reference to http://localhost/ReportServer_SQL2008/ReportExecution2005.asmx where ReportServer_SQL2008 is the name you configure in the Reporting Services Configuration Manager. The Web Application files are located in C:\Program Files\Microsoft SQL Server\MSRS10.SQL2008\R