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

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

问题描述

我重组了我的ColdFusion目录结构,我很好奇经验丰富的CF开发者如何组织更小的阻碍的库。

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

我不喜欢复杂的组件)


  • 你使用一个大的单个文件与cffunctions和cfinclude it?

  • 您是否使用大型单个文件作为cfcomponent并调用creatobject / cfinvoke?

  • 您将每个实用程序都放在自己的cfc中,

  • 您是否使用CustomTags或cfmodule?

  • 您有更好的方法吗?

  • Do you use a large single file with cffunctions and cfinclude it?
  • Do you use a large single file as a cfcomponent and call creatobject/cfinvoke?
  • Do you put each utility cffunction in its own cfc and call createobject/cfinvoke?
  • Do you use the cfimport taglib syntax?
  • Do you use the CustomTags or cfmodule?
  • Do you have a better way?

由于我不喜欢冗长的语法我一直只是cfincluding lib.cfm它有一堆共同的阻碍。我可以重构他们分组cfcs我可以创建对象只是为了更好地隔离在变量范围。

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.

有更好的方法吗?

推荐答案

这是一个重印的博客文章我做了回到2007年6月13日。我已经使用这种方法相当多的时间,它的工作伟大! YMMV。

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可用于你的应用程序几乎所有我看到的组件都加载到应用程序范围。下面这行被放置到 onApplicationStart()如果你使用Application.cfc或者通过只是添加到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()>

're using,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 的目录,我有我的UDFs.cfc。这是我放所有UDF CFM文件的目录。 UDFs.cfc执行的操作是在调用此目录时扫描此目录,并自动包含其找到的每个CFM文件。因此,它会自动将UDF文件夹中的任何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 ,位于UDF目录中。

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>

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

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