Solved

Createobject - cfinvoke

Posted on 2012-03-22
7
331 Views
Last Modified: 2012-03-22
Hello experts.
I need help to replace the following createobject with cfinvoke.
cfm
<cfset objsheduletask = createobject("component","com.scheduletasks").init(request.dsn)/>
<cfset objsheduletask.fsendundelivered(request.homeurl)/>

cfc
   <cffunction name="init" access="public" output="false" returntype="any">
        <cfargument name="dsn" type="string" required="true">
        <cfset variables.dsn = arguments.dsn>
        <cfreturn this>
   </cffunction>
   <cffunction name="fsendundelivered" returntype="boolean" access="public">
     <cfargument name="homeurl" type="string" required="yes">
 <cfquery name="....." datasource="#variables.dsn#">
.....
     
     <cfreturn true>
   </cffunction>

I could use:
cfm.
<cfinvoke component="com.scheduletasks" method="fsendundelivered">
 <cfinvokeargument name="homeurl" value="#request.homeurl#" >
</cfinvoke>
cfc
<cffunction name="fsendundelivered" returntype="boolean" access="public">
     <cfargument name="homeurl" type="string" required="yes">
      <cfquery name="....." datasource="#request.dsn#">
      .......
     <cfreturn true>
   </cffunction>
Is this correct?
Is there any better way?
0
Comment
Question by:Panos
7 Comments
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 37752056
here u should should

 <cfinvokeargument name="homeurl" value="#request.homeurl#" >

rest i m not sure what u are asking abt
0
 
LVL 2

Author Comment

by:Panos
ID: 37752078
Hi myselfrandhawa
I did correct this the same time you posted your comment.


Using the createobject i use the init function to set the dsn.
Using cfinvoke i have to delete the init function and use the request.dsn value.

I'm asking if i do this right
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 450 total points
ID: 37752282
Nothing technically wrong with the way you have it. But using a #request# variable makes the cfc slightly less modular.

Is there any better way?

If you're sure the component is stateless, and not tied to a specific session, yeah. Whichever you're using it's better to create stateless components once (in OnApplicationStart). Then store them in your application scope.  Using your original init() method:

       <cfinvoke component="com.scheduletasks" method="init"
                   dsn="#request.dsn" returnVariable="theObject" />
        <cfset application.scheduleTasks = theObject >

Then when you need to call one of the functions use:

    <cfinvoke component="#application.scheduleTasks#" method="fsendundelivered">
          <cfinvokeargument name="homeurl" value="#request.homeurl#" >
     </cfinvoke>

Just make sure ALL of your function variables are properly var scoped. Otherwise you'll have threading problems.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 50 total points
ID: 37752347
I'm curious why the change from createObject to cfinvoke.  To me createobject is a cleaner, more intuitive style and easier to assign to an application scope


<cfset application.sheduleTasks = createobject("component","com.scheduletasks").init(request.dsn)/>

Why would you want to change away from it?  (Other than a person preference to cfinvoke?)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 37752406
Possible hosting restriction? Though my preference is createObject too..
0
 
LVL 2

Author Comment

by:Panos
ID: 37752471
Hi agx and gdemaria
Thank you for your posts.
Agx is right . There is a hosting restriction when i use createobject edit a scheduletask.
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 37752581
Thank you
regards panos
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now