你如何组织你的小型可重用 cffunctions? [英] How do you organize your small reusable cffunctions?

查看:22
本文介绍了你如何组织你的小型可重用 cffunctions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新组织我的 ColdFusion 目录结构,并且很好奇有经验的 CF 开发人员如何组织较小的 cffunctions 库.

I am reorganizing my ColdFusion directory structures and am curious about how experienced CF developers are organizing libraries of smaller cffunctions.

我对复杂的组件(对象)并不像我对我们随着时间的推移而建立的几十个小实用函数那么好奇.

I am not as curious about elaborate components (objects) as I am about the dozens of little utility functions we all build up over time.

  • 您是否使用带有 cffunctions 和 cfinclude 的大型单个文件?
  • 您是否使用单个大文件作为 cfcomponent 并调用 creatobject/cfinvoke?
  • 您是否将每个实用程序 cffunction 放在自己的 cfc 中并调用 createobject/cfinvoke?
  • 你使用 cfimport taglib 语法吗?
  • 您使用 CustomTags 还是 cfmodule?
  • 你有更好的方法吗?

因为我不喜欢冗长的语法,所以我只是在 cf包括一个 lib.cfm,里面有一堆常见的 cffunctions.我可以将它们重构为分组 cfc,我可以在其上创建对象,以便更好地隔离变量作用域.

Since I don't like verbose syntax I have been just cfincluding a lib.cfm that has a bunch of common cffunctions in it. I may refactor them to grouped cfcs I can createobject on just to have better isolation on variable scopes.

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

这是我在 2007 年 6 月 13 日发表的博文.我已经使用这种方法有一段时间了,效果很好!天啊.

This is a reprint of a blog post I did back on June 13, 2007. I've been using this method for quite sometime and it works great! YMMV.

谁不喜欢用户定义函数 (UDF)?如果你做过任何编程,很可能你已经广泛地使用过它们.人们遇到的最大问题是如何在您的应用程序中包含和组织它们.

Who doesn't like user-defined functions (UDFs)? If you have done any programming, chances are that you have used them extensively. The biggest problem that people have with them is how to include and organize them in your application.

我发现大多数人所做的是创建一个 Utils.cfc 或 UDFs.cfc 并将他们想要使用的 UDF 剪切并粘贴到组件中,如下所示:

What I've found that most people do is create a Utils.cfc or UDFs.cfc and cut and paste their UDFs that they want to use into the component as demonstrated below:

<!--- UDFs.cfc --->
<cfcomponent output="false">

<cffunction name="init" access="public" returntype="Any" output="false">
  <cfreturn this>
</cffunction>

<cffunction name="myUDF1" access="public" returntype="Any" output="false">
</cffunction>

<cffunction name="myUDF2" access="public" returntype="Any" output="false">
</cffunction>

</cfcomponent>

一旦您将应用程序将使用的所有 UDF 粘贴到您的组件中,您就需要使这些 UDF 可用于您的应用程序.我见过的几乎每个人都将组件加载到应用程序范围内.如果您使用的是 Application.cfc,则将以下行放在 onApplicationStart() 中,或者如果您使用的是将其添加到 Application.cfm 中:

Once you have all the UDFs that your application will be using pasted into your component, you will need to make the UDFs available to your application. Almost everyone I've seen does this loading by the component into the application scope. The following line is placed into the onApplicationStart() if you're using Application.cfc or by just adding it into the Application.cfm if you're using that:

<cfset application.functions = CreateObject("component", "udfs").init()>

无论您使用的是 Application.cfc 还是 Application.cfm,结果都是一样的;您的所有 UDF 都可用于您的应用程序,您可以在整个过程中自由使用它们.唯一的区别是您使用的变量名称.我使用 application.functions,有些使用 application.utils 或 application.udfs;没关系,结果还是一样的.

Whichever one you're using, Application.cfc or Application.cfm, the results are the same; all your UDFs are available to your application and you can use them freely throughout. The only difference is what variable name you use. I use application.functions, some use application.utils or application.udfs; doesn’t matter, again, the results are the same.

不过,我使用这种方法存在一个问题,它很麻烦,而且 UDF 组件会变得很大.拥有如此庞大的组件文件的问题是编辑它变成了一场噩梦,因为滚动数千行代码并不是很有趣,而且我还注意到 CFEclipse 陷入了巨大的文件中.当然代码折叠确实提供了一些缓解,但必须有更好的方法.

There is one problem that I have with this approach though, it's cumbersome and the UDFs component will get huge. The problem with having such a huge component file is editing it becomes a nightmare since scrolling through thousand of lines of code isn't very fun and also I've noticed that CFEclipse bogs down on huge files. Sure code collapse does provide some relief but there has to be a better way.

我想要的是为我使用的每个 UDF 只拥有一个文件,以及一种让我的应用程序自动加载它们的方法.这背后的原因是,如果我需要编辑 myUDF1,我只需打开文件 myUDF1.cfm 并编辑我需要的内容.我还希望能够从 CFLib.org 获取 UDF,然后将它们放入我的应用程序中,而无需编辑任何东西.如果我需要从我的应用程序中删除 UDF,就像删除 UDF 文件并重新初始化我的应用程序一样简单.

What I wanted was to just have one file for each UDF I was using and a way for my application to load them automatically. The reason behind this was so that if I needed to edit myUDF1, I could just open the file myUDF1.cfm and edit what I needed. I also wanted to be able to grab UDFs from CFLib.org and just drop them into my application without having to edit anything. If I ever needed to remove a UDF from my application, it would be as easy as deleting the UDF file and reinitializing my application.

为了完成我想要的,我将 UDFs.cfc 修改为 11 行代码:

To accomplish what I wanted, I modified my UDFs.cfc to 11 lines of code:

<!--- UDFs.cfc --->
<cfcomponent output="false">

  <cfset variables.udfdir = GetDirectoryFromPath(GetCurrentTemplatePath()) & "udfs">
  <cfset variables.q = "">

  <cffunction name="init" access="public" returntype="Any" output="false">
    <cfreturn this>
  </cffunction>

  <cfdirectory action="list" directory="#variables.udfdir#" filter="*.cfm" name="variables.q">

  <cfoutput query="variables.q">
    <cfinclude template="udfs\#name#">
  </cfoutput>

</cfcomponent>

那到底是怎么回事?

简而言之,发生的事情是这样的:我在 UDFs.cfc 所在的目录中有一个名为 udfs 的目录.这是我放置所有 UDF CFM 文件的目录.UDFs.cfc 的作用是在调用该目录时扫描该目录,并自动包含它找到的每个 CFM 文件.因此它会自动将 UDFs 文件夹中的任何 UDF 加载到自身中(通常称为mixin").

In a nutshell, here's what’s happening: I have a directory called udfs in the same directory that I have my UDFs.cfc. This is the directory that I put all of my UDF CFM files. What the UDFs.cfc does is scan this directory when it is called and automatically includes each CFM file it finds. Thus it automatically loads any UDFs in the UDFs folder into itself (commonly called a "mixin").

我的目的达到了!我将每个 UDF 都放在自己的文件中,因此我不必滚动浏览巨大的组件文件即可找到它.我现在可以轻松打开和编辑它.通过查看目录,我就知道我的应用程序正在使用哪些 UDF.只需将浏览器中的文本保存到目录中的文件中,我就可以自动从 CFLib.org 添加 UDF.另外,如果我不再需要在我的应用程序中使用 UDF,我只需从目录中删除该文件,并在下一次重新初始化期间将其从我的应用程序中删除.所有这些都无需接触主 UDFs.cfc 文件即可完成.

So my goal is reached! I have each UDF in its own file so I don't have to scroll through a huge component file to find it. I can now open and edit it easily. By just looking at the directory, I know what UDFs my application is using. I can automatically add a UDF from CFLib.org by just saving the text from browser into a file in the directory. Plus if I no longer need to use the UDF in my application, I simply delete the file from the directory and it's removed from my application during the next re-init. All this is done without having to touch the main UDFs.cfc file.

下面是一个 UDF CFM 文件的示例.该文件名为 fullLeft.cfm,位于 UDFs 目录中.

Below is an example of what one of the UDF CFM files looks like. The file is called fullLeft.cfm and resides in the UDFs directory.

<!--- fullLeft --->
<cffunction name="fullLeft" access="public" displayname="fullLeft" returntype="string" output="false">
  <cfargument name="str" type="string" required="true">
  <cfargument name="count" type="numeric" required="true">
  <cfif not refind("[[:space:]]", arguments.str) or (arguments.count gte len(arguments.str))>
    <cfreturn Left(arguments.str, arguments.count)>
  <cfelseif reFind("[[:space:]]",mid(arguments.str,arguments.count+1,1))>
    <cfreturn left(arguments.str,arguments.count)>
  <cfelse>
    <cfif count-refind("[[:space:]]", reverse(mid(arguments.str,1,arguments.count)))>
      <cfreturn Left(arguments.str, (arguments.count-refind("[[:space:]]", reverse(mid(str,1,arguments.count)))))>
    <cfelse>
      <cfreturn left(arguments.str,1)>
    </cfif>
  </cfif>
</cffunction>

这篇关于你如何组织你的小型可重用 cffunctions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆