I put all interaction with the database in CFCs, with each action happening
once so that the same result always comes back regardless of where  the
logic for the database activity is needed.  If you write, test and debug
these CFCs as an early step in your project, downstream development that
uses them becomes much more efficient. Once your CFCs are written and
tested all programmers should be required to use them.  It saves much time
and trouble if everyone is interacting with the database exactly the same.

I use one CFC for each data table, with the same name as the table. It
includes all the selects, gets, sets, puts and initialization for the
object.  If the data table contains foreign key fields, I will do a select
in the CFC that joins the foreign key to the appropriate table and returns
all the information necessary for display in a structure.

Each CFC extends  DATAMANAGER.CFC which is a custom CFC that controls the
DSN being used, confirms permissions, cleans data being sent to the
database to trap any hacker attacks, manages failed transactions,  logs the
result of any catch-try failures, etc.  This allows all my error management
and logging to happen in one place.

I only use stored procedures for complex database interactions or handling
massive amounts of data  that will run faster on the database than doing
the calculation and logic with CFML, and I call them from the CFC for the
related table so that I can use the DATAMANAGER.CFC as above.  Again, this
allows me to have total control over the call to the database, and always
returns the same result regardless of where the stored proc is needed in
the business or display logic.

I never put queries in CFM pages or any display pages because I want the
code where I can find it readily and if the database changes I only have to
make those changes in one place. Frankly I just don't think database
interaction belongs with the business and display logic.  I do occasionally
put a query of queries in CFM pages to re-sort or sub-select data items for
display.

On Tue, Oct 23, 2012 at 6:55 AM, Shannon Rhodes <shan...@rhodesedge.com>wrote:

>
> I'm drafting our first set of code standards, and I'm running into a
> philosophical debate which I'd like to open up to the community.
>

...(snip)

>
> So...inline code if reusability is unlikely?  Everything in CFCs?  Forget
> CFCs, go to stored procedures?  Some rationale for when to use what?  Very
> interested in hearing your opinions!  Thanks.
>


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:352986
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to