This tutorial will show you how to create an object data factory. This can allow you to easily extend your objects and reference everything from a single location in your application; yet allow you to extend to multiple database types, etc.
I used to create objects; then call them individually. The problem with creating your application like that is that it becomes tedious and hard to maintain going forward. Keep in mind that maintenance accounts for 80% of the life of the application. So making it easier to maintain or extend it will save you time and money!
Being that everything seems to be going OO (object oriented) these days; I wanted to explain what a factory was and how to create one for yourself.
So let’s use a test base "fictional" application for this tutorial. We’ll imagine that we are creating an application that keeps track of your contacts. This tutorial will not create the full application; but it will be used to simply demonstrate an easy way to explain a specific section of the application and how to approach in OO (using a data factory).
First let's see what a contacts application would have to make it work:
-People
-Categories
Next let’s look into how the folder structure would exist within the application:
Now that we have a brief overview of the application; the first thing you want to do is to create a file called "Contacts.cfc". This will be your actual factory. This will invoke the necessary file in the system (for the right DB type) and then extend to the code file. Now we’ll begin by looking at the "Contacts.cfc" file; in the file put the following code:
<!--- Contacts.cfc code (this is the factory) --->
<cfcomponent>
<cffunction name="init" output="false" returnType="contacts">
<!--- pass in certain values used throughout the application --->
<cfargument name="datasource" type="string" required="yes" />
<cfargument name="dbType" type="string" required="yes" />
<cfscript>
//This is the datasource name for the system
variables.datasource = Arguments.datasource;
// This will define the type of database you are using (i.e. MySQL, MSSQL, etc)
variables.dbType = Arguments.dbType;
//This is the actual people system.
this.people = createObject("component", "com." & variables.dbType & ".people").init(datasource);
//this.categories = createObject("component", "com." & variables.dbType & ".categories").init(datasource); (
//When you finish creating the categories files; uncomment this code to make it be called by the factory
//Return the factory (This makes it usable)
Return this;
</cfscript>
</cffunction>
</cfcomponent>
That is all the code that the factory will contain. What this will do is basically create a connection gateway and based on the database type it will load the appropriate objects.
Now let’s use the example of just MSSQL for this tutorial (however you can extend it the same way for the other databases).
We will begin by creating a file called "people.cfc" in the "core" folder and the "mssql" folder.
Let’s open up the people.cfc in the "mssql" folder and place this code in it:
<cfcomponent extends="com.core.people">
</cfcomponent>
Basically this file will extend to the "core" file. Think of it like this; if the function you are calling exists in this page it will run it; if it does not then it will look in the "core" file and run it there. This allows you to customize your "mssql" file to just run "mssql" content (or oracle in the oracle file, etc). So functions that are not DB dependant can live in the core file. If the actual data then becomes DB dependant then put it in the appropriate file.
Now let’s open up the people.cfc in the "core" folder and place this code in it.
<cfcomponent>
<cffunction name="init" output="false" returnType="people">
<cfargument name="datasource" type="string" required="yes" />
<cfset variables.datasource = arguments.datasource />
<cfreturn this />
</cffunction>
<cffunction name="getPeople" output="false">
<cfargument name="peopleID" type="numeric" required="yes" />
<!--- Create the logic to get the people from the database --->
</cffunction>
<cffunction name="setPeople" ouput="false">
<cfargument name="peopleStructure" type="struct" required="yes" />
<!--- Create the logic to set the people structure in the database --->
</cffunction>
</cfcomponent>
Now with that in mind… let’s say that you needed to for example return the inserted ID in the setPeople. Since that is specific to each Database type; you could create that exact function in the "DB Type specific" file and that would run first and therefore allow you to extend to additional database types, etc.
To make this all work now; simply create an "Application.cfc" and put this in it (this will go in the root of your site).
<cfcomponent output="false">
<cfscript>
this.name = "MyApplicationName";
this.applicationTimeout = createTimeSpan(0,2,0,0);
this.clientManagement = false;
this.sessionManagement = true;
this.sessionTimeout = createTimeSpan(0,0,20,0);
</cfscript>
<cffunction name="onApplicationStart" returnType="boolean" output="false">
<cfreturn true>
</cffunction>
<cffunction name="onRequestStart" returnType="boolean" output="false">
<cfargument name="thePage" type="string" required="true">
<!--- Generate the conenction to the data factory --->
<cfscript>
Application.datasource = "MyApplication";
Application.dbType = "MSSQL";
Application.Contacts = createObject("component", "com.contacts").Init(
datasource=Application.datasource,
dbType=Application.dbType
);
</cfscript>
<cfreturn true>
</cffunction>
</cfcomponent>
Now create an index.cfm page and dump your factory (this will show you what you have available and what functions you can use):
<cfdump var="#application.contacts#" />
Well, I hope this tutorial has shown you the first steps of creating an OO Data Factory. Hope this helps you move forward with your application development.