4

It seems like the standard MVC approch (as it relates to ColdFusion) is to make the view files .cfm and do a CFINCLUDE inside of the cfc that ultimately processes the view.

Does this break the Object Orientation of cfc's?

Does this cause the CFML compiler to have to compile the view everytime?

Is there a strong reason to NOT make the view files themselves cfc's with a GetContent method?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Tom Hubbard
  • 15,820
  • 14
  • 59
  • 86

4 Answers4

6

Does this break the Object Orientation of cfc's?

Achieving this vague "Object Orientation" of cfcs is kind of subjective. Forcing yourself into "everything must be an object" is going to force you into doing things with CF that will create extra overhead. I little compromise is needed to make sure apps are speedy and efficient. Don't worry about achieving some undefinable goal of being "object oriented". Make a more definable goal, such as achieving reuse of cfcs, or encapsulation of change. Trying to make views into objects isn't necessarily help you achieve those goals because every view will be different and probably not reusable.

Does this cause the CFML compiler to have to compile the view everytime?

Cfms are compiled and cached too. I've had several large forms consisting of tabs, where each tab is a separate cfm file. On the first load, they take a couple seconds to compile and display. On subsequent loads, the tabbed view is generated and displayed instantly. The same happens with cfcs.

Is there a strong reason to NOT make the view files themselves cfc's with a GetContent method?

I while ago I tried implementing my own framework just for the learning experience and I ended up with the cfinclude approach. From what I remember, what I found was that using cfinclude encapsulated things better and avoided the cumbersomeness of creating objects, passing around arguments needed for the view, worrying about objects being in the right scope, and avoided the extra overhead of creating view objects.

In the end though, I suppose this is one of those things you have to try yourself to figure out what approach is best for your situation.

Jayson
  • 2,021
  • 3
  • 25
  • 26
4

If you're interested in implementing MVC, you should check out the wide array of CFML frameworks that already make these decisions for you.

Try ColdBox, ColdFusion on Wheels, Mach-II, or Model-Glue. Or at least take a look at their source code and see how they handle it. :)

Chris Peters
  • 17,918
  • 6
  • 49
  • 65
  • I'm looking more for why the frameworks made the decisions that they made rather than what decisions they made. – Tom Hubbard Dec 11 '09 at 14:38
  • Gotcha. I don't have any direct experience with this, but I hear that doesn't scale well for large sites. Not sure what the reasoning was, but I vaguely remember it coming from someone credible. Do with that what you will. ;) – Chris Peters Dec 12 '09 at 13:41
1

Do not blindly follow a pattern or methodology just because someone suggested it. Look at the needs that your site has and pay attention to your own preferences, with an eye toward someone else maintaining the code down the road.

Specifically, I use CFC's to encapsulate the interaction with the DB. I follow a MVC pattern because separating the view code from the model code is a very good idea, whether the CFCs are following true OO rules is less important.

I use cfincludes in many places to reduce redundency in my view layer.

I have implemented the controller portion of my pattern using a custom framework (I created the controller), then got smarter and used MachII, Fusebox and Model Glue. Each of these frameworks supports/encourages the use of CFC's views etc, or better stated that each supports good design and separation of concerns.

Find good patterns, decide what works for you, implement and document.

jamesTheProgrammer
  • 1,747
  • 4
  • 22
  • 34
  • Thanks for causing me to revisit this question! In the end we did use some includes for various view code but basically stuck to cfc. The onMissingTemplate function became the killer feature in allowing us to drive everything from cfc's. – Tom Hubbard Dec 16 '11 at 12:00
0

Wouldn't CFInclude tie down your CFC portability more by coupling it directly to a CFM.

jfrobishow
  • 2,897
  • 2
  • 27
  • 42