By Peter Bell

Interfaces: Doing the DAO

The core DAO interfaces are pretty similar the to CRUD service interfaces: getbyUniqueAttribute(), getbyFilter(), deletebyUniqueAttribute(), deletebyFilter() and save(). The biggest difference is that the get methods should return a recordset rather than an IBO.

Often there is the question as to where save() should be broken out into insert() and update(). Most commonly, you insert if the Object.ID is 0 and update if it is not, but sometimes it is nice for a DAO to be able to test an object for uniqueness in some other manner (email address or first name, last name and address1) and to follow certain business rules depending upon whether the uniqueness test is passed (check out Steve Bryants seriously underused DataMgr for an example of this).

All things considered, I’m going to delegate the decision of whether to insert or update down to the DAO and it can decide whether to call its private Insert() or Update() method for each record to be saved.

There would be a real argument to adding a validate method to the DAO that would specifically validate the passed data against the db introspected metadata to make sure you’re not trying to save a float in an int or 100 characters in a varchar(15). For now, I’m not going to implement that as it is doing at runtime what could quite easily be done at generation time by writing a tool to ensure that every custom data type is validated in a manner that is consistent with its underlying persistence requirements.

Comments
Isn't validation supposed to be in the BO, not a DAO? DAOs shoudl be dumb so to speak, eh?

DK
# Posted By Douglas Knudsen | 10/1/06 10:25 PM
I am putting my validation in my business objects, but you could argue that the DAO is knowledgeable about the persistence mechanism and that it is therefore responsible not for validating that the data matches your business rules, but that it will be persistable.

I'm not putting validation into my DAO, but I think there would be an argument for asking a DAO to ensure that it doesn't try to save a 20 character string into a varchar(7) field. That would be an arguable appropriate level of intelligence - at least in my opinion.
# Posted By Peter Bell | 10/1/06 10:38 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.