Creating our Domain Objects - ColdBox Series Part 6

Now that we have our database tables set up from our UML and our folder structure has been created we can begin creating our domain objects (Technically we could have done it first, it could probably be argued both ways).

Code Generator

I am going to use Brian Rinaldi's Illudium PU-36 Code Generator to help create our domain objects. Since Illudium uses the database tables to create the objects, we needed to have the database set up first. To see the MySQL DDL script check out part 2 of this series.

Now first, let's look at our UML again.

We have two domain objects, one for TodoLists and one for Todoitems. Since we created a database table to represent each one, it should be really easy to generate code for them.

Once you have downloaded the PU-36 Code Generator and placed the "cfcgenerator" folder in your webroot, you can browse to it by going to:


http://localhost:81/cfcgenerator

Note: My web server is on port 81, if your is on a different port (like the default 8500) then place that in the URL above.

The code generator will ask your for your administrator password. This is your ColdFusion Administrator password. Once you have logged in your will see a screen like this.

So let's choose the datasource for our todo list application, which we set up in Part 2 of this series.

The PU-36 Code Generator generates a lot of code for us. We are not going to use all of it. In fact, we are only going to be getting the "bean" part today. We will work on putting together our Gateway object in the next post.

First we will choose the table we want. Let's grab the "items" table. And we will choose the "default" template. This will generate what we need. We also need to enter a CFC Path. We will enter "model.todos.item", because we are going to place the item.cfc into the /model/todos directory.

Click "Generate".

You will now see seven tabs worth of code generated on the right-hand side of the screen. This is awesome. You will see that this will save you a lot of time in the future.

The first tab is the bean. That is the only one we will need today.

Now, let's go to Eclipse (or whatever editor you are using) and create a new folder inside of our /model folder, called "todos". Next, we will create a new CFC called "item.cfc". Then we will copy all of the code from the bean tab of the code generator into the newly created CFC.

Reviewing our Bean

This file is now ready to work with. The code generator has created 13 functions, 1 struct, and 4 properties for us.


    <cfproperty name="itemid" type="Numeric" default="" />

    <cfproperty name="title" type="String" default="" />

    <cfproperty name="dueDate" type="Date" default="" />

    <cfproperty name="listid_fk" type="Numeric" default="" />
The tag is for documentation and serves as metadata for Web Service and Flex connections. In our application these will not serve a functional purpose, but we will leave them anyway, because there is no reason not to.


    <cfset variables.instance = StructNew() />

This struct is used to store the private variables. This way they can all be returned (as a struct) with a call to a single method, which is getMemento().

The last method, dump(), is for troubleshooting.

The other methods (the getters/setters and the validate() method) we will look at in coming posts. Briefly, getters and setters are used for getting data from the object and for putting data into the object, and the validate method is for validating that data.

Now, let's create the "Lists" object using the same method as above. We will place the list.cfc in the /model/todos folder as well, so the CFC Path will me model.todos.list.

Modifying the List Bean

Finally, we need to add a few modifications to the list.cfc to accommodate its list items.

Since a List can contain multiple Items we need to set up a way in the list.cfc for it to store one or more items. We will do this with an Array.

First we will add it to the properties sections.


    <cfproperty name="items" type="array" default="" />

Next we will add it to the init() method.


    <cffunction name="init" access="public" returntype="model.todos.list" output="false">

        <cfargument name="listid" type="string" required="false" default="" />

        <cfargument name="name" type="string" required="false" default="" />

        <cfargument name="priority" type="string" required="false" default="" />

        

        <!--- run setters --->

        <cfset setlistid(arguments.listid) />

        <cfset setname(arguments.name) />

        <cfset setpriority(arguments.priority) />

        <cfset setItems(ArrayNew(1)) />

        <cfreturn this />

    </cffunction>

Notice that I am not adding Items as an argument for the init() method. When we create a list it will be with 0 items, so we will not be passing in an array of items. Instead I am simply adding the call to the setItems() method and creating a new array to pass.

Next, we need to create the setItems() method that we just arranged to call.


    <cffunction name="setItems" access="public" returntype="void" output="false">
        
        <cfargument name="items" type="array" required="true" />
        
        <cfset variables.instance.items = arguments.items />
        
    </cffunction>

And then we need a way to retrieve the array of items, so we'll need a getitems() method.


    <cffunction name="getItems" access="public" returntype="array" output="false">
        
        <cfreturn variables.instance.items />
        
    </cffunction>

Conclusion

That's it. Let's save our work. Next post we are going to create our gateway object and then we'll create our service object.

I have attached the files that we created today to this post. NOTE: I have not tested these files yet, so we may run into some minor bugs along the way. But hey, that's how we learn.

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1. Contact Blog Owner