By Peter Bell

When a Form ISN'T a Business Object

Often in OO apps, forms relate to business objects. You might have a user registration form, an admin form for managing products and a login form for users to access the system. All of those probably relate to business objects (User, Product and User or SiteUser respectively) and you'll have some kind of system for populating and validating those business objects on form submission.

However, I often find that there are a number of forms on simple websites that really don't need to relate to a business object at all. If someone wants to set an appointment online, you may well want an Appointment object with a bunch of business logic for setting them and managing them, but if you just want a simple "contact us" form or even an appointment form that is just supposed to send an email to a person to handle, it's probably not worth having a whole OO based system for processing those forms. This doesn't mean the form processor won't go through an OO framework - just that the form processing itself won't include populating and validating a form specific business object.

That begs the question, how do you determine whether a given form should have a related business object (or objects) . . .

I have generally used the rule of thumb that if something needed to be stored to a database (as opposed to just being emailed or saved to a file), I'd treat it as a business object. I don't think it's a terribly theoretically pure approach as the traditional benefit of business objects is the encapsulation of business logic with data, and if you have a form that is persisted to a database but doesn't *have* any custom business logic, there is very little benefit in an OO approach over a simpler procedural solution.

That said, it actually wasn't a bad rule of thumb for my use case as I already have some really slick infrastructure for describing business objects and their associated validations in a simple XML file that then generates everything from the db tables to the code required to list, view and process the forms. So, adding a new business object for me doesn't require anything more than describing the properties of the object and their associated validation rules in an XML file and everything is generated to make the solution just work. The only downside is that any substantive changes to the form (changes in the fields, their data types or their validation rules) requires a change to the XML file.

I'm currently rewriting my "non business object" form processor (you know, like a cgi form script - only in CF) and I'm trying to figure out how much it should be able to do. I definitely want it to support processing, validating, sending n-emails and persisting to a file, but I'm also wondering whether it makes sense for me to add some functionality so that a form can also generate and be persisted to a db table. On the whole I think it probably isn't worth it as once you start storing stuff in a db then you want to be able to list records, delete items, run imports and exports and maybe create reports - stuff which I already have the infrastructure to handle declaratively for business objects and which I'd just have to replicate in the form processor.

What do you think? Any opinions on how much your form processor should be able to do?

Comments
Peter - Why is taking for data and sending it via an email or writing it to a file any different from storing it in a database? Really, all three options are "persistence" mechanisms, so I am not sure why you would not want the level of abstraction between the raw form data and the processes and/or persistence that is being performed. What if you needed to store the data in the database and send out an email notification. I am just not seeing the difference here and thus the need for a separate approach.
# Posted By Jeff Chastain | 11/13/08 2:51 PM
Peter,

I look at it completely opposite. Forms are views of our business objects. so I'd tend to look at the problem as with the BO need 0..n form views rather than will the form need a BO...
# Posted By Paul Marcotte | 11/14/08 1:07 PM
that should read "will the BO need 0..n form views". :)
# Posted By Paul Marcotte | 11/14/08 1:08 PM
Paul - That makes sense, but what happens when a form utilizes multiple business objects? I am working on a fairly large data entry form right now that is supported in the background by over a dozen different business objects. This continues to be a disconnect for me in terms of application generation - you almost have to define the forms (ui, controllers, etc.) separate from your object model as they are very rarely a one to one relationship.
# Posted By Jeff Chastain | 11/14/08 1:12 PM
That's a tough one Jeff. Generating complex forms from composite BOs. I don't have single form views that represent 1..n relationships. I take the easy route and use multi-step forms.
# Posted By Paul Marcotte | 11/14/08 2:05 PM
I am currently writing an article on the whole scaffolding thing but anyway when are you OOP people going to start realizing that forms and pages are objects? The whole 1....n problem is a result of not recognizing this. By making your application pages a slave to the business objects you end up in a situation where you can't increase the complexity of a single page without hacking it into the the main application/config file/xml etc.

Now as for your for processor problem I think it suffers from the same thing - it can't solve its own problems. Its like a xml file - it just sits there waiting to be processed. If it were a object in its self then you could complicate it to any extent you wish without having to decide on what features EVERY form in you application should have.

However I'm talking in general here,not about any particular language or frame work. Only observations that I've made when people use the term business object.

This is the second post I'm reading on your site, I apologise if I'm being disrespectful.
# Posted By owen | 11/21/08 4:25 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.