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