• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
DAOFactory: Using the DAO Factory with CF Applications
by Oscar Arevalooarevalo@sandals.comLast Update: March 12, 2008
About DAOFactory
DAOFactory is a set of components that provide a mechanism to interact with a persistent storagemedium for ColdFusion applications via the use of different Data Access Objects (DAOs). EachDAO provides a common interface for the application to deal with frequently used backend dataoperations such as retrieving single and multiple records, and deleting, inserting and updatingrecords.Additionally, DAOFactory abstracts the specific details on how to interact with a particular storagemedium, opening the possibility to use different mechanisms such as XML files, differentdatabase engines or even different database configurations.
Integrating DAOFactory into your Applications
DAOFactory is a package comprised of several components within a single directory. There is norequirement as for the placement or directory naming of the package.The following sections describe the steps required for using the DAO Factory components in anapplication.
-
Create DAOs
-
Define DAO Factory configuration
-
Using the factory
Creating DAOs 
All applications that use DAOFactory need to define the individual DAOs that will be used. Eachentity on the domain model that needs to be persisted needs its own DAO. The namingconvention is to use the entity name followed by “DAO”. For example, for an entity “Person”, theDAO will be named “personDAO.cfc”.All DAOs must extend the dao.cfc object provided in the DAOFactory package.All DAOs should be placed in the same directory. This can be any directory, but it is
 
DAOFactory
recommended to create a directory that will be used only to place the DAO components.As mentioned before, all DAOs must extend dao.cfc; however they are required to override thefunction “initTableParams”. This function is used to programmatically describe the particularentity. That is, it describes the field names, their data types, the name of the entity in the storagemedium (i.e the table name), and the entity’s identity field.See the following sample code of a dao named PersonDAO.cfc
<cfcomponent extends="DAOFactory.dao.DAO"> <cffunction name="initTableParams" access="package" returntype="void"> <cfsetsetTableName("users")> <cfsetsetPrimaryKey("userID","cf_sql_varchar")> <cfsetaddColumn("firstName","cf_sql_varchar")> <cfsetaddColumn("lastName","cf_sql_varchar")> <cfsetaddColumn("email","cf_sql_varchar")> </cffunction></cfcomponent>
The following table describes the methods used to describe the entity:
Method Description
setTableName Defines the name of the entity on the storage medium. For examplewhen storing the entity on the database, this would be the table name; orif using XML documents for storage, then this would be the file name ofthe XML document. Calling this method is required.
Arguments:Name:
(string, required) The name to use.setPrimaryKey Defines the field that is the identity field for the entity. DAOFactoryrequires that all entities have a unique identity field. Calling this methodis required.
Arguments:Name:
(string, required) the name of the field
Type:
(string, required) the datatype of the field using standard “cf_sql_*”naming convention.setLabelField Defines a field as being the “labelfield for the entity. The use of this iscompletely optional and is only used when calling the getByLabel()method on the DAO. This is only provided as a quick way of locatingrecords for entities in which there is a field (other than the primary key)that can unique identify a record but in a human readable way (i.e. name,code, etc)
Arguments:
Page 2 of 6
 
DAOFactory
Name:
(string, required) the name of the field
Type:
(string, required) the datatype of the field using standard “cf_sql_*”naming convention.addColumn Use this function multiple times to describe each field on the entity. It isnot necessary to describe every single column or field of the entity, onlythose that will be used or affected by a DAO operation. For examplecolumns like timestamps that will take values automatically assigned bythe database, do not need to be defined.NOTE: The primary key or identity field of the entity, does not need to beadded using addColumn() since it is already declared by the use of thesetPrimaryKey() method.
Arguments:Name:
(string, required) the name of the field
Type:
(string, required) the datatype of the field using standard “cf_sql_*”naming convention. 
DAOFactory Configuration 
DAOFactory configuration is done via an XML document that is read by the DAOFactory.cfccomponent. This document indicates the type of mechanism that will be used to store the data(the dataProvider), the location of the application DAOs and any setting specific to the selectedstorage medium (paths, datasources, username, etc)The following is a sample configuration file for the current example:
<?xml version="1.0" encoding="ISO-8859-1"?><config><!-- define data provider type --><dataProviderType>xml</dataProviderType><!-- this is the path to where the DAOs for the application are stored --><clientDAOPath>DAOFactory.db.</clientDAOPath><properties><!-- xml storage --><property name="dataRoot" value="/DAOFactory/data/" /></properties></config>
Tag Name Parent Tag Description<config> Document root<dataProviderType> <config> Defines the type of data provider to use. To use agiven data provider, the correspondingimplementation tag must exist on the DAOFactory
Page 3 of 6
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...