ColdFusion Structure Functions
ColdFusionI Heart ColdFusion
As I dabble more and more with CFCs and CFScript I find myself liking them more and more. It’s like falling in love with ColdFusion all over again. At the beginning of the year I have to admit I was getting pretty bored with procedural type code. Every application I wrote was starting to look the same just with different colors and different numbers. But now it’s a brave new world with these CFCs and OO ideas. Now I’ll be the first to tell you I’m far away from writing applications that are true OO, in fact they still might be a bit procedural, but I’m getting there.
The simple fact that a single line of code like this can call a query brings a smile to my face and makes coding fun again!
<cfscript>
qCycleSummary = application.lsmtm.gatewayBill.getBillCycleSummary();
</cfscript>
If fact one of the newer tricks I’ve learned is calling a function that returns a structure of queries rather than a single query. For example, before I might write something like this…
<cfscript>
qCycle = application.lsmtm.gatewayBill.getBillCycle();
qCycleSummary = application.lsmtm.gatewayBill.getBillCycleSummary();
qBill = application.lsmtm.gatewayBill.getBills();
</cfscript>
Which is still cool, three lines of code, returns three queries.
However now I’ve figured out you can call all those queries with one line of code. How, you may ask? Like so…
sCycleInfo = application.lsmtm.gatewayBill.getCycleStruct();
In this example sCycleInfo now contains the structure of queries and they can now be referenced in cfoutput tags like:
<cfscript>
sCycleInfo.qCycle
sCycleInfo.qCycleSummary
sCycleInfo.qBill
</cfscript>
How cool is that? I like it, the more dots the better I always say! J For example sCycleInfo.qCycle.recordCount would give the record count for that query for example. Look at those dots!!
Now this is what my function (getCycleStruct) looks like. Note: I am passing parameters to this function but I removed them to make it a bit cleaner for this post. I also tab in and all that stuff, just in case those look messy below.
<cffunction name="getCycleStruct" access="public" returntype="struct" output="false"
hint="pull the cycle data to be worked">
<cfscript>
var returnStruct = structNew();
returnStruct.qCycle = getCycle();
returnStruct.qCycleSummary = getCycleSummary();
returnStruct.qBill = application.lsmtm.gatewayFeature.getBills();
return returnStruct;
</cfscript>
</cffunction>
Notice that this function is pretty easy just a few lines of code itself and that it has a return type of structure. All that is happening here is that this function is calling 3 different functions. I designed it this way so that any one of these 3 function can be called independently of each other. Here is an example of one of those functions
<cffunction name="getBill" access="public" returntype="query" output="false"
hint="pull a summary of bills">
<cfscript>
var qrySelect = "";
</cfscript>
<cfquery name="qrySelect" datasource="#variables.dsn#" username="#variables.dbuser#" password="#variables.dbPassword#">
SELECT BillCycle,BillMonth,BillYear,
COUNT(Entry_ID) AS BillCount,
SUM(AmountDue) AS AmountDue,
SUM(RecurringCharge) AS RecurringCharge,
SUM(OneTimeCharge) AS OneTimeCharge,
SUM(UsageCharge) AS UsageCharge,
SUM(FederalTax) AS FederalTax,
SUM(StateTax) AS StateTax,
SUM(LocalTax) AS LocalTax
FROM LSMTM_Bill
GROUP BY BillCycle,BillMonth,BillYear
ORDER BY BillCycle,BillYear DESC,BillMonth DESC
</cfquery>
<cfreturn qrySelect />
</cffunction>
Just a simple query function. But the beauty of all this CFC stuff is that you can call that single function or the structure form any one of your CF pages with just a few lines of code. No more writing the queries over and over again on different pages. Depending how you have it set up it may be just one line of code. Add a column to your query? No problem, just add it to you CFC and it’s available to all the pages calling that function.
I know CFCs have been out a while now but I still consider myself a newbie at them. So I’m sharing this code with other newbies out there hoping it will save them some code writing down the road. This concludes the post on why “I Heart ColdFusion”





Loading....