Object-oriented Programming in ColdFusion

Matt Gifford

Mentioned 3

Break free from procedural programming and learn how to optimize your applications and enhance your skills using objects and design patterns.

More on Amazon.com

Mentioned in questions and answers.

I am a web application developer using Coldfusion 9. I have incorporated the use of cfc's in my code for some time. I would done like to incorporate more OOP principles in my design.

I am looking for books, tutorials, videos, etc that cover application design and interaction using OOP principles.

Most of the material I've found covers the object design but not they are used in application design.

Thank you for your suggestions.

Once you are past the fundamentals of Object-Oriented concepts, you need a solid handle on how to apply those concepts to your real-world projects.

An understanding of the basic Design Patterns is of significant value. You will want to start with Design Patterns: Elements of Reusable Object-Oriented Software, which defines these patterns, many of which are commonly used in OO design and development. For a more real-world, personal approach to the patterns, consider Head First Design Patterns, which builds off the first book, and demonstrates real problems that poorly designed OO systems suffer from, and how you can begin to think about solving them.

You will also want references to actual ColdFusion-related implementations of OO systems. Object-Oriented Programming in ColdFusion is a very good start.

Finally, you should immerse yourself in existing, well-known ColdFusion applications that employ OO techniques. ColdBox is a framework for building CFML-based websites that implements many OO techniques, including Aspect Oriented Programming and Dependency Injection and you can learn a lot from this framework and websites that have been build using it.

I've a simple CFC file that contains the different functions for different queries & a separate function that displays the reports dynamically based on the queries.

All the queries work except one which returns approx. 50k rows. Its just a blank screen & I get no error. When I dump the query results, they do get dumped on the screen but while displaying it in a tabular report it gives nothing.

I've a another CFM file that returns 100k rows & works fine.

Below is CFC code that is not working.

<cfcomponent>
    <cfparam name="qry1" default="">
<cffunction name="showqry1" access="remote">
    <cfquery name="qry1" dataSource="myds"  cachedwithin="#CreateTimeSpan(0, 2, 0, 0)#">
        <!--- myquery --->
    </cfquery>
    <cfset Display()>
</cffunction>
<cffunction name="showqry2" access="remote">
    <cfquery name="qry1" dataSource="myds"  cachedwithin="#CreateTimeSpan(0, 2, 0, 0)#">
        <!--- myquery --->
    </cfquery>
    <cfset Display()>
</cffunction>

<cffunction name="Display" access="private">
    <cfdump var="#rptQry#" top="20">   
    <cfsavecontent variable="myrpt">
        <table>
                <!--- make a tabular report here using cfloop over the query--->
        </table>
    </cfsavecontent>

   <cfform action="test.cfm" method="post" name="ExcelData">
        <cfoutput>#myrpt#</cfoutput>
        <cfinput type="hidden" name="excel_data" value="#myrpt#"/><!---This is giving the error. --->
        <cfinput type="submit" name="test" value="Export" />
    </cfform>

</cffunction>  
</cfcomponent>

Any idea why CFM works fine but CFC doesn't? I need my CFC to work & dont want it to convert it to CFM...

UPDATE: I've added a comment("This is giving the error") in the above code that is cause of the error. Irrespective of CFC/CFM this doesn't work.

I use the hidden field to pass data to another file which exports data to excel. Any alternate suggestions??

Any help is highly appreciated.

Thanks

You still need to read that doc I put in the comment about how to ask questions clearly.

However you are putting your recordset into a variable qry1, but trying to dump a variable rptQry. But that would just error, unless there's some code you're not showing us that populates rptQry.

Also, from a coding practice POV, you shouldn't really be outputting stuff in your functions: that's best done in a CFM page. Get your data with a CFC method; display it with a CFM.

I also recommend you read up on how to do OO with CFML (or in general). Perhaps get Matt Gifford's book "Object-Oriented Programming in ColdFusion"