質問

I've several logically-related UDFs in a single file in my application.

The question is that should the single file be a CFC file or a CFM file? and Why?

I've referred to several links as below but they explain more about how to go about implementing the solution. All I want is to know is which one is better - CFM or CFC?

How do you organize your small reusable cffunctions?

http://blog.adamcameron.me/2012/07/which-is-better-having-your-methods.html

Thanks for helping.

役に立ちましたか?

解決

"Better" is subjective. If a collection of UDFs all work with the same data that you need to pass between them, they should probably be implemented as a CFC so one can have stateful objects so the data and the methods can be encapsulated in their own memory space.

If they're purely static methods, then an included library file might be fine.

INcluded UDFs pollute the variables scope individually, whereas functions in a CFC instance are accessed via the one object variable, so is a bit tidier.

If CFML had the concept of static methods, I'd always use CFCs, but as CFML doesn't have static methods, there's scope for justifying function libraries as well as CFCs.

Personally: I'd always use CFCs. They just seem more organised and more tidy.

他のヒント

Based on my experience, I would prefer CFCs. Take into consideration that most UDFs are just utility helpers so they only need to be created one time. Placing them in a CFC means you can load them into say the application scope and persist the CFC instance. The UDFs only get created one time for your application. Also, you could have your other CFCs extend this "utility" CFC so that the UDFs are available there as well.

Now with CFMs, anytime you include that template the UDFs get created again for that request. It is additional processing for something that really does not need it. Plus what was already mentioned around UDFs polluting the variables scope is another big reason to prefer CFCs.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top