Friday, November 18, 2011

Working with Class in Coldfusion

It has been like months I've abandoned this blog, seems it dusts and rusts the every pages. *taking broom and start cleaning*

The day has come for every programmer in the world, for those who haven't, to catch up with new (not that really new, at least been a decade) and popular concept of programming : OOP.

Why do I think so ? well to me, first and all . it's COOL.

I don't say it's better, faster, reliable, or anything sounds other than COOL. read this blog before you decide to join the club http://www.codinghorror.com/blog/2007/03/your-code-oop-or-poo.html.

Regardless what they say, lets us just start having fun with Class and the OOP Concept, and how do we gonna apply that in real using ColdFusion.

I assume you've already understand about the concept of OOP and what characteristic it has. so here I will go with the basic : How to define the Class in ColdFusion.

in Coldfusion, we defined the class using a file with extension .cfc, e.g. classname.cfc. We can put the file under the webroot together with the the other coldfusion files or any specific folder as we wish as long as we setting it up in the coldfusion administrator. (see the adobe coldfusion website for detail)

Inside the file, we define the class with tag . and inside the we can defined the functions related to this class using tag. here's the sample.



....

we don't have to name the component as we do in Java. In Coldfusion, the name of class is same with the name of the file. let say if we have a coldfusion cfc with name employee.cfc, the object name when once we want to initiate will be "employee". and to invoke the function inside the class, we just simply call it like employeeclass.functionthatdosomethinghere(param1,param2,...).

alright, we have through the very basic, can wait to go for some real action ? I will explain you in more detail about making a simple coldfusion class.

But before we do that, think as if we are a programmer who been asked by our boss to work on a certain problem. the problem is that you are now work in Employee System project, where it maintains employee data.
Boss asks you to make a class to manage employee data, add new employee and edit their data. (lets start with this simple first)

How we gonna do that ?

start with a problem solving design about our plan to make it work. I usually grab a sheet of paper and put some a scratch to figure it out. I figure the employee, the functions it has, the parameters and... um.. I think the plan is seems like this image below.


can it it work like that ? ........

I'm totally kidding, a bad scratch anyway.

there're certain kind of methodology to define a relation between one class to another class, the parameters that are used to be transferred from one to another. But, I found it's too difficult.
besides, I'm a kind of person who better scratch badly instead of creating a visually beautiful template to define a relation of classes. Read some article relate to Class definition in web.
but we just gonna do it simple now.

go back to the problem, thinking OOP is a matter of understanding the meaning of object you want to create. The problem is boss asked us to make an object to maintain employee data, that able to input, and edit. I came up with idea to make a class with name employee, where there's 2 function there, get employee data and set employee data.

Class name : employee.cfc
Function getEmployee(emp_id,field_name)
Function setEmployee(emp_id,field_name,field_value)

Then, I will start to begin with the programming. first, I will create the basic structure in the file.










Well, we've got the scratch now. next we will going into the detail. but firstly, assume there is a database that keep the employee data inside. I assume you've already familiar how create database and setting the Datasource in Coldfusion. if you're not, check this web site http://www.quackit.com/coldfusion/tutorial/coldfusion_datasource.cfm.

let say we set the datasource name to "mydb" in Coldfusion administrator and we have table temployee, with following data structure.
Table Name : temployee
Fields :
emp_id varchar(20)
emp_name varchar(100)
emp_address varchar(255)
birth_date datetime
emp_email varchar(255)

Getting the employee data in the database is fairly simple using tag, please see Coldfusion documentation for detail.

so lets go back to the class problem. the scratch of employee.cfc is ready, the database is ready as well. we have also understand about what we need to do. so we can now "upgrade" each getemployee () and setemployee() function to what it should be.

BUT.... I will continue my writing later, coz I need to do something, BRB and stay tuned.

No comments: