Learning OO in ColdFusion Pt. 2
To continue in my series of posts about my experience of learning object oriented programming in ColdFusion. After working my way through Matt Gifford’s book, Object Oriented Programming in ColdFusion, I decided I would work with an old app I had worked with in the past. A while back I had paid for the code to Steve Moore’s Event Management System from steveeray.com. I used it as a model for a similar, more complex system I built using CFWheels. It’s a pretty simple, but nice app that has only a few database tables behind it. I figured I would use it practice my new found OO skills but this time with no framework. This is purely an exercise for me with no intention of actually deploying this app for any real use. The goal is just to see if I can get it to work exactly as I found it but using the OO techniques learned over the past few weeks. Sounds simple enough, I think.
I should begin by describing how the original app works. It is organized into events, which have sessions. Users can register for a session, by providing their basic contact info. On registration, users receive a confirmation email. There is an admin back end which handles adding events and sessions, viewing the session roster, moving people to different sessions, and sending participants emails. For ease of use, I began the way I usually do, by starting with the back end functionality.
The main admin page (I will not be dealing with authentication at this time) presents several options. I’ll work my way through each one building out the pages I need as I go. First up was Event Maintenance.
I had already built the Event Bean, DAO, and Gateway files using techniques learned from my book. The Event Bean contains its properties, the getters, and the setters. The EventDAO contains my CRUD methods to handle a single Event record. The EventGateway contains all other more complex functions that deal with multiple records. The main method at use in the gateway is the retrieveAllEvents() method. It’s worth noting that the data is not contained in the page, but rather in the Event object. I’m not performing any logic on the page, but rather within the Event object, or more precisely its methods. In my past my pages would contain logic and perform all kinds of operations on data as it got passed from page to page. In this way, I am calling the various operations that need to occur. The object already has everything it needs to do so.
The DAO and Gateway files were instantiated in my Application.cfc file to make it easy to access them throughout the application. So looking at the page, as well as the original code for the app shows that I need to retrieve a listing of events for the drop down menu. For this I used my EventGateway.retrieveAllEvents() method as follows:
The eventID value is passed on the the Event Information page where we are presented a form with the selected event’s details populated and ready for editing.
I won’t discuss the design code for the page, but the code to handle the Event does 2 things. First I use the EventDAO.retrieveEventByID(form.eventID) method to retrieve the selected event. This method returns an Event Bean that I then use to populate the form. It’s just one line of code that looks like this:
To populate the form, I just use the getters like so:
<cfinput type="Text" name="eventName" value="#objEventBean.geteventName()#" size="80" maxlength="250" required="yes" message="Enter the name of the Event" class="input">
To show the Event bean, I dumped its contents to the Event Information page.
After I make my changes to the form and submit, I update the Event bean and then pass the bean to the EventDAO.update() method. The code for this looks something like:
That’s about it. The event is updated and user is sent back to the Event Maintenance page.
The main takeaway from this so far is that there is no real logic in the page itself. The logic is all contained in the object itself. If nothing else, that should make maintenance easier. That realization was my big “Aha” moment. Next time I’ll look at the New Event page, which is actually the same as the Edit Event page–gotta keep it DRY.