Mike,

You are definitely on the right track here.  I like to encapsulate all  
my database access (or at least my inserts and updates) inside of  
components.  For instance, a User component with functions such as...

addUser
getUser
updateUser
removeUser

.. is a useful data structure.  Get functions might return a query, or  
another component with addition get functions for specific fields.  For  
instance, getUser might return a UserData component with functions such  
as...

getUserId
getFirstname
getLastname
getAddress
getCity
getState
getZipcode

The advantage to keeping as much database code in components as  
possible is all the usual advantages of encapsulation.  It has saved me  
before when having to switch database vendors; I had to rewrite a  
decent amount of SQL, and it was nice that I could just go into a  
single package and make all the changes to just a very small number of  
files and the rest of the application did not need any changes at all.

As far as what to return from set methods, you can certainly return a  
boolean if you like, but I typically don't return anything.  Instead, I  
will throw an exception if the operation was not successful using  
<cfthrow>.  Then the calling code wraps the function call in a <cftry>,  
and not only will I know when the operation didn't succeed, but I will  
also be able to figure out why.

Hope this helps.  If you find yourself really getting into components  
and you have a lot of questions, cf-talk is a great resource, but so is  
the cfcdev list.

Christian

On Wednesday, January 1, 2003, at 01:53 PM, Mike Miessen wrote:

> I am very new to cold fusion and have been looking at the CFC info and
> was wondering how to write a CFC to add information to a database.
> I think having functions to do this would save a lot of time because
> they are reusable and eliminates the need to rewrite several pages when
> the database structure is changed.
>
> We defined the tables like so;
>
> Table addressbook
> Id (tinyint 4 required)
> Firstname (varchar 50)
> Lastname (varchar 50)
> Address (varchar 100)
> City_id (varchar 50)
> State (char 2)
> Zipcode (varchar 10)
>
> I have a good example for writing a database query but not for adding
> records.  I assume my return value would consist of a Boolean value
> which would verify a successful (or not) update.  Can you please give  
> me
> some guidance on this?
>
>
>
> 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Signup for the Fusion Authority news alert and keep up with the latest news in 
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm

Reply via email to