In the next few series, our Contact-O-Matic will persist data in a database. The queries and relationships are simple and should translate over to any database. Our code will be organized into two types of components the DAO (Data Access Object) and the Gateway. These two components are common structures used in many Object Oriented systems and a quick google search will land you on endless descriptions for each. For our purposes, all we need to know is the DAO and Gateway will help us organize our code into sections. The DAO is for pulling back a single object, the Gateway for a query of objects.
Another architectural feature of our Contact-O-Matic is our ContactService.cfc component. Service components help tie your other components together. Note how simple it will be to add in new database functionality.
At the end of series 5 of our Contact-O-Matic, we had a ContactService component which returned the ContactType data , a single contact object and a list of contact objects. Even though we are going to add a DAO component as well as a Gateway component, they will be tied together within the service component.
Some of you might be asking yourselves why we have all of these components. After all, there are huge and complex ColdFusion applications written without a single component. Couldn't we just put our queries in the Controller and be done with it? That is a very valid question to ask. You should be proud of thinking of it. Question Everything!!!
The main point of these separations in our code is to isolate variability. Meaning, when the application needs a feature, or a new data column, the affected sections in our code are compartmentalized and perhaps only one section of code needs to change in order for the entire application to be upgraded with the newest feature.
The complete source code up to the end of series 5 is attached to this article. It will be easier to follow along if you use this code.
A few quick notes before we begin:
- Remember that the application files will be cached if the ModelGlue reload setting is 'false'. Any changes to Controllers, components created with ColdSpring, or stored in the Controllers will NOT change until you reload the application. This trips me up sometimes and my first step is to ALWAYS reload the framework
- The <include /> tag is processed by BlogCFC as a cfinclude. Thus, whenever I paste in an event-handler section with an <include tag, I always have to use the entity symbol '& lt;' or i'll get an error when I post it. I try to leave a small note at the end of the code block about changing this in your code. If I forget, just know an XML tag will never actually start with '& lt;'
- I am writing this tutorial with MSSQL server (delete derogatory note about Microsoft Products). Your database might have a slightly different syntax and I trust you to convert it over
Lastly, our database will have two tables:
|varchar( 255 )
Without further ado, let us get to the next piece in our Series, database support.