The basic lineaments of Flex are tag based programming and use of metadata injection. Several times we used metadata tags in our Flex application and most of us are very familiar with Flex SDK defined metadata i.e. [Bindable], [Event], [Effect] and many more. All these are assigned and SDK defined metadata tags, apart from this we can defined our own metadata tags also. User defined metadata called custom metadata. Basically metadata tags provide information to the Flex compiler that describes how your components are used in a Flex application.Custom metadata tag will be accessed at runtime by FlashPlayer. Nowadays for injecting the dependency many engineer’s introduced this in the application framework. Like in Swiz framework has a [Autowire] custom metadata tag. Autowire metadata is used to define the bean name and is injected with the object that is configured in SwizBeans.xml with the ID field.
This custom metadata feature give us an option to introduced our own tag for achieving specific purpose. For this we have to instruct the compiler to keep this keyword as metadata. If you open the flex-config.xml then you can find the list of sdk defined metadata in <keep-as3-metadata>. For creating own custom metadata we have to use the same -keep-as3-metadata argument in Flex compiler for providing the name of custom metadata tags.
Create Custom metadata in three simple steps.
Steps 1 – in the compiler options you need to tell the compiler to keep your new metadata tag, using this -keep-as3-metadata+=MyMetaData
. Be sure to use the “+=” otherwise you lose the standard Flex metadata tags.
Steps 2 – Add your new tag into your AS3/MXML files. Depending on how you intend to use your metadata tag, you may just have the tag, like this [MyMetaData] or with some attributes like this [MyMetaData(value="my custom tag")], it’s all depends on how we want to use it.
Steps 3 – Now we need to create a custom metadata tag manager class for handling this metadata logic. In custom metadata tag manager class we need to use the flash.utils.describeType to find the properties annotated. The describeType util returns a XML object describing the public properties/methods on the specified class.
Here in the below example you can easily understand the process of creating custom metadata.
From the metadata XML object we have to extract the custom <metadata> value and using those value we need to write our own metadata manager class for providing business logic.
Here I want to give an useful tips regarding compiler argument. If we are using Flex SDK version 3.5 and below then we have to provide the metadata name using -keep-as3-metadata+=MyMetaData. In the case of Flex SDK version 4.0 and above we don’t need to provide the metadata name in compiler argument. Please keep in mind that this is the enhancement in Flex SDK 4.0 and above.
For getting into the live and real example of custom metadata tag you can browse Christophe Coenraets on creating and using metadata tags. He created an AIR app that accesses a SQLLite database. Instead of manually mapping out the relationships between value objects and tables and the necessary queries he used the metadata tags to annotate value object properties with their corresponding database table columns.