[ASP.NET]ScriptShart(Script#):.net版的GWT
前陣子聽了公司中撰寫JSP的同事說她們使用了一套Java的framework,可以大大降低Javascript的撰寫,因為該framework會自動將Java code轉成Javascript Code,而這個工具叫做GWT(Google Web Toolkit)。
其實開發Web產品這麼久了,在進行大量客製化時總會遇到一些瓶頸,那就是過去撰寫Winform程式時,程式設計師需要熟練的程式語言是C#或者VB.NET,但開始撰寫ASP.NET時,程式設計師還要多了解Javascript,說實在的Javascript的結構並不是那麼好,偵錯也不容易,對開發人員來說真的是很大的瓶頸,也大大的增加了客製時所需耗用的時數,當初我們也希望可以降低量產人員撰寫Javascript的量,讓他們撰寫C#程式,然後由我們的函式庫將這些C# Code render成Javascript Code,但大多是局部性的使用,很難形成大量的應用,因為若加上了邏輯判斷與AJAX的功能,要撰寫就不是那麼容易了。
今天在Plurk上貼了GWT2.0的消息後,老狗大貼了Script#這東西給我參考,還沒消化完,不過我們可以先看看這些內容:
- What's in Script#:說明Script#提供了哪些功能。
- Undering and Using Script#:說明的運作機制,可以參考下面兩張圖一起享用。
Script# Compile Time 架構圖
上圖中我們可以看到,我們撰寫的cs程式,會分別被Script#的Compiler與C#的Compiler給編譯,看起來應該是使用了Script#語法的部分會交由Script# Compiler來處理,而一般的C#則交由C# Compiler來處理,Script# Compile出來後的結果不是dll,而是Script file,這就是Script#特別的點了,而透過這樣的架構,確實也可以大大減少了script的撰寫,但這邊我有個疑問,就是程式設計師是否具備能力可以識別何時用Script#語法,何時用C#語法呢?應該很容易混淆才是。
Script# Runtime 架構圖
上圖中我們可以看到,Runtime時Script#使用的幾乎都是js檔了,其中sscorlib.js是一定要參考的,而ssfx開頭的js檔則視我們是否有用到對應的功能而定。
乍看之下Script#很威吧,不過我們也不能不看一下它的限制,條列如下,還蠻多的,這邊就不一一說明囉,有興趣的朋友自行觀看吧:
Limitations
C# Limitations
- Global non-namespaced types and nested types
- Nested namespace declarations; instead you must declare the whole namespace name in a single namespace statement.
- The “System” namespace can only be used to represent existing script types and APIs and not be used for any C# code that will be compiled into script.
- The set of reserved words that cannot be used in Script# include not only C# reserved words, but also JavaScript reserved words.
- Struct types are disallowd as script does not offer value type semantics.
- Pointer types are also disallowd.
- Overloading is limited to providing alternate signatures, rather than multiple implementations for the same named member.
- Destructors, operators methods and conversion methods are unsupported.
- Enumeration fields must have explicit values defined, even if they are the default (0, 1, 2, ...)
- Set-only properties are unsupported.
- The “new” modifier on members is unsupported.
- Throw statements must always throw an explicit object.
- Unsupported statements include goto, using, lock/unlock and yield.
- Unsupported expressions include sizeof, fixed, stackalloc, and default.
The following are limitations in the current compiler implementation:
- Generics
- Support for ref, out and params modifiers on parameters
- Ability to specify a type using its namespace-qualified type names; instead as a workaround you can uses aliases (eg. using Foo = SomeNamespace.SomeType) if you need to disambiguate between conflicting types.
JavaScript Limitations
Script# provides support for the majority of script constructs needed for real-world applications and frameworks. It does however have some small limitations:
- Limited uses of closures - Closures can be modeled to a certain extent using anonymous delegates in C#. However closures used to define classes are not supported. Instead the Script# compiler generates classes based on the prototype pattern.
- Identifier characters cannot contain "$" - This is not supported in C# source code. The compiler uses this limitation stemming from C# rules to its advantage by using “$” in generated identifier names to ensure they do not conflict with existing identifiers elsewhere in your code.
但是即使Script#有這麼多的限制,個人對這樣的framework還是抱有蠻大的興趣,也值得多加留意他的發展囉,下面是它後續的計畫,可參考:
Script# Roadmap
The Script# project has been an on-going piece of work. The following are on the list for a v1 release during 2009 beyond what is available in the current and latest build:
- Unit testing support
- jQuery support
- High priority bug fixes and improved Visual Studio integration
Longer term, there are a number of additional features on the roadmap such as:
- Support for generics
- Support for static linking and other script optimizations
- Support for additional script APIs and scenarios
參考資料:Script#
游舒帆 (gipi) 探索原力Co-founder,曾任TutorABC協理與鼎新電腦總監,並曾獲選兩屆微軟最有價值專家 ( MVP ),離開職場後創辦探索原力,致力於協助青少年培養面對未來的能力。認為教育與組織育才其實息息相關,都是在為未來儲備能量,2018年起成立為期一年的專題課程《職涯躍升的關鍵24堂課》,為培養台灣未來的領袖而努力。 |