Which is a better practice? GetJsonRequest() shoul...
# cfml-general
d
Which is a better practice? GetJsonRequest() should be available to call from every remote CFC. Should I use application scope or request scope?
Copy code
component {

	void function onCFCRequest(string cfcname,string method,struct args){

        // Request
        request.GetJsonRequest = new utils.Request().GetJsonRequest;
 
		// invoke remote cfc
		invoke(arguments.cfcname,arguments.method,arguments.args);

		return;
	}

}
Or
Copy code
component {

    boolean function onApplicationStart(){

         application.GetJsonRequest = new utils.Request().GetJsonRequest;

    }

	void function onCFCRequest(string cfcname,string method,struct args){

		// invoke remote cfc
		invoke(arguments.cfcname,arguments.method,arguments.args);

		return;
	}

}
s
What is
utils.Request
?
d
where I have GetHttpRequestData(), deserializeJson() and some checks.
s
It's a CFC with no state, just functions?
d
correct
s
Why not just do
new utils.Request().GetJsonRequest()
where you need to call it? (i.e., instead of putting it in a scope and then having your application code reach out to global scopes -- which is bad practice)
d
Hmm. I did think about creating BaseRequestHandler.cfc and adding my functions there then my remote CFCs can extend that. But I thought this was cleaner. No good reason really.
I guess I'm used to coldbox, where some functions are built-in to the handlers.
So what happens when you reach out to global scopes?
I went with extending a base cfc that provides those functions.
👍🏻 1
b
> So what happens when you reach out to global scopes?
Application.GetJsonRequest
would then be available to everyone using the application, remote as well as non-remote users. It is poor design - and it can be risky - to expose functionality to the wrong user. In so doing, you would be increasing the vulnerability surface area.
request.GetJsonRequest
in
onCFCRequest
is better. However, you could still improve on this concept. For example, you could define
request.UtilsRequest = new utils.Request();
in
onCFCRequest
, then call
request.UtilsRequest.getJsonRequest()
wherever needed. This saves you from changing Application.cfc whenever functionality is added to or removed from Utils.Request.