将 CFC 文件拆分为多个文件
我被要求更新一个旧项目。当我进入 cfc 文件时,它有超过 3000 行代码和超过 100 个 cffunction。我想知道是否可以将 cfc 拆分为多个文件,这些文件的 cffunctions 逻辑分组,而无需更改任何其他页面中的代码。
I have been asked to update an old project. When i went into the cfc file it had over 3000 lines of code and over 100 cffunctions. I was wondering if i could split the cfc into multiple files whose cffunctions are logically grouped without having to change the code in any other pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
遇到类似的问题。我创建了新的 cfc,并修改了原始函数以调用新 cfc 中的函数。
例如
Run into a similar problem. I created the new cfcs and modified the original functions to call functions within the new cfcs.
For e.g
重构,重构,重构...
最简单的方法可能是使用 cfinclude 来注入函数(mixin 的)
Refactor, Refactor, refactor...
simplest way might be using
cfinclude
to inject functions (mixin's)问题意味着有足够的客户端代码使用该对象,如果该对象被分解,则在其他地方更改调用会很麻烦。在这种情况下,将现有对象视为 Facade - 这是一个为某个对象提供统一接口的对象。底层的类层次结构。
产生层次结构的方法是确定那些应该放在一起的功能。每当我遇到这个问题时,函数通常不共享任何状态,而是像
静态
java 方法一样,但如果存在共享状态的函数,那么它们是该分组的良好候选者。否则,通常是共享相同输入参数或名称中具有相同措辞的函数(即saveMyData
、loadMyData
等)。在该示例中,将这些函数复制到新的 CFC 中(例如
MyData
) - 此时您可以更改函数名称以消除重复或提高其清晰度(例如>MyData.load()
)。返回原始对象(即BigCFC
),删除这些函数的实现,并将调用委托给新创建的 CFC(您可以考虑使新 CFC 成为旧 CFC 的一部分)。因此,它看起来像这样:其中
variables.myData
将作为 CFC 初始化的一部分进行设置。采用这种方法意味着您现有的客户端代码不会受到更改的影响,但仍将所有内容分解为逻辑分组,并定位新代码以使用更细粒度的 CFC。
Question implies there's enough client code using this object that changing the calls elsewhere if the object's broken apart apart is burdensome. In this case treat the existing object as a Facade - that is an object that provides unified interface to an underlying class hierarchy.
The way to approach producing the hierarchy is identifying those functions that should go together. Whenever I come across this problem the functions usually do not share any state, rather they are like
static
java methods, but if there are functions that share state they are a good candidate for this grouping. Otherwise it's usually functions that share the same input parameters or tend to have the same verbiage in their name (i.e.saveMyData
,loadMyData
, etc...).Given that example, copy these functions into a new CFC (e.g.
MyData
) - at this point you may change the function names to eliminate repetition or improve their clarity (e.g.MyData.load()
). Back in the original object (i.e.BigCFC
) remove these functions' implementation and instead delegate the call to the newly created CFC (you may consider making the new CFC part of the old's composition). So it would look something like this:Where
variables.myData
would be setup as part of the CFC's initialization.Taking this approach means your existing client code is unaffected by the change, but still breaks apart everything into logic groupings, and positions new code to use the more granular CFCs.
这是一个老问题,我只是随机遇到它,但我想我应该在这里插话,因为这是我必须多次处理的事情。
如果目标只是从代码管理的角度更好地组织事物(而不是说专门减少每个 CFC 中的方法数量),那么我建议将 CFC 分解为多个 CFM 页面并包括 它们在 CFC 中。从代码管理的角度来看,您可以将多个函数分组到一个命名良好的 CFM 文件中,这一切都变得更容易处理。调用代码保持不变,因为所有函数仍像以前一样在 CFC 中实例化。
我在 init 方法中使用了一些代码,这些代码自动包含在同一文件夹中找到的所有 CFM 文件,并且我在每个文件夹中放置一个 base.cfc 以及分组的函数。
例如
It's an old question and I just came across it randomly, but I thought I'd chime in here as it's something I've had to deal with on many occasions.
If the goal is simply to organise things better from a code management perspective (rather than say, to specifcially reduce the amount of methods in each CFC) then I'd advocate breaking the CFC down into multiple CFM pages and including them in the CFC. From a code management perspective, you can group several functions into a well named CFM file and it all becomes a lot easier to deal with. The calling code remains the same, as all the functions are still being instantiated in the CFC as before.
I have a bit of code I use in my init methods that automatically includes all the CFM files it finds in the same folder, and I house a single base.cfc in each folder along with the grouped functions.
e.g.