Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set datasource name in CFC or CFC how to read the application.cfm 1

Status
Not open for further replies.

waquindev

Programmer
Nov 21, 2010
1
0
0
US
Hi,

Im new to CFC (coldfusion components), one thing I noticed is that it doesn't read my application.cfm.
So in my cfc file, i still have to set my datasourcename.

Normally Im used to set my DSN in one application file(application.cfm) so I dont have to set DSN on other pages or files..

Please help me on this, I wanted my cfc file as dynamic as possible and dont have to hardcode all over again my DSN.

Please see code below, notice that I have to re-set my DSN on my application.

Many thanks in advance.
Code:
<cfcomponent>

    [b]<cfset DSN = "msmdevTestDB">[/b]

  <cffunction name="addEmp" displayname="Add Employee" returntype="any">
	<cfargument name="first_name" type="string" required="yes" default="#form.first_name#">
	<cfargument name="last_name" type="any" required="yes" default="#form.lastname#" >
	<cfargument name="address" type="variablename" required="yes" default="#form.addressnew#">
	
	
	<cfquery name="addRecordEmp" datasource="[b]#DSN#[/b]">
			insert into EMPLOYEE (first_name,last_name,address)
			values('#arguments.first_name#','#arguments.last_name#','#arguments.address#')		
	</cfquery>
	
	<cfreturn true>
	
  </cffunction>


</cfcomponent>
 
Components are not supposed to read your Application.cfm/cfc file. Typically you inject whatever values are needed into the component when it is created. The component then stores those values in its variables scope for use by its functions.

Since database components are usually stateless, they are often created once when the application starts and stored in the application scope for reuse. The typical convention is to have an init() function that accepts whatever arguments the component needs to function (DSN, etectera). Once it is initialized, you can call the component's functions any where in your application using the application variable ie <cfset x = application.yourComponent.addEmp(..arguments.)>

Code:
<!--- initialized in OnApplicationStart of Application.cfc --->
Application.cfc
<cfcomponent>
    <cffunction name="onApplicationStart" ...>
        ... arguments ....
      <cfset application.yourComponent = createObject("component", "YourComponent").init(dsn=yourDSNName)>
    </cffunction>
</cfcomponent>

YourComponent.cfc
<cfcomponent>
    <cffunction name="init" ....>
        <cfargument name="dsn" ...>
        <cfset variables.DSN = arguments.dsn>
        <cfreturn this>
    </cffunction>

 <cffunction name="addEmp"  ...>
    ... arguments ... 
    
    <cfquery name="addRecordEmp" datasource="#varabibles.DSN#">
         ... INSERT QUERY ....
    </cfquery>
  </cffunction>

</cfcomponent>

Having said that, technically components do have access to other scopes like application, request etcetera. So they could read any variable stored in one of those scopes. But that is often considered bad form as it breaks encapsulation.

<cfargument name="last_name" type="any" required="yes" default="#form.lastname#" >
...
<cfquery name="addRecordEmp" datasource="#DSN#">

A few observations about the function

1) Those settings (required/default) contradict each other. Supplying a "default" it means the argument is optional. If it should be mandatory, remove the "default".

2) Usually you want to avoid referencing external scopes (like FORM) inside a function. As it creates unnecessary dependencies. Functions should be independent. They should not know, or care, where the arguments come from. Only that they receive the values needed to do their job.

3) Do not forget to VAR scope. Any variables that are only used within the function should be VAR scoped to prevent conflicts. That includes your query "name".




----------------------------------
 
Even though I very rarely use CFC's, this is great info for my toolbox. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top