Tripping On The Beans

Today I got a little time to hop back on my journey to OO and Model-Glue enlightenment. It's difficult to keep focused on this project because [insert standard I'm really busy excuse here]. Anyhow, I'm a little tripped up and I'm going to lay out my experience here for the sake of documenting my learning.

Here's the deal. I'm working with a simple user management module in my application. The code that I'm using is generated by the Illudium code generator. While I don't expect the code to be in final working code 'out of the box' (what box?) I have been pleasantly surprised with how workable it truly is. So here's my dilemma. I have a simple user bean that I'm using to capture the form inputs and pass to the userDAO. Let me side step for a moment for the sake of beginners and give a quick explanation of beans and DAO's. I'm going to quote from Doug Boude's lexicon entitled OO Terms In a ColdFusion Context.

Doug defines a bean as:

You can first off think of a bean as a small, solo object. It's somewhat simple, as objects go, and has no other purpose in life than to store a collection of related values, and the means to get and set them. For example, we have a User bean. It stores the values Firstname, Lastname, Age, Sex, and IQ. We are never allowed to DIRECTLY change or retrieve the bean's values, and can only do so using the bean's SETTER and GETTER methods, like so:

[cfscript]

myUser = CreateObject("component","UserBean");

userIQ = myUser.getIQ();

//change the IQ a little...

myUser.setIQ(userIQ + 10);

[/cfscript]

And a DAO as:

When you hear or read the acronym 'DAO', always think "database actions dealing with a single record". DAO is one of the three "Model sisters" (the other two are Gateway and Record (who also holds the nickname "Bean" in some circles)), and is a CFC that contains a collection of basic functions to allow one to create, read, update, and delete a single record in a database table. If your app needs to perform some action on a single record, create a DAO to help out with that task.

Another resource that I've just discovered but yet to really dig into is the ooGlossary.

Back to the topic. So my user form is pretty simple. My event handler gets the user bean and uses it to populate the form. Very nice for reusability. Essentially the same exact form can be used for adds and edits. The tricky part comes in with an add. With an empty bean comes an empty userId. Which doesn't work so good with a required element. Obviously I have to tweak something somewhere to make the add happen. The first thought that I had was to do something like this in my user form.

<cfif len(userFormBean.getuserId())>
   <cfset userId = userFormBean.getuserId()>
<cfelse>
   <cfset userId = createUUID()>
</cfif>

And then use #userId# to populate the form field. Something about this just doesn't feel right. I don't really think it's good form to go messing with the bean. Anyhow, I rolled with it just to see what would happen. So my controller took the user bean and passed it along to my user service which in turn was ready to happily serve that bean to my DAO to commit the data to my database. Unfortunately I stumbled upon the following MG error.

The argument USERID passed to function saveuser() is not of type string.

Very weird. Now hear me out here. Granted I am using generated code and I do not expect it to be perfect, but it appears that MG is actually stricter then CF when it comes to data types. So since my database datatype was varchar(35) to accommodate the UUID the code generator gave me a CF datatype of string. MG noticed that the userId that I passed was a UUID and stopped me dead in my tracks. Isn't a UUID also technically a string? CF would have gladly allowed the data to pass but not in MG. This occurrence as well as my earlier bad feeling about manipulating the userId on the form side of things lead me to rethink.

My thought track is now moving to my user bean component. I'm thinking of changing my setuserId() function to default the userId to #createUUID()#? Should I be changing all the userId datatypes in the generated code to be a uuid datatype? I'm not quite sure. I'm not specifically asking those reading to help but please feel free to offer up an opinion if you have one. I'm mainly trying to document the learning process to hopefully aid others in the thought process.

Comments
Todd,
I'm no OO expert but I believe you would want to create our UUID in your listener object and not on your form page or your bean object. Just my 2 cents, I haven't used MG, and have limited knowledge of m-ii.

Ryan
# Posted By Ryan Everhart | 2/24/07 9:49 PM
Not sure where MG fits in as far as validating data types (or why it would be doing that). FWIW, you should use char(35) for UUIDs. Varchar takes up more space and isn't necessary in this case since this is a fixed length string and it doesn't need the unicode support.
# Posted By Brian Rinaldi | 2/24/07 10:16 PM
Oh, and if you use char(35) my code generator should recognize that as a UUID - just an FYI.
# Posted By Brian Rinaldi | 2/24/07 10:17 PM
Todd - concerning your USERID field, I wonder why you did not decide to use the database's identity (SQL SERVER) function to have the database create the unique user ID?

Using the identity function for the user id would eliminate your add problem since the database would be responsible for creating the new unique user id.

I also wonder how the code generator will handle a table column that uses the identity function.
# Posted By Bruce | 2/25/07 7:46 AM
Personally I prefer to use UUID's myself since I can set that prior to ever calling the DB. So, I can create a user object and have a unique ID for it before ever actually persisting it to the db. Nonetheless, the code generator recognizes and handles identity fields. However, it is important to note, as Todd regularly does, that the generator is not designed to generate finished code.
# Posted By Brian Rinaldi | 2/25/07 7:58 AM

Calendar

Sun Mon Tue Wed Thu Fri Sat
  12345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex misc model-glue off topic personal project learn slidesix sql

Recent Comments

More CF+Java: Compiling Classes And Persisting Objects
Getburl said: I have been attempting to get Db4o working in my CF application and I have not succeeded. I would lo... [More]

Thoughts On Ajax Frameworks And ColdFusion/Adobe
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

Extending Ext With Ext Extensions
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

CF Needs An Open Source Contact List Importer
Kay Smoljak said: Heh, the fact that sites DO it doesn't mean they SHOULD. To us it's ok, but to a non-tech-savvy user... [More]

A Few Project Updates
Helena said: Now punctually what is the situation ? [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com