This morning, I created the BaseModel.cfc for OOPhoto, my latest attempt at learning object oriented programming in ColdFusion. Then, just now at lunch, I created my three primary domain objects - the PhotoGallery.cfc, the Photo.cfc, and the Comment.cfc. That seemed pretty easy. But then, I went to start integrating my domain model into my service objects and I got very stuck. I have a lot of repeated queries in my service objects for the various methods; but, I shouldn't be repeating all my object "load" style functionality in each of those methods, should I?
At first, I started in the PhotoService.cfc and created a GetPhotoObjects() method. The idea here was that this method would take a query of photo data (already being done by each service method) and return an array of Photo.cfc objects. I thought that this would be a good way to keep things DRY. But then I remembered that each Photo object has a reference to its PhotoGallery object. A simple concept, but what happens if the record set I passed to GetPhotoObjects() have a common gallery (such as the query created in GetPhotosForGallery())? In that case, I don't want to create a new PhotoGallery object for each record! I would rather create one and then pass that reference into each Photo instance.
To make matters even more complicated, some photo data queries have a mixture of galleries. The GetRecentlyUploadedPhotos() method returns a whole mess of photos regardless of their photo gallery. In that case, I might have to create a new PhotoGallery.cfc instance for each photo data record.
This is really frustrating. Every step seems a bit harder than the previous one.
Maybe not all the service methods should return an array of objects. Afterall, let's not forget how awesome the ColdFusion query object is. But, if we go down that road, how do we decide which service methods should return queries and which should return arrays of objects? A tough question and one reason why I think that I should totally abandon the idea of returning queries while learning object oriented programming in ColdFusion. On one hand, they are lightweight and very powerful; but, on the other hand, I think they complicate my understanding of where to do what, when.
I think what I need to do is completely strip out the implementation of my Service objects and rebuild them. Right now, I am getting confused because there are a lot of queries floating around. If I rewrite the service objects while thinking about objects rather than queiries, perhaps it will make more sense.