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:

Please upgrade your Flash Player This is the content that would be shown if the user does not have Flash Player 9.0.115 or higher installed.


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.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • StumbleUpon
  • Twitter
This entry was posted in Air, Flex, MAX, SQLite. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Forrest
    Posted November 26, 2008 at 12:57 am | Permalink

    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
    Posted November 26, 2008 at 12:58 am | Permalink

    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. Posted December 8, 2008 at 2:00 pm | Permalink

    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. Posted July 19, 2009 at 10:10 am | Permalink

    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. Posted July 23, 2009 at 5:25 pm | Permalink

    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

2 Trackbacks

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

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>