Consider the following two db tables
User
UserID PrimaryKey
firstname
lastname
Security
SecurityID PrimaryKey
UserID ForeignKey
Permission
All database tables have Create, Read, Update, Delete operations (CRUD)
CRUD operations can exist in several places
- Inside of
<cfquery>
tags
- Inside of Stored procedures
- Others
The thing is all the CRUD operations belong together in their own way. Consider making a User object (user.cfc
).
<cfcomponent>
<cffunction name="create"></cffunction>
<cffunction name="read"></cffunction>
<cffunction name="update"></cffunction>
<cffunction name="delete"></cffunction>
</cfcomponent>
Security is a part of user management, so is the object a one to one match to the db table? In some environments like ORM the answer is yes, in others not.
If you consider security to be a part of user managment, your user.cfc
might look like this
<cfcomponent>
<cffunction name="create"></cffunction>
<cffunction name="read" hint="Read will also read security info"></cffunction>
<cffunction name="update" hint="Perhaps this can update security too"></cffunction>
<cffunction name="delete" hint="Delete will also delete security info"></cffunction>
<cffunction name="create_security"></cffunction>
<cffunction name="read_secrity" hint="This may not even be needed"></cffunction>
<cffunction name="update_security"></cffunction>
<cffunction name="delete_security" hint="This may not even be needed"></cffunction>
</cfcomponent>
At the end of the day you may find that you need far fewer objects (*.cfc
s) than tables.
OK, now you have you user.cfc
what do you do with it? It can be attached to you the rest of your app in various different ways
- application.User = new user();
- session.User = new user();
- request.User = new user();
Each one of these is very from the next. Before we go down the road of which is appropriate, we have to consider member data, and how long we want it around.
<cfcomponent>
<cfset this.userid = ""><!--- This always points to the user I want to interact with --->
<cffunction name="create"></cffunction>
<cffunction name="read"></cffunction>
<cffunction name="update"></cffunction>
<cffunction name="delete"></cffunction>
</cfcomponent>
It is likely that your CRUD operations are going to interact with the same UserID
for all their operations. You may find that after you update a record, you will often read it. Rather than always stating which UserID
you are interacting with, you may just want to set it up once, and have all the functions just use the same one.
OK, now let's get back over to where you will be using them
application.User
Only one User
object will exist in the the whole system. It will be created when the request comes in onthe site. This object will be shared for every request. If you attach your user
object here, that suggests that all requests will be looking at the same user.
session.User
One User
object will exist for a given end-user in the outside world. It will separated from all other end-users. This suggests that each end user will be looking at their own user
AND that even as they click around the site, they will still be looking at the same user
request.User
One User
object will exist per request. It will only exist for a particular request, and then be discarded. This suggests that looking at at particular User
is meaningful on this request, but the next may be quite different, or maybe not even about users.
~~~~~~~~~~~~~~~
At the end of the day, you will need to decide how to bundle your DB interactions, and how long you will keep those bundled action together