1

我正在使用 CFC 组件在 ColdFusion 中编写一些代码,当时我对使用不同的方法来创建组件的对象感到有些困惑。如果有人请让我知道哪种创建对象的方法更好,我将不胜感激。 CreateObject(), EntityNew()&New关键字。我阅读了一些博客并得到了不同的答案,有些人说 Entity New 比 Create Object 更快。我还发现语法不同在EntityNew(). 如果我能从任何人那里得到一些想法,我将不胜感激。谢谢。

4

3 回答 3

5

原始版本,在此问题结束时编写:http: //blog.adamcameron.me/2014/12/fucking-stackoverflow-and-new-vs.html

成绩单:这是一个非常模糊的问题,一旦发布,我将投票关闭它。

除非有人可以使用 new, 或 提出现实世界的性能损失createObject()<cfobject>否则<cfinvoke>我不会费心去听他们的。不会有有意义的差异。他们居住在微观(和过早的〜)优化领域。

我在上面列出的所有这些选项都与entityNew()专门用于创建基于 ORM 的对象的 略有不同。其他的更通用。但是,同样,在性能方面,这里不会考虑现实世界。

当我可以避免时,我不使用标签。所以折扣<cfobject><cfinvoke>考虑IMO。

不过,所有这些都是意见。

As I said, entityNew() is specifically designed for creating ORM objects, so there might be something to be said to using that in a mixed environment that has a mix of ORM-based and vanilla objects.

As for createObject() and new? I now reserve createObject() for Java objects, and use new for CFML objects. For code clarity.

Also bear in mind that new also calls the init() method (or whatever the initmethod attribute on the component suggests should be called).

于 2014-12-16T22:56:25.720 回答
1

你的开发团队的标准是什么?

这几乎是您需要回答的唯一问题。“X 比 Y 快”可能会因平台和服务器资源而异。对别人来说更快的东西对你来说可能不会更快。如果你想使用新的语法,那就让它成为未来的标准。您不必重构 的现有实例createObject(),至少不必立即重构,但您应该标准化每个人未来将使用的内容。

于 2014-12-16T21:34:58.927 回答
0

If the object is not ORM enabled (i.e. no persistent=true), use the new operator. It's clean and readable by many.

Even if I am writing in CFML, I would much prefer <cfset foo = new X()> over <cfobject>. I would use <cfinvoke> only if I need to invoke dynamic method back then when Evaluate() is the only option, but CF10+ has invoke() and I do not use <cfinvoke> anymore, see: https://wikidocs.adobe.com/wiki/display/coldfusionen/Invoke

If the object has persistent=true, then I may opt for entityNew() because from what I've learned in the CF9 days: https://stackoverflow.com/a/1349161/35634 it should be more efficient when I need to entitySave() it later.

于 2014-12-16T23:00:24.967 回答