i Know Kung Foo Consulting

Yo, Adrian! Instead of having multiple cfc's, can one have just 1 cfc?

A reader of my OOCF Primer asks: "Instead of having multiple cfc's can one have just 1 cfc?" The answer is: it depends.

Q: Can you have 1 CFC that is both a Gateway and a DAO?

A: Yes and that would make Sean Corfield very happy.

Instead of having a ContactDAO.cfc for CRUD methods and a ContactGateway.cfc for record set methods, you can have just ContactDAO.cfc that contains them all. There are pros and cons to this and I'll go into more detail in a later post. For the sake of learning OOP with ColdFusion, I (and others) prefer to use both so that we can separate code and focus each CFC on certain tasks.

Q: Can you have 1 CFC that is both a DAO and a Bean?

A: Yes, but I don't recommend it.

I'm going to cover that in detail as well, but the premise has to do with caching objects in server, application or session memory and the problems you could have doing that with a CFC that has both Bean and DAO methods.

Q: Why so many CFCs?

Right now I know it seems daunting to have so many CFCs. For those learning OOP with ColdFusion we're going step by step through what each type of object does and why. Eventually we'll get to where we can use less objects for some tasks and more objects for others.

If you think 3 (Bean, DAO and Gateway) is a lot of CFCs now, just wait a few weeks. :)

Related Blog Entries


Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Eric Knipp's Gravatar I think it is important to note that the "bean as DAO" ie ActiveRecord pattern has been implemented in ColdFusion, as the basis of the popular Reactor framework. Reactor also includes Gateways that can return larger result sets, but the basic unit of work is the Record which packages data access and persistence methods in one place.

Whether or not you like it, is another question, but this particular animal is already available in framework format in CF.
# Posted By Eric Knipp | 3/20/08 3:23 PM
Adrian J. Moreno's Gravatar That's a great point Eric. Having not used the Reactor framework, I was unaware of its inner workings. From what I have read about it, Reactor has its pros and cons as well.

I can only address what I've seen of code using "Bean as DAO" and the performance and maintenance issues that stem from it.
# Posted By Adrian J. Moreno | 3/20/08 7:21 PM
Brian Kotek's Gravatar To be clear, Reactor also uses DAOs under the hood to handle the actual saving and loading of Records. The DAOs are Singletons, while the Records are transient. So the Record is not a DAO. The Record has a reference to a DAO, and when you call save() on a Record, it in turn calls the DAO to do the actual work. I just wanted to make this very clear, because keeping the actual database interaction separate from the bean is a critical thing to understand.
# Posted By Brian Kotek | 3/20/08 8:27 PM
Adrian J. Moreno's Gravatar Thanks for the clarification Brian.
# Posted By Adrian J. Moreno | 3/20/08 10:01 PM
Don Q's Gravatar I know its a little late, but.. could you discuss the pro's and con's to having DAO's and Gateway's merged?

And, thank you for the series.
# Posted By Don Q | 8/1/08 4:47 PM