Related Categories: Web Dev

I have often thought that many smaller applications could be more useful if they didn't use a database.  It isn't that I don't like databases, but all to often they become a barrier for adoption, testing, etc.   Those apps could easily run without a DB.  Easier to test and deploy in theory.    Plus writting applications to support multiple DB platforms can be a pain.

Wouldn't it be nice if those small applications that are read only for the most part didn't use a database at all?  On the other hand, writing those apps using only xml (or another format) can be a bit more of a pain than it is worth.  Using a database makes it so much easier really.  None of it is rocket science, but hey we use ColdFusion because it is easy right?  Why make our lives more complicated when we don't need to.

That is why DataMgr caught my eye last month.  It looked like the perfect tool for quickly and easily using an xml doc as a datasource.  I set out to make a quick sample web site as proof of concept and indeed it was quite easy.  So I figured I would post a bit about how it can be used. Note: Steve just posted his own entry on this topic as well.

DataMgr has a feature that lets one use a simulated database.  Very handy for testing, etc.  Not much has been written about using that as the primary db however.  

To do so you first need an xml document. First you specify the table structure.  I have just one table for this:

  <table name="PageData">
    <field ColumnName="RecordID" CF_DataType="CF_SQL_INTEGER" PrimaryKey="true" Increment="true" />
    <field ColumnName="MyTitle" CF_DataType="CF_SQL_VARCHAR" Length="80" />
    <field ColumnName="ShortDescription" CF_DataType="CF_SQL_VARCHAR" Length="80" />
    <field ColumnName="FullText" CF_DataType="CF_SQL_LONGVARCHAR" />
     <field ColumnName="FullURL" CF_DataType="CF_SQL_LONGVARCHAR" />    
  </table>



Then the data.  Here is one row for an example.

    <row
    MyTitle="My DataMGR Sim Sample from XML."
    ShortDescription="Home Page"
    FullText="#htmleditformat("<p>This is my full text.</p> <p>This is only for testing.</p>")#"
    FullURL = "#htmleditformat("http://bryantwebconsulting.com/cfcs/DataMgr2.htm")#"
    />

 Name this file mydata.cfm

Now we load that table and data into memory using the Application.cfm in my example.


<cfapplication name="dataMGRSimSample">

<cfif NOT isDefined('application.datamgr')>
    <cflock type="exclusive" scope="Application" timeout="10">
        <cfset Application.DataMgr = CreateObject("component","DataMgr").init("junkdb","Sim")>
    </cflock>
    
    <!--- This loads the data for the site.  Only do this when needed. --->
    <cfinclude template="mydata.cfm">
    
    <cfset Application.DataMgr.loadXML(mydata, true, true)>
    <!--- END  --->
</cfif>



Thats it!  We have now got all the data we need to query in any way we want.

For my home page I may do this:

<cfset myfilter = structNew()>
<cfset myfilter.RecordID = '1'>
<cfset mytabledata = application.DataMgr.getRecords('PageData', myfilter)>


 
I have just queried the sim db and gotten the record with recordID 1 from the PageData TBL.

Alternatively if I ran :

<cfset mytabledata = application.DataMgr.getRecords('PageData')>

I would get all records from that table.

I then output just like a query.

<cfoutput query="mytabledata">#mytitle#</cfoutput>


I have made a quick little example with a few pages. It isn't sexy by any means, but it shows how to make the data, load it and display it.  Just copy into a folder of your site and run. Download the zip here. What this code doesn't do is update the data in memory or update the XML doc.  Not hard to do, but will have to wait for another entry.  For a more detailed example site, check out my affiliate web site.  It is very self serving (I get money if people sign up) and was made just to test out this idea.  It uses multiple queries, filters, etc.  It took some time only because I had to research each program and write up little 'reviews' the code itself was cake.

I am not sold yet that it is completely safe or best practice  At one point is the benefit lost?   Plus there are a few things one would NOT want to do such as storing user/passwords in xml for example.   Seems to me it is worth pursuing.


Like this entry? Subscribe to my blog.

Comments (moderation on)

Sponsors


Savvy Content Manager