Hi Matthieu, Bobby,
The USER table has a 1-1 relationship with the CONTACTINFO table. So 1 user,
may have no more, and no less, than 1 set of contact info. To add a USER, I
have no choice but to add the CONTACTINFO first, get the new autogenerated
ID, and use that to add a User. In the case of an error, the new CONTACTINFO
is deleted - so no orphans.
Keep in mind that CONTACTINFO is used for more than just USERs. So while
there are ATLEAST as many CONTACTINFO records as there are USER records,
there are actually more CONTACTINFO records.
For that reason, each of those tables needs to have its own CFC as well.
The question is: How much coupling should go on between those 2 cfcs? Where
should I call the CONTACTINFO methods? Here are 3 different ways to
accomplish the same thing:
<!--- Example A: Index.cfm with no dependencies between CFCs --->
<cfscript>
// Add Contact to DB
ContactObj=createObject('ContactInfo');
ContactObj.setFullName('Matthieu');
ContactObj.AddToDB();
// Add User to DB
UserObj=createObject('User');
UserObj.setRelatedContactID(ContactObj.getContactID());
UserObj.AddToDB();
</cfscript>
<!--- Example B: Index.cfm is aware of dependencies between CFCs --->
<cfscript>
UserObj=createObject('User');
// Add Contact to DB through User
UserObj.getContactObj().setFullName('Matthieu');
UserObj.getContactObj().AddToDB();
// Add User to DB
UserObj.setRelatedContactID(ContactObj.getContactID());
UserObj.AddToDB();
</cfscript>
<!--- Example C: Index.cfm is NOT aware of existing dependencies --->
<cfscript>
// Add Contact AND User
UserObj=createObject('User');
UserObj.setFullName('Matthieu');
UserObj.AddToDB();
</cfscript>
PROS & CONS
Example A: Index.cfm with no dependencies between CFCs
PRO: No coupling between CFCs
CON: Dependencies are not hidden, app has to be very aware of them
CON: Not always possible, sometimes you HAVE TO define a dependency
Example B: Index.cfm is aware of dependencies between CFCs
PRO: MINOR coupling between CFCs
PRO: Easier, everything for USER get handled through USER
CON: You must be aware of the complex structure of the cfc
CON: You still have to know to add CONTACTINFO before u add USER
Example C: Index.cfm is NOT aware of existing dependencies
PRO: Complex dependencies are hidden! You just have to AddUser() and
this will take care of CONTACTINFO
CON: High coupling between components, especially if you duplicate
getter/setter methods. Changes in CONTACTINFO.cfc will
necessitate changes in USER.cfc.
To me it seems like the valid choices are Example A and Example C, because
why even introduce a non-required dependency if it doesn't significantly
simplify matters...
Thoughts?
Baz
-----Original Message-----
From: Cornillon, Matthieu (Consultant)
[mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 03, 2006 3:56 PM
To: CF-Talk
Subject: RE: Adding Multiple Objects
Well, I would say that you can add contact info without adding a user
(multiple contacts, right?) but you can't add a user without adding
contact info. So, it seems to me that you first instantiate the user
and then instantiate the contact info entry, not the other way around
(i.e., AddUser() first).
Some reasons why:
1) What if you want to add a second contact later? Your class may not
support this now, but if you want to add the ability to do it, you would
have to rework things significantly if you called AddContactInfo()
first.
2) If you call AddContactInfo() first and it calls AddUser()
automatically, what if AddUser() throws an error? Then you are stuck
with an orphaned contact entry.
HTH,
Matthieu
-----Original Message-----
From: Baz [mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 03, 2006 3:37 PM
To: CF-Talk
Subject: Adding Multiple Objects
Hi,
If I had a USER cfc and a CONTACTINFO cfc and no user should exist
without having contact info - whats a good way to add a user to the DB?
Should I call AddUser() which in turn calls AddContactInfo()?
OR, should I call AddContactInfo() first, then take the ContactID and
pass it into AddUser()?
Cheers,
Baz
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking
application. Start tracking and documenting hours spent on a project or with a
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:228316
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54