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.
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.