Using the SQLite Database Access API in AIR… Part 2: Using the DAO Pattern

The first version of our contact management application introduced us to the database access API in AIR. However, the lack of “application partitioning” or “separation of concerns” in that first implementation represented a poor architecture with no potential for reusability.

In this second version, we use the Data Access Object pattern to improve the overall architecture of our application. A Data Access Object typically encapsulates the data access logic for one entity (in this case: Contact).

An interface (named IContactDAO) defines the “contract”:

package
{
	import flash.utils.ByteArray;
	import mx.collections.ArrayCollection;

	public interface IContactDAO
	{
		function findAll():ArrayCollection;

		function insert(contact:Object):void;

		function update(contact:Object):void;

		function updatePicture(contactId:int, jpeg:ByteArray):void;

		function deleteItem(contact:Object):void;
	}
}

The ContactDAO class implements that interface and provides one specific implementation of the contract (persisting data to the embedded SQLite database).

Benefits:

  • The View doesn’t know anything about your data access logic: You can reuse the same view (ContactForm) with a different way to access your data. You would just create another class implementing IContactDAO and pass an instance of that class to ContactForm. Notice that the dao property of ContactForm is of the IContactDAO data type (the interface). This allows us to pass an instance of any class implementing the IContactDAO interface to ContactForm.
  • The DAO doesn’t know anything about the view: You can reuse the same data access logic from within different views.

Install inSync Local DAO Edition:
[airbadge]inSyncLocalDAO, http://coenraets.org/apps/insync/inSyncLocalDAO.air, v1, http://coenraets.org/apps/insync/insync1.png[/airbadge]


Click here to download the source code. You can also right-click the app and select View Source to view the source code and download the application.

Limitation:

There is still a lot of SQL code to write. There are a few options to overcome that limitation:

  • You could create a mini DAO framework where a base DAO class would take care of all the boilerplate code to set up and execute SQL statements. (See the BaseDAO class in Salesbuilder for an example).
  • You could use an ORM framework where SQL statements are automatically generated.

In part 3, we’ll look at a version of inSync built with an annotation-based ORM framework.

25 Responses to Using the SQLite Database Access API in AIR… Part 2: Using the DAO Pattern

  1. Forrest November 26, 2008 at 12:57 am #

    Hi,Christophe Coenraets ,all of your collaboration flex smaples do not work on my machine on the FlashPlayer 10!!!!and i don’t know why, I’m so heart hurt!!!

  2. Forrest November 26, 2008 at 12:58 am #

    Hi,Christophe Coenraets ,all of your collaboration flex smaples do not work on my machine on the FlashPlayer 10!!!!and i don’t know why, I’m so heart hurt with trouble shooting!!!

  3. Rob McKeown December 8, 2008 at 2:00 pm #

    Do you have any plans to extend the ORM prototype you posted a while back to work with lazy loading, collections and/or more complex object graphs? I took your example and added collections support for use in Klok, but it starts to get very complicated (obviously).

  4. radyo dinle July 19, 2009 at 10:10 am #

    Hi,Christophe Coenraets ,all of your collaboration flex smaples do not work on my machine on the FlashPlayer 10!!!!and i don’t know why, I’m so heart hurt!!!

  5. Daniel McQuillen July 23, 2009 at 5:25 pm #

    Kind of a stretch from the original intention of the article but, any thoughts on how to manage database changes as one pushes out new versions of an AIR application (assuming that the DB is not wiped out on a new install).

    Rails has that excellent “migrations” approach, and I’m wondering if anybody has implemented something similar for AIR.

    Thanks for any thoughts. I won’t be heart hurt if you don’t reply.

    Daniel

  6. Boyce Polito April 25, 2014 at 8:31 pm #

    Poor luck with all the boat. I feel you must re-train for the swim it’ll be definitely the hardest portion of the race. You can should be ultra healthy and bulked up. When Martin Strell swam the Amazon he mentioned it absolutely was totally unattainable to not slim down, it doesn’t matter how substantially meals he ate. He ate 8,000 to 9,000 calories every day and went from 96kg to 59kg. He began the race as bulked up as possible just to end. So get consuming!

  7. Lasik Price May 8, 2014 at 9:58 pm #

    Very good quality post! I’ve book marked your website as well. I realized your writing is genuinely educational and I loved reading your posts.

  8. laser eye surgery May 10, 2014 at 2:13 pm #

    Very excellent post! I’ve book marked your web site as well. I noticed your writing is genuinely useful and I appreciated reading your posts.

  9. http://dapperbroker6965.yolasite.com May 15, 2014 at 12:27 am #

    Nice read, I just passed this onto a friend who was doing some
    research on that. And he actually bought me lunch because I found it for him
    smile Therefore let me rephrase that: Thanks for lunch!

  10. Tula Mckittrick May 18, 2014 at 8:34 am #

    Hello, i think that i saw you visited my blog thus i came to

  11. ROI May 21, 2014 at 4:44 pm #

    Very informative blog.Much thanks again. Much obliged.

  12. Interesting article, by the way have you read about this product review? About Raspberry Ketone with Green Coffee Supplements

  13. phytoceramides benefits June 21, 2014 at 4:25 am #

    You really make it seem so easy with your presentation but
    I find this matter to be really something that I think
    I would never understand. It seems too complex and very broad for me.
    I am looking forward for your next post, I’ll try
    to get the hang of it!

  14. how do i treat dermatitis June 27, 2014 at 3:36 pm #

    Hello, i think that i saw you visited my website thus i came to “return the favor”.I
    am trying to find things to enhance my website!I suppose its ok to use some of your ideas!!

  15. best treatment for angular cheilitis July 2, 2014 at 1:46 pm #

    Hi there, I enjoy reading through your article post.
    I like to write a little comment to support
    you.

  16. Munchausen alternative medicine July 3, 2014 at 5:11 pm #

    My spouse andd I absolutely love your blog and
    find a lot of yyour post’s to be precisely what I’m looking for.

    can you offer guest writers to write content to suit your needs?
    I wouldn’t mind creating a post or elaborating
    on a number of the subjects you writye related to here.

    Again,awesome website!

  17. Goonies, July 4, 2014 at 1:06 pm #

    I loved listening tօ all tɦeir laughter ɑnd chatter….
    To transport “The Goonies” storyline іnto modern American life աould
    еither require fοllowing the cast of thhe first film as
    adults or retelling the story in a modern setting.
    Starring Barret Oliver аs Bastian Balthhazar Bux
    (named оnly \”Bastia Bux\” іn tҺе movie), Noah Hathaway аs Atreyu,
    and Tami Stronach as Thе Childlike Empress.

  18. learn how to play zynga poker July 4, 2014 at 10:43 pm #

    It’s awesome designed for mme to have a web site, which is valuable in favor of my experience.
    thanks admin

  19. Helaine July 7, 2014 at 4:44 pm #

    Hey! Quick question that’s completely off topic.
    Do you know how to make your site mobile
    friendly? My website looks weird when viewing from my apple iphone.
    I’m trying to find a theme or plugin that might be able
    to resolve this problem. If you have any recommendations, please share.
    Appreciate it!

  20. Randolph July 8, 2014 at 1:16 am #

    whoah this blog is wonderful i love studying your
    posts. Stay up the good work! You realize, lots of individuals are searching around for this information,
    you can aid them greatly.

  21. gazeta shqip July 8, 2014 at 3:46 pm #

    Simply want to say your article is as surprising.
    The clarity in your post is just great and i could assume you’re
    an expett on this subject. Fine with your permission allow me
    to grab your eed to keep updated with forthcoming post. Thanks a million and
    please continue tthe enjoable work.

  22. Sharon July 8, 2014 at 3:58 pm #

    I get pleasure from, lead to I discovered exactly what
    I used to be having a look for. You’ve ended
    my 4 day long hunt! God Bless you man. Have a great day.
    Bye

  23. Twyla July 28, 2014 at 5:48 am #

    You actually make it seem so easy with your presentation but I find
    this matter to be really something that I think I would never understand.
    It seems too complex and very broad for me.
    I am looking forward for your next post, I will try to get the hang of it!

Trackbacks/Pingbacks

  1. Using the SQLite Database Access API in AIR | Adobe AIR Tutorials - November 26, 2008

    [...] Using the SQLite Database Access API in AIR… Part 1 Using the SQLite Database Access API in AIR…… [...]

  2. Using the SQLite Database Access API in AIR… Part 3: Annotation-Based ORM Framework : Christophe Coenraets - December 1, 2008

    [...] the second version of our contact management application, we encapsulated the data access logic for the Contact entity in a Data Access Object (ContactDAO). [...]

Leave a Reply

css.php