On May 9, 2004, at 10:45 AM, Jim Davis wrote:
Firstly I had been using DB-specific includes (called dynamically based on a
"DSN.type" property) to handle multiple database engines in my app. For
example the "DP_Session.save()" method could, depending on the DSN contained
in its parent "DP_App" component could call either the
"MSSQLServer2000/DP_Session_Save.cfm" or the
"MSAccess2000/DP_Session_Save.cfm" include. These includes would contain
only the DB-specific code for the method.

This is a classic case of polymorphism - you call a method but you want different things to happen at run-time.


Any other options?

Create your base CFC - as you have now - containing the generic code. Create a separate CFC for each database type that extends your base CFC. In those CFCs, override just the methods that are different for each database. Use the DSN.type property to determine which derived CFC to instantiate but work with the base CFC type everywhere else in the program (you could use a factory to create the actual CFC).


baseDB.cfc:
<cfcomponent>
        <cffunction name="doStuff" ..>
                ... common stuff ...
                <cfset dbSpecificFunc() />
                ... more common stuff ...
                <cfset anotherDBspecificFunc() />
                ... more common stuff ...
        </cffunction>
        <cffunction name="dbSpecificFunc" ..>
                <cfthrow message="dbSpecificFunc not implemented" />
        </cffunction>
        <cffunction name="anotherDBSpecificFunc" ..>
                <cfthrow message="anotherDBSpecificFunc not implemented" />
        </cffunction>
</cfcomponent>

sqlserver.cfc:
<cfcomponent extends="baseDB">
        <cffunction name="dbSpecificFunc" ..>
                ... SQL Server code ...
        </cffunction>
        <cffunction name="anotherDBSpecificFunc" ..>
                ... SQL Server code ...
        </cffunction>
</cfcomponent>

mysql.cfc:
<cfcomponent extends="baseDB">
        <cffunction name="dbSpecificFunc" ..>
                ... mySQL code ...
        </cffunction>
        <cffunction name="anotherDBSpecificFunc" ..>
                ... mySQL code ...
        </cffunction>
</cfcomponent>

Your factory code would switch on DSN.type and instantiate the appropriate CFC:

        <cffunction name="dbFactory" returntype="baseDB">
                <cfargument name="dbType" type="string"/>
                <cfswitch expression="#arguments.dbType#">
                <cfcase value="sqlserver">
                        <cfreturn createObject("component","sqlserver").init() />
                </cfcase>
                <cfcase value="mysql">
                        <cfreturn createObject("component","mysql").init() />
                </cfcase>
                </cfswitch>
        </cffunction>

Regards,
Sean

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' in the message of the email.


CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to