For my application, I'm caching collection objects (such as com.app.userCollection) which basically just store query data. In my individual objects (such as com.app.user), I've also got obtain{CFCType}By{Key}() methods that directly query the database for themselves via passed ID or other key.
-- pseudo-code for ya: --
component userCollection {
private query _data;
public query getList() {
query _data, cachedWithin(0,6,0,0) {
select * from users
}
return _data;
}
private void updateList() {
query _data, cachedWithin(0,0,0,0) {
select * from users
}
}
public void deleteUser(userID) {
var delQry;
query delQry {
delete from users where id = arguments.userID
}
updateList()
}
}
component user {
private string _name;
private int _id;
private array _roles;
public void obtainUserById(userID) {
var getUser;
query getUser {
select * from users where id = arguments.userID
}
_name = getUser.name;
...
}
public string getName {
return _name
}
public void update {
var updUser
query updUser {
update users set name=_name where userID=_id
}
}
}
---etc. you get the point---
It occurs to me that I've already got much, if not most, of this data cached inside my collection objects. Is it a good pattern to create my individual objects inside my collection objects, and return them from an obtain function? I'm worried about losing what loose coupling I've got, and about state management issues, as the more complex objects will then need to know of the collections objects I've got cached in the application scope in order to create their sub-elements (flight contains legs, pilot & copilot, and passengerlist, which contains passengers).
Should I leave the obtain methods inside the individual objects, where I'll need to connect to the database fairly often? Should I move the obtain methods to the collection objects, where I'll need to let the individual objects know about the environment they're in? What are your thoughts on this?
Thanks,
ecd.
--
Eric C. Davis
