Dao – Data Access Objects

So last post about my blog I introduced the objects that form the foundation of my blog. But the thing that was most confusing to me was the data storage part, I was still not understanding well enough how virtual data room function. Doing a quick search on the net lead me to Sun’s J2EE DAO Solution. After reading through this, I felt empowered. All of a sudden this all made sense to me. So i have a Data Access object and it knows how to deal with the database. My entry object doesn’t have to know that is will be persisted, or that the contents of it came from a db. All it knows is what info is inside it, just as it should.

Great so now we can build, right? Well not so fast. I started building this, and all of a sudden, i had to start modifying the fusebox code that sean had done. that was never my intention. Also in building my dao class, I had implemented all my query as functions like getEntries and such. This all just didnt feel right, so I popped online and sent a note to sean. I asked him what he thought of my plan. Sean had some encouraging words but also pointed me at Martin Fowler’s Book on Patterns of Enterprise Applications. In particular he pointed me at the Service layer section.

After reading through the book, I felt like i had a new lease on life. Just like the initial excitement I felt when I found DAO, I jumped in with both feet.

Long story short, I decided that the best route would be to use the blog.cfc as an API for my model. blog.cfc would maintain all its methods, but instead of doing all the work i would implement my other objects inside it. So now my fusebox app doesnt need to change from what sean implemented, and it doesnt have to know anything about the other objects. I implement gateway object for selecting multiple rows of data, and dao for the basic CRUD (Create Read Update Delete) operations.

That sounds like fit that is perfect.

So now the real nitty gritty. When my blog.cfc get initialized it initializes a gatewayFactory and daoFactory passing in our dsn, and dbtype. These factories return database specific factories. These factories each have 3 classes. getEntryGatway, getCategoryGateway, getCommentGateway and matching methods for getting dao objects. The objects that are returned from these methods are stored as instance variables in my blog.cfc. This allows us to only have to run this code once for the life of our blog.cfc. So if we persist our blog.cfc into the application scope then this happens once for our application.

As of right now, I have all of factories working, and the gateway objects implemented. After I get writing this post I am going to work on the Dao implementation. I will let you know how that goes :)

Leave a Reply

Your email address will not be published. Required fields are marked *