Day two with FW/1

Playing around with FW/1 today I decided to take a look at the differences at how services differ from working with domain objects.  Skimming through the FW/1 documentation I did notice the framework has a built in function to populate a bean, namely the fw.populate() function.  Calling a bean (an object with getter/setter methods) is not much more involved in calling a service.  First, you create an instance of your bean.  Then call populate() method passing rc keys that you would like to set and the populate function will find the setX method in the bean and set the appropriate value.

I have an extremely simple application I have setup that has two views.  The default view is just a form that gets posted to the main.submit action which is just cfdumping the rc scope.

The sample “bean” setup for testing purposes:

 <cffunction name="init">
   variables.instance = structNew();
   instance['fw'] = arguments.fw;
 <cffunction name="setFirstname">
   instance['firstName'] = arguments.firstName;
 <cffunction name="setLastname">
   instance['lastName'] = arguments.lastName;
 <cffunction name="saveUser" returntype="struct">
   var response = structNew();
   response['message'] = "Saving user: " & instance.firstName & " " & instance.lastName;
  <cfreturn response/>

Nothing too complicated going on here, just a basic init method to setup the instance variables, two setter methods and finally a method for “saving” our user information.  The controller method for handing the submit is as follows:

<cffunction name="submit">
 <cfargument name="rc" type="struct"/>
  var userObj = createObject("component", "model.user");
  variables.fw.populate(userObj, "firstName,lastName");
  structAppend(rc, userObj.saveUser());

This creates the user object, populates the values and then saves the user and appending the results into the rc scope.  Services are a little easier to implement as they have a one line invocation.  I am still trying to figure out when it would be best to use a service to move data in and out of the application as opposed to creating a bean in the model.  I would imagine the ultimate answer would be whatever works best for you, but I can see where services could be advantageous as well.  I welcome any comments.


2 thoughts on “Day two with FW/1

  1. I’ve been playing with FW/1 for a few weeks now and even after reading the documentation, I was still confused about the services tier. I feel I have a fair grasp on OO concepts, but this is the first time the concept of a ‘bean’ has finally resonated. I’ll be playing with beans and FW/1 this week. Thanks for the great article!

    1. I am glad you found the post useful. I am still trying to figure out the best way to handle services/beans/domain objects. Although it works just fine in the controller it might be best to move the “logic” behind your application into services. On the other hand, it doesn’t look like service methods get fw API hooks. I will play around with it a little this afternoon.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s