Annotating ActionScript Classes with Custom Metadata + Simple ORM Framework for AIR

A little known feature of Flex 3 is that you can annotate ActionScript classes with your own metadata. For example you could annotate a class as follows:

package
{
	[Bindable]
	[Table(name="contact")]
	public class Contact
	{
		[Id]
		[Column(name="contact_id")]
		public var contactId:int;

		[Column(name="first_name")]
		public var firstName:String;

		[Column(name="last_name")]
		public var lastName:String;
		public var address:String;
		public var city:String;
		public var state:String;
		public var zip:String;
		public var phone:String;
		public var email:String;
	}
}

In this example, [Bindable] is a standard Flex metadata annotation while Table, Id and Column are custom. 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).

To set the compiler flag, right-click the AIR project name in FlexBuilder, select “Properties”, and “Flex Compiler”. For this example, I added -keep-as3-metadata+=Table,Id,Column to the “Additional compiler arguments”. Notice that it is important to use += and not =, otherwise standard metadata annotations (like [Bindable]) will not be available in the SWF.

Custom metadata annotations can be used for all sorts of interesting things . For my session at MAX this year, I used custom annotations to build a simple persistence framework for AIR.

Using this framework, you annotate your model classes to indicate the database table that should be used to persist instances of that class, the identity field in your class (which corresponds to the primary key of that table), and name mappings when the database column name is different from the class field name. That’s all you have to do to provide your AIR applications with automatic persistence to the embedded SQLite database. No SQL to write! The framework will even generate the table if it doesn’t already exists.

For example to add a new contact to your database, you’d simply do something like this:

var contact:Contact = new Contact();
contact.firstName = "Christophe";
contact.lastName = "Coenraets";
contact.email = "ccoenrae@adob.com";
entityManager.save(contact);

to modify the contact:

contact.firstName = “Chris”;
entityManager.save(contact);

to remove the contact:

entityManager.remove(contact);

You can provide the entityManager with instances of any annotated class and it will figure out how to persist the object (how to generate the appropriate SQL statements) based on your metadata annotations.

Disclaimer: This is a simplistic proof of concept and is by no means a production ready ORM solution. Some basic assumptions are made for simplicity. For example, I assume that all primary key are autoincremented integers.

You can download the sample application here. To test it, create an AIR project in FlexBuilder, copy the sample files in that project, and set the compiler options as described above.

65 Responses to Annotating ActionScript Classes with Custom Metadata + Simple ORM Framework for AIR

  1. shaun October 3, 2007 at 7:47 am #

    Nice. Thanks for the example code.
    After playing with AIR and the embedded SQL functionality I thought I’d have a go a writing a ORM for AIR. This(metadata information) is the missing piece of the puzzle for me. I was considering an alternative approach but i prefer this option.

  2. Tink October 4, 2007 at 4:14 am #

    Adding the metadata references as compiler arguments could end up being quite a dirty process if you have a lot of metadata you want to keep and you only have that single line text input comtrol.

    Is there any plan for an option to keep all as opposed to listing them individually?

    keep-as3-metadata=ALL

  3. Gregory Pierce October 5, 2007 at 10:01 am #

    Are you going to implement the rest of JPA (object graphs and such) or are you leaving this as an exercise for the reader? :)

  4. mloncaric October 5, 2007 at 1:07 pm #

    omg. Thank you very much

  5. Pat Ryan October 7, 2007 at 8:37 pm #

    I attended your session at Adobe Max in Chicago. It was one of the best sessions of the conference.
    When I returned home I started to create a simple application to show the synchronous API, asynchronous API and finally your EntityManager. I created the database outside of the EntityManager so it existed before running the EntityManager create sql.

    I noticed by running it this way the EntityManager did not populate data. It turned out to be an assumption in the typeObject method. In there you had:
    instance[item.field] = o[item.column];
    which assumed the column was upper cased which mine were not. But they would have been if I let the EntityManager create the table. The columns were the same case as the Object property that I was loading so the SQLStatement would populate the object from column name.

    I added the following and was able to get my sample application to work for synch DB, asyncDB as well as your EntityManager.

    instance[item.field] = o[item.field];
    if( instance[item.field] == null ) {
    instance[item.field] = o[item.column];
    }

    Conversely if I created the table with the EntityManager but if I used my syncAPI example it would not work because of the upperCased columns. So I also had to modify the loadMetadata method to not upperCase the column names.

    If you would like to see a test case I can send you my simple example.

    Do you have any thoughts or comments on these changes? If I have misunderstood its usage just let me know.

    Thanks again for a great session and a nice piece of work.

  6. NetCfmx October 8, 2007 at 2:46 am #

    Hi Christophe,
    I attended the very last session that you presented at max this year. I liked your session very much it was actually my favorite this year. When i started using air to make something simple to begin with of course a contact manager was the most obvious choice. So when i ran into some bump i used your application as reference. But i am a problem with the relation in this line of code:
    “var identity:Object = map.identity;”
    now when i used it in my code it failed giving a null object yet the object passed to the function arguments was in fact as expected is there anyone who can explain this line within this function?

    private function createItem(o:Object, c:Class):void
    {
    var stmt:SQLStatement = map.insertStmt;
    var identity:Object = map.identity;
    var fields:ArrayCollection = map.fields;
    for (var i:int = 0; i<fields.length; i )
    {
    var field:String = fields.getItemAt(i).field;
    if (field != identity.field)
    {
    stmt.parameters[“:” field] = o[field];
    }
    }
    stmt.execute();
    o[identity.field] = stmt.getResult().lastInsertRowID;
    }

    Thanks

  7. Pat Ryan October 8, 2007 at 6:51 am #

    Will you be making your presentation from Adobe Max available for download.

    Thanks

  8. Pat Ryan October 8, 2007 at 6:53 am #

    Hi NetCFmx
    If you make your code available I would take a look as I have time.

    Maybe you could try the new Adobe share beta. It has been a really easy way for me to share files.

    http://www.adobe.com/go/share

  9. NetCFmx October 11, 2007 at 6:12 pm #

    Hi Pat,

    Thanks for taking the time to look at this i have opened an account so you can get the files they are based on the example that Christophe has posted in his contact manager. Of course i modified a few things to make sure i was able to understand the structure of the code. I am trying to leverage the usage of SQL lite and the relation with the annotation of objects. When i studied the function i could not understand what was going on in the line of code :
    “var identity:Object = map.identity;”

    I miss the relation between the “map” and the property “.identity”.

    Anyways here is the link check it out when you can

    you can message me at wws*at*worldwidesmith*dot*com

    Thank you
    https://share.adobe.com/adc/document.do?docid=68809bd9-785e-11dc-b75f-151d3f6d9313

  10. NetCFmx October 11, 2007 at 6:15 pm #

    Figures of course two files two links here is the missing link.

    https://share.adobe.com/adc/document.do?docid=6977581e-785e-11dc-b75f-151d3f6d9313

  11. flex video November 8, 2007 at 9:06 am #

    Great Job. I think I will use this technique as part of my flex proejct.

  12. Dan Zeitman November 15, 2007 at 8:11 am #

    Great concept. FYI… Since the AIR BETA is a relentlessly moving target… I noted some changes in the AIR API that will need to be addressed in order for your sample to work.

    new SQLConnection(); now takes no args

    in EntityManager.as
    you will need to change line 242:

    _sqlConnection = new SQLConnection(TRUE);

    to

    _sqlConnection = new SQLConnection();

    and in the main.mxml file line 21
    change:

    _sqlConnection = new SQLConnection(TRUE);

    to

    _sqlConnection = new SQLConnection();

  13. Láďa November 21, 2007 at 6:56 am #

    I have just started building my first AIR application and I was terrified that I have to go back to SQL again. After I had spent for almost two years with Rails and ActiveRecord I almost forgot that SQL exists :-)

    But actually I think that it will not take much long for some ORM project to appear.

  14. ICQManZ November 28, 2007 at 6:27 am #

    Prodaiy ICQ za 12$ za vse.Ïðîäàþ ICQ 12$ çà âñå.
    274-693
    324-994
    564-567
    605-800
    695-769
    985-425
    132-335
    478-575
    Sviaz so mnoi ICQ 458411483. Ñâÿçü ñî ìíîé ICQ 458411483

  15. eregilkibly December 3, 2007 at 1:37 am #

    Ricerca Farmacie. Inserisci il tuo CAP o la localita che ti interessa. CAP o localita. Se vuoi restringere la ricerca inserisci anche l’indirizzo comprare viagra on line

  16. Dan Zeitman April 27, 2008 at 10:58 am #

    – UPDATE —

    Again what a great technique… I’m using it to map value objects from one to another…

    FYI — Final release of Flexbuilder 3.0 doesn’t support the option=value syntax!!!!

    Instead you must repeat the option = value:

    -keep-as3-metadata “Column”
    -keep-as3-metadata “ID”
    -keep-as3-metadata “Table”

  17. Anthony June 24, 2008 at 5:18 am #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

  18. Jacob Wright August 15, 2008 at 4:49 pm #

    I will be using your Simple ORM as an example of projects that use metadata in my 360 Flex presentation this next week. I hope you don’t mind.

    I am presenting on how to use metadata and Proxy to create better APIs and will site you as someone who has done this.

    [Tink]: With Flex Builder 3, if your SWC was compiled with the -keep-as3-metadata option, any project the SWC is used in don’t need to add it. It will be added automatically! Helps since the -keep-as3-metadata=ALL isn’t available.

  19. sohbet August 22, 2008 at 6:26 am #

    omg. Thank you very much

  20. chat August 22, 2008 at 6:27 am #

    Thank you very much

  21. key ödemeleri August 28, 2008 at 3:37 pm #

    thanks thanks thanks :)

  22. cryptos September 15, 2008 at 2:00 pm #

    Thanks Google that helps me to find this site!

  23. cryptos September 15, 2008 at 2:09 pm #

    Thank you very much! I’ll use this technique as part of my new proejct.Try to see this blog – Sexy Latina Maids

  24. sohbet January 7, 2009 at 1:07 pm #

    Is there any plan for an option to keep all as opposed to listing them individually?

    keep-as3-metadata=ALL

  25. daslaber January 7, 2009 at 1:07 pm #

    Thanks Google that helps me to find this site!

  26. medyum July 10, 2009 at 6:58 pm #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

  27. Costin July 16, 2009 at 3:04 am #

    Has anyone tryed to map data on multiple related tables? Would be a really nice feature to be able to take orm at this level of abstractization. Tx for this class, it’s awesome ^_^

  28. zerrin egeliler July 23, 2009 at 11:37 am #

    Again what a great technique… I’m using it to map value objects from one to another…

    FYI — Final release of Flexbuilder 3.0 doesn’t support the option=value syntax!!!!

    Instead you must repeat the option = value:

    -keep-as3-metadata “Column”
    -keep-as3-metadata “ID”
    -keep-as3-metadata “Table”

  29. Егор Макаров August 18, 2009 at 10:49 am #

    Хорошо написано, приятно полистать ваш сайт!

  30. sohbet odası November 24, 2009 at 6:15 am #

    how are you baby melisa

  31. chat December 3, 2009 at 3:05 pm #

    thanks sites

  32. araç sorgulama December 29, 2009 at 6:09 am #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

  33. sohbet January 9, 2010 at 9:40 pm #

    good post admin..

  34. chat January 13, 2010 at 2:18 pm #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

  35. alem March 15, 2010 at 4:25 pm #

    thanks I think I will use this technique as part

  36. Büyü Nasıl yapılır May 10, 2010 at 8:54 am #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

  37. Webmaster forum May 14, 2010 at 1:24 pm #

    After I had spent for almost two years with Rails and ActiveRecord I almost forgot that SQL exists

  38. Eryaman hali yikama June 10, 2010 at 10:17 am #

    Good information and good way your blog post. Good luck blogger man.

  39. مدونة الاماكن June 18, 2010 at 3:25 pm #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

    مدونة الاماكن

  40. chat June 19, 2010 at 4:01 pm #

    Well done and good luck with your very great work thanks

  41. diziizle June 28, 2010 at 5:29 pm #

    Metadata tags to your actionscript files.

  42. pornoizle July 10, 2010 at 12:27 pm #

    Good luck blogger man.

  43. rottweiler July 16, 2010 at 8:51 pm #

    thanks for information

  44. stock picking August 2, 2010 at 2:24 am #

    This is really useful. I can imagine people not understanding its function correctly though.

  45. matbaa August 8, 2010 at 9:34 am #

    Thanks for that I had no idea you could add your own Metadata tags to your actionscript files.

  46. Rory MacDonald August 12, 2010 at 5:11 pm #

    Spend all day trying to figure out why my compiled AIR app was not outputting the Meta data when using describeType() – just stumbled across your site + the “-keep-as3-metadata+=Table,Id,Column” compiler argument! You’ve saved the day – thanks!

    R

  47. medyum August 20, 2010 at 10:12 am #

    Very nice sharing.thanks.

  48. free August 28, 2010 at 3:27 pm #

    why my compiled AIR app was not outputting the Meta data when using describeType() – just stumbled across your site + the “-keep-as3-metadata+=Table,Id,Column” compiler argument! You’ve saved the day – thanks!

  49. Sohbet October 3, 2010 at 7:16 am #

    Best of the best blog Tenks admin you power blog

  50. canlı sohbet October 5, 2010 at 1:24 pm #

    nice ! admin

  51. free sex videos March 14, 2011 at 3:41 pm #

    xbha kdcln free sex videos cxycvd d jh n wkj

    jfka sdaiu [URL=http://www.frannysex.com]adult movies[/URL] thodhj u fz l oan

  52. porno sohbet August 9, 2011 at 11:43 am #

    thank you admin .!

  53. Gerry Beauregard November 2, 2011 at 10:17 am #

    Thanks for this very useful post. I spent several hours (!) trying to figure out why info in Inspectable tags of getters/setters appeared in the XML returned by flash.utils.describeType() in a Debug build, but not in a Release build. After much Googling, I found your post and the tip to use -keep-as3-metadata in the compiler options. I added this
    -keep-as3-metadata+=Inspectable
    to my compiler settings, and voilà – problem fixed. Thanks!

  54. New Arrival Michael Kors July 26, 2013 at 11:20 pm #

    We are a gaggle of volunteers and opening a new scheme in our
    community. Your web site offered us with useful information to work on.

    You’ve done an impressive task and our entire community can be grateful to you.

  55. Brock Demora June 21, 2014 at 1:44 am #

    I like it whenever people come together and share views. Great site, keep it up!

Trackbacks/Pingbacks

  1. Sönke Rohde » MAX Barcelona - Short Notes - October 17, 2007

    […] Reflexion API (describeType). Christophe has posted some code regarding the ORM approach on this blog. Now this is opens real new possibilities. Now let’s wait when Hibernate or ActiveRecord is […]

  2. Salesbuilder Beta 3 (AIR file + Flex Source Code) : Christophe Coenraets - February 4, 2008

    […] DAOs used in the previous version. I might move to the annotation-based ORM approach introduced here in a future version. Finally, the “lazy loading” strategy has been improved as well and […]

  3. » air orm - February 23, 2008

    […] have already been some moves in this direction, but something styled after Hibernate or ActiveRecord would be nice. Subscribe […]

  4. Anonymous - March 24, 2008

    Dirty Latina Maids Jennifer Luv…

    Dirty Latina Maids Jennifer Luv…

  5. online amerikansk roulette - July 20, 2008

    online amerikansk roulette…

    philosophizing Simons sub syntactical …

  6. Sönke Rohde » Open Source and Flex - August 7, 2008

    […] Chotin asked for feedback on suggested projects related to Flex. Personally I would love to see an ORM framework like suggested by Christophe Coenraets which works with […]

  7. FrameworkQuest 2008 Part 4: IoC With Swiz « aodz - January 16, 2009

    […] tag. Flex has had metadata around for a while, but recently allowed a compiler option to keep custom metadata. The Swiz swc library adds two tags to the list of metadata that the Flex compiler will keep […]

  8. Christophe Coenraets » Blog Archive » Using the SQLite Database Access API in AIR… Part 3: Annotation-Based ORM Framework - February 15, 2009

    […] In this third version, we use a mini Object Relational Mapping (ORM) framework that leverages the Flex support for class annotations to entirely eliminate manually-written SQL statements. This is an approach I first explored at MAX 2007 (see original blog post here). […]

  9. Digging into Custom Metadata Tag in Flex and ActionScript « ravigeek - July 10, 2011

    […] 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 […]

Leave a Reply

css.php