why not create the table definitions straight from the database? (ooh man probably gonna get flammed for this)
Have a look at Doug Hughes Table object CFC. http://www.doughughes.net/index.cfm/page-blogLink/entryId-129. I talked about this topic last year but there didn't seem to be much interest in it at the time hence I didn't deem my code worthy for sharing nevertheless I have MSSQL, MySQL, and MS Access support. Each database has support (although Access is a bit limited) for being able to read table definitions and hence extract out the names of the fields and their types. Here's an example of how I create a DAO. application.projectTasks = CreateObject('Component',"\TIMS\Com\tableDAO"); application.projectTasks.init(dataSource=dataSource,table="ProjectTasks",PK="ProjectTaskID"); Doing a query is a simple as application.projectTasks.select("completed=false"); (options arguments here for ordering etc) Adding a new record for example pid = application.projectTasks.init(); (this is an interesting method as like getting the table definitions, the way to return the new primary key is different for each database type but its abstracted out :) Anyway, lots of methods for updating, deleting etc. Eg setting individual column values, updating a record from a form etc. If I need to create a custom object I simple inherit the base table object. Eg application.DAO.categories = CreateObject('Component',"model/CategoryDAO"); application.DAO.categories.init(datasource=dataSource); Inside that ojbect you can just use the inherited methods for new functions or write CRUD on rare occasion eg <cffunction name="getUserID" hint="Gets users ID or creates if not existing" returnType="numeric" > <cfargument name="userID" type="numeric" required="true"> <script> record = getRecordDetails(arguments.userID); if (NOT Len(record.userID) return insertEmpty(); else return record.userID; </script> </cffunction> Ok now the downsides and whatnot. This doesn't work where u need to use multiple tables, same concept as DAO vs gateway objects. In this case I just create a regular object and write CRUD statements as per usual or sometimes use some temp table objects to do the dirty work hehe. Also it's not particularly efficient as the constructor first checks if it trying to access an mysql database and then mssql and then lastly access, haha now you see why I didn't want to release it? - what would be nice would be caching of the table schema's. Anyhoo, all my work is Intranet based so I dont care about too much about performance hehe. Not saying any of this is good advice, just that this has saved me countless hours of development time. I estimated I was spending around 20% of time on database code, used to drive me crazy writing near identical CRUD statements for every damn DAO CFC. Now its more like 5% and even tho it's not as clean/abstract and OO like as gateways/beans etc it works and lets me get on with the more important stuff, eg design patterns, User Interface, etc. Alright probably better shutup now except to say I recently started looking at PHP database abstraction libraries and was shocked how far behind the curve they are in this area. Either they abstract the database away but it's not object oriented, or it's object oriented but the database abstraction and CRUD creation is done by hand. Doh.. hehe. - ahh now i see since writing this email others have suggested Doug's code. hahah ok TiM -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Cameron Childress Sent: Thursday, January 12, 2006 3:47 PM To: [email protected] Subject: [CFCDev] DAOFactory and BeanFactory based on XML Schema I am playing with the idea of building some bean and DAO factories that will read table definitions from a set of XML files and dynamically create DAO and Beans based on these definitions. I was also thinking about including some Utilities of some type to build the actual database tables from the XML schemas too. One change to the XML Schema will update the Beans, DAOs and the databse. Question is, do any existing tools (CF or otherwise) do this already? I'd rather have a head start on implementation and don't want to re-build something that already exists. Thanks! -Cameron -- Cameron Childress Sumo Consulting Inc http://www.sumoc.com --- cell: 678.637.5072 aim: cameroncf email: [EMAIL PROTECTED] ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ---------------------------------------------------------- You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email. CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com). An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
