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:
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.


RSS