标签(Metadata)注入(Inject)式MVC(S)框架之原理及简介
Swiz是此类框架的鼻祖,后起之秀以Robotlegs为最。
1,MVC(S)是什么,路人皆知了。何为标签注入式?
通过在类,属性、方法上添加Metadata标签,以达到MVC协作的框架设计方式,称之为标签注入式。例:
[Inject]
private var sban :Author
2,标签注入式框架的原理?
1)自定义标签名称,如sbanMetadataTag1,sbanMetadataTag2
2)在项目编译参数中添加-keep-as3-metadata += sbanMetadataTag1,sbanMetadataTag2
3)拿到含有自定义标签对象,使用describeType在运行时提取自定义标签信息,然后进行取值、赋值、添加监听等框架动作
3,what is -keep-as3-metadata ?
The -keep-as3-metadata compiler flag allows you to instruct the compiler to keep your metadata in the generated SWF so that you can get to this information at runtime using the reflection API (describeType)
4、目前圈里最流行的标签注入式MVC框架简介
1)Swiz:它是最古老、也是目前最流行的标签注入式框架。文档资料、使用者也最多。
2)Robotlegs:避免使用单例,倡议规则高于限制,代码具有高可测性。是目前最有潜力的标签注入式MVCS框架。S为Service,为客户端与服务端进行数据交互的功能组件。
3)SwiftSuspenders:相比Robotlegs更高提出MVCS概念,是一个标准的标签驱动的IOC(Inversion Of Control) AS3框架。
4)SmartyPants IOC framework
5)Prana
5,what is Robotlegs?
Robotlegs is a pure AS3 micro-architecture (framework) with a light footprint and limited scope. Simply put, Robotlegs is there to help you wire your objects together. It provides the glue that your application needs to easily function in a decoupled way. Through the use of automated metadata based dependency injection Robotlegs removes boilerplate code in an application. By promoting loose coupling and avoiding the use of Singletons and statics in the framework Robotlegs can help you write code that is highly testable.
6,标签注入式框架的优点及缺陷?
优点:实现MVC分离,但同时又对开发者没有任何接口、规则的束缚,在使用这些框架的同时,开发者仍可以使用其它框架。标签注入式框架,在所有框架中,是对开发者的束缚最小的。
缺点:
1)使用reflection API在运行时提取Metadata信息,再进行各项框架基础动作,这对性能是一个极大考验。Flex大量使用Metadata标签,但Flex并不在运行时提取标签信息,它是在编译时提取、并作相关处理,最终编译为AS3代码的。这是标签注入式框架最大的弊端,致其不能广泛被推广、应用于企业项目开发。
2)不便调试。
7,Mate的起伏
印度人写了一个基于标签(Tag)的事件驱动框架、Mate。它没有使用Metadata,避开了其第一缺陷,在2009年未获得了广泛认可。但最终因其它原因博得了许多骂名,而不被看好。
8,sban的建议
以上标签注入式框架,以及Mate,还有PureMVC,还有Cairngorm,究竟在中国的企业项目开发中,应该使用哪一个框架?
虽然它们都很优秀,在没有一个框架适当在中国企业项目中应用,不然便是噩梦的开始。
sban 2010/6/21 北京 转载请注明作者及出处,非商业。
扩展阅读:
1)http://coenraets.org/blog/2007/10/annotating-actionscript-classes-with-custom-metadata-simple-orm-framework-for-air/
2)http://en.wikipedia.org/wiki/Inversion_of_control