Script#: The .NET Response to the Google Web Toolkit


Published on

Script# is a free .NET tool that converts C# into JavaScript, allowing us to use a lot of existing C# tool support to write and manage large JavaScript code bases easier. This presentation introduces Script#, explains when and how to use it and discusses some common pitfalls with this tool.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Script#: The .NET Response to the Google Web Toolkit

  1. 1. Script# - the .NET response to Google Web Toolkit Gojko Adzic [email_address]
  2. 2. Why should you care? <ul><li>A much more productive way to write and maintain complex JavaScript code </li></ul><ul><ul><li>Compile-time consistency check </li></ul></ul><ul><ul><li>Refactoring support </li></ul></ul><ul><ul><li>Full Intellisense for DOM and your own JS classes </li></ul></ul><ul><ul><li>Lots of other VS IDE benefits </li></ul></ul><ul><li>Free (not opensource, though) </li></ul>
  3. 3. What Script# is not <ul><li>It does not hide browser complexity </li></ul><ul><li>It does not abstract away JS peculiarities </li></ul><ul><li>It is not a general-purpose .NET to browser converter </li></ul><ul><li>It is not a widget/effect library (it has some, but not nearly like GWT) </li></ul>
  4. 4. Under the hood <ul><li>Compiles C# into JS directly </li></ul><ul><li>Replacement for System namespace, a mashup of .NET and JS </li></ul><ul><ul><li>Sscorlib.dll in C# for Intellisense, </li></ul></ul><ul><ul><li>sscorlib.js in the browser </li></ul></ul><ul><ul><li>sscompat.js provides cross-browser compatibility </li></ul></ul><ul><li>System.DHTML.Document, System.Script System.DHTML.Window link to the environment </li></ul>
  5. 5. A very simple example <ul><li>Script# library project </li></ul><ul><li>Compile C# into javascript </li></ul><ul><li>Execute from HTML </li></ul><ul><ul><li>namespace.class used to instantiate objects </li></ul></ul><ul><li>If you do the web site by hand, don’t forget sscompat.js! </li></ul>
  6. 6. Visual Studio Integration <ul><li>Project templates for class libraries and web sites </li></ul><ul><li>C# editor for scriptlets (has some bugs, though) </li></ul>
  7. 7. MSBuild Integration <ul><li>ScriptSharp target does the job for you <Import Project=&quot;$(ProgramFiles) StuffScriptSharpv1.0 Stuff.ScriptSharp.targets&quot; /> </li></ul><ul><li>Remember <NoStdLib>True</NoStdLib> </li></ul><ul><li>Automatically added by the VS ScriptSharp template </li></ul>
  8. 8. Scriptlets <ul><li>Script# webforms components </li></ul><ul><ul><li>Add Script# assembly and page control to web.config </li></ul></ul><ul><ul><li>Use main() as an entry point to the component </li></ul></ul><ul><li>No need to worry about script# initialisation </li></ul><ul><li>Put all scripts into App_Scripts folder (or use a VS template to create the project) </li></ul>
  9. 9. AJAX support <ul><li>Add <script type=&quot;text/javascript&quot; src=&quot;App_Scripts/ssfx.Core.Debug.js&quot; > </li></ul><ul><li></script> </li></ul><ul><li>Use ScriptFX.Net. HTTPRequest for cross-browser compatible AJAX requests </li></ul><ul><li>Supports Script transport for Cross-Domain Ajax! </li></ul>
  10. 10. Ajax/JSON <ul><li>private void OnOKButtonClick(object sender, EventArgs e) { </li></ul><ul><li>Dictionary parameters = new Dictionary(); </li></ul><ul><li>parameters[&quot;prefix&quot;] = _prefixTextBox.Text; </li></ul><ul><li>parameters[&quot;count&quot;] = 5; </li></ul><ul><li>_request = HTTPRequest.CreateRequest(&quot;CompletionList.ashx/GetItemsViaPost&quot;, HTTPVerb.POST); </li></ul><ul><li>_request.SetContentAsJSON(parameters); </li></ul><ul><li>_request.Invoke(new HTTPRequestCompletedCallback(OnRequestComplete), null); </li></ul><ul><li>} </li></ul><ul><li>private void OnRequestComplete(HTTPRequest request, object context) { </li></ul><ul><li>if (_request == request) { </li></ul><ul><li>string[] values = (string[])_ request.Response.GetObject() ; </li></ul><ul><li>_valuesLabel.Text = values.Join(&quot;, &quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  11. 11. Behaviours <ul><li>Declaratively attach functionality to DOM elements </li></ul><ul><li>Taken from ASP.NET Ajax (Atlas) </li></ul><ul><li>Popups, watermark, autocomplete, overlay etc… </li></ul><ul><li>Not a lot of widgets, but you can use ExtJS with ExtSharp! </li></ul>
  12. 12. More advanced options <ul><li>FxCop code analysis </li></ul><ul><li>Unit testing should follow soon </li></ul><ul><li>ASP.NET Ajax instead of sscorelib </li></ul><ul><li>Some silverlight support </li></ul><ul><li>MSN Messenger APIs </li></ul><ul><li>Facebook client API </li></ul><ul><li>Sidebar Gadgets </li></ul>
  13. 13. Quirks: Namespace references <ul><li>Does not work: </li></ul><ul><ul><li>DOMElement runner= System.DHTML. Document.GetElementById( </li></ul></ul><ul><ul><li>runnerElementId); </li></ul></ul><ul><li>Works: </li></ul><ul><ul><li>Using System.DHTML; </li></ul></ul><ul><ul><li>DOMElement runner= Document.GetElementById(runnerElementId); </li></ul></ul>
  14. 14. Quirks: Compilation issues <ul><li>VS New class wizard adds System, System.Data and System.Xml references </li></ul><ul><ul><li>Script# compilation breaks as a result </li></ul></ul><ul><li>Does not resolve indirect module dependencies </li></ul><ul><li>Nested namespaces not supported </li></ul>
  15. 15. Quirks: 0 and null comparisons <ul><li>if (something == null) and if (something == 0) compiled into if (!something) </li></ul><ul><li>Try this: </li></ul><ul><li>Number a = null; </li></ul><ul><li>if (a == 0) </li></ul><ul><li>Script.Alert(&quot;I shouldn't be seeing this???&quot;); </li></ul><ul><li>Number b = 0; </li></ul><ul><li>if (b == null) </li></ul><ul><li>Script.Alert(&quot;I shouldn't be seeing this either???&quot;); </li></ul>
  16. 16. Quirks: Scriptlets <ul><li>Scriptlet tag has to be inside a form tag </li></ul><ul><ul><li>If not, nothing happens, but you don’t get an alert </li></ul></ul><ul><li>Inline Scriptlets use ScriptletArguments, pre-compiled ones use Dictionary </li></ul>
  17. 17. Quirks: Events <ul><li>DOMEventHandler expects void() delegate </li></ul><ul><li>Use Window.Event.ReturnValue to return false from an event </li></ul>
  18. 18. What’s good? <ul><li>Working with complex JS files is much very productive </li></ul><ul><li>VS integration </li></ul><ul><li>MSBuild integration </li></ul><ul><li>Basic Documentation is great </li></ul>
  19. 19. What’s not so good? <ul><li>Some unintuitive mismatch between C# and JS – type conversions, 0 and null… </li></ul><ul><li>Advanced stuff not documented that well </li></ul><ul><ul><li>See the samples zip in the distribution! </li></ul></ul><ul><li>Still not opensource </li></ul>
  20. 20. Where next? <ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
  21. 21. What next? <ul><li>Dependency Injection with Castle – Oct 23 rd </li></ul><ul><li>Asynchronous Enterprise Applications with NServiceBus – Nov 27 th </li></ul><ul><li>TDD with .NET – Dec 17 th </li></ul><ul><li>ALT.NET Community evening – Jan 13th </li></ul>