Archive

Archive for February, 2012

Learning OO in ColdFusion Pt. 2

February 26, 2012 1 comment

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.

Event Maintenance page

Event Maintenance page

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.

Event Information page

Event Information page

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.

Event Bean and Event Information page

Event Bean and 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:

<cfset objEventBean.setEventName(form.eventName)/>
<cfset objEventBean.setContactName(form.contactName)/>
<cfset objEventBean.setContactPhone(form.contactPhone)/>
<cfset objEventBean.setEventDescription(form.eventDescription)/>
<cfset Application.EventDAO.saveEvent(Event=objEventBean)/>

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.

Categories: ColdFusion Tags:

Learning OO in ColdFusion Pt. 1

February 25, 2012 1 comment

Object oriented coding is not completely new to me.  I’ve attempted to pick it up a few times over the years.  I do not have a pure CS background–I majored in biology and minored in chemistry.  Somehow any presentation on the subject always contained terms that I was not familiar with.  So my attempts to learn OO in the past had always stalled.  That is until the CFWheels framework came along a couple of years ago.  I love how approachable the framework was for someone with no experience in OO.  Even better, it had lots of documentation and example code to look at and use as a guide.  It became my framework of choice.  In fact, my work with the framework led me to my current job.

In the course of my daily work, I also deal with legacy code in another familiar framework, Fusebox 4.  I love Fusebox and have worked with it in my past as well.  However, I found myself thinking in CFWheels and OO terms while coding in FB.  I remembered that FB can be used in an object oriented manner and our legacy code is in need of refactoring into OO so I thought this would be the perfect time to finally learn OO in CF without a framework.  I’m not knocking the framework.  Not at all.  I love CFWheels and continue to use it.  This is just another tool in my CF toolbox.

I started with some searching on the web for resources.  I tried Nic Tunney’s CFOOP, which I’d heard about at a past CFUnited, but it appears to be defunct.  I remembered hearing about a Object Oriented Programming in ColdFusion by Matt Gifford, so I picked up a copy from Amazon.  There’s also a neat site run by Adrian Moreno called IKnowKung(Foo);. There is a great series on OO programming in CF there.

Resources in hand, I dove in, determined that this time I would get a handle on using object oriented programming in ColdFusion.  It took me about a week to work my way through the book and its exercises and perhaps another week to try out the techniques by converting a fairly simple app from procedural to OO.  I’m not done yet, but I’ve completed enough that I’m confident in my ability to finish shortly.  My next few posts will detail my efforts to use my newly acquired knowledge to code my first app.

Categories: ColdFusion Tags: