Amazon.com Widgets

CFObjective: Day 2 - OO Architecture Back to Front

Matt Woodward - (coldfusionweekly.com)

Providing a practical example of real world ColdFusion object-oriented application architecture.

Interesting - Most devs think that the DB is the most important part of the app.  But if we had servers with unlimited RAM and never crashed, a DB wouldn't be needed.  The Object Model is the most important part of the app.

Keep objects doing one thing only, and create them such that they don't know anything about other objects.

App Layers:

  • Biz Logic Layer - most important
    • Create an API to talk to this layer
    • Database tables and fields do not map directly to your objects
  • Service Layer
  • Controller Layer
  • Presentation
Simple Object: Person (as a bean) - dumb object with attributes and behavior - relates only to a singe record in the DB.
Getting data from a DAO - grabs single records from the DB into a bean.
  • Request for person data to controller
  • controller talks to service layer
  • which talks to DAO
Gateway objects grab multiple records - usually returns a query object - could also return a collection of beans, but not recommended.  Better just to use a query.

Service Layer provides an API to the entire biz layer.  Key in making biz logic resuable.
Controller layer - can be a framework or not.  Should be as dumb as possible.  Just talks to Service layer.
Pres layer - should only contain pres logic.  No data logic.

Sample App: Contact Manager - Very simple app with all 4 layers.  Try to obtain this sample code.

My comments: 
This app seems like a perfect example of OO over engineering.   Matt kept saying that it's important to do all this in order to allow your biz logic to be used in other apps.  But how often does that ever happen?  Seems like if your app ever changed (say you needed to add 3 more fields to your Person table), you would need to make tons of changes in every single layer across 6 templates.  You would have to edit nearly all of your methods.  His tiny app uses about 500 lines of code.  I know it's just a sample app, but you could build it in about 1 hour using two pages.  And if I ever "needed to build this app again", I could copy and paste my original code in about 20 seconds. 

I realize I'm not being fair.  Don't yell at me.  This is the technique we will need to use in building our new, top secret app at work.  Our app is going to be quite large and will rely heavily on passing objects around to various sub-applications that make up the meta app.  And we will need to have the ability to create and attach additional apps at any time in the future.  The LAST thing we want to do is fill the thing with procedural code.


 

Comments

Raymond Camden's BlogCFC version 5.8.001