To answer the question: do not use variables scope...
# fw1
s
To answer the question: do not use variables scope in controllers for request-specific data ever. Use
rc
or `local`/`var` scope variables only. The
variables
scope of a controller should only be used to cache global state (like an in-memory copy of a fixed lookup table or some such) and should either be initialized in the constructor method
init()
via dependency injection, or with careful locking as if dealing with
application
scope.
👍 2
👍🏻 1
c
Hi Sean Thanks very much for your advice. How about the use of the
variables
scope in services? https://github.com/framework-one/fw1/blob/develop/examples/userManager/model/services/user.cfc Can I assume that the:
Copy code
variables.users = {};
Used here, is to store every
user bean
that is created, updated or deleted, into a cached Struct? The idea, being that it is quicker to do an in-memory look up than a DB look up? And here, anyone with access to the user service, can add/update this Struct. I can see now, that this makes sense. Unfortunately, I should have realised a long time ago that this approach, would not suit my objective, of returning a pagination based list. My objective requires that different users might see a different user list, for a different page. In my case, the
variables
scope is not the correct mechanism for storing my list. My list is a subset of users, rather than all users.
s
I thought the answer was obvious and did not need to be stated, but here is an updated recommendation: do not use variables scope in controllers any singletons for request-specific data ever
I am pretty sure the FW/1 docs explicitly state this, probably in several places.
It's been the standard advice ever since CFCs were added to CFML back in CFMX 6 (well, they were messed up in 6 so let's say 6.1).
That usermanager example is imitating a database so the
variables.users
is like "global storage". When I rewrote the example in Clojure, I used an actual database instead (mind you, Clojure doesn't have objects anyway because it's a functional language).
👍 1
c
OK. I see. Thanks for clarifying this.
s
@Charles Robertson just pass in request specific data to the singleton, don't use them within it