关于 Coldfusion 8 中的 getter、setter 和组件的问题

发布于 2024-11-14 06:35:14 字数 400 浏览 2 评论 0原文

因此,我正在尝试学习和利用组件来使我的代码变得更好...

我了解 getter 和 setter 是什么...但是,我不确定将它们放在哪里以适应我的组件的工作方式。我的组成部分是一个具有唯一 ID 和部门的教师。我希望所有信息都采用结构形式,因为每个教师都有很多信息。我的 init 方法将初始化特定实例的 id 和部门,然后继续调用一个查询,将其余信息填充到一个结构中。我只是不确定如何为 id 和部门执行 getter 和 setter...我是否只是初始化一个“空白”实例,然后使用 getter/setter 来实际执行输入?

还有关于组件的另一个想法/问题: 我的组件是否应该仅具有处理单个实体(个人)的方法,或者我的组件中是否也可以具有处理整体的方法(例如针对所有个体的搜索功能)。 ...或者我应该将两者分开吗?

谢谢!

So, I'm trying to learn and utilize components to make my code better...

I understand what getters and setters are...however, I'm not sure where to put them in respect to how my component works. My component is a faculty which has a unique id and department. I want all my information in a struct form because each faculty has a lot of information. My init method would initialize the id and the department of the specific instance and then proceed to call a query that will populate the rest of the information into a struct. I'm just not sure how to do the getters and setters for the id and department...do I just init a "blank" instance and then use a getter/ setter to actually do the input?

Also another thought/ question regarding components:
Should my component only have methods that deal with single entities (individuals) or can I also have methods in my component that deals with the whole (like a search function for all individuals).
...or should I separate the two?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2024-11-21 06:35:14

这是设置教员 CFC 的 CF8 方法。请注意,我没有使用“实例”范围,因为当升级到 CF9 时,您可以删除 getter/setter 并将 accessor=true 添加到 cfcomponent 你就完成了。但是,当您需要从 CFC 中获取数据作为 DAO 的结构来持久保存对象时,您可能会发现添加人工“实例”范围很有用。

<cfcomponent>
  <!--- does nothing in CF8 other then for documentation purposes --->
  <cfproperty name="id">
  <cfproperty name="department">

  <!--- if you want to type your param and return type for functions in CF8, use CFML --->
  <cfscript>
    function init (id, department)
    {
      setId(id);
      setDepartment(department);

      return this;
    }

    function getId() {
        return variables.id;
    }

    function setId(id) {
         variables.id = arguments.id;
    }

    // do the same for dept
    // make use of Snippet in CFEclipse / CF Builder to gen for you

  </cfscript>

</cfcomponent>

我的方法中也可以有方法吗?
处理整体的组件
(就像所有的搜索功能
个人)。 ……或者我应该分开吗
两个?

通常在 CF 世界中(受到 Java 世界的启发),我们会将它们分离到没有状态的 FooService 中,并将您的 FooService 缓存为 Application< 中的单例。 /代码>范围。然后实现与 FooDAO(数据访问对象)层中的数据库对话的创建读取更新删除(CRUD)方法。然后,您的 FooService 将调用 FooDAO 中的 CRUD 方法来为您读取(并填充)Foo 对象。

Here's a CF8 way of setting up the faculty CFC. Notice that I didn't use the 'instance' scope 'cause when it's time to upgrade to CF9, u can remove the getters/setters and add accessor=true to the cfcomponent and you're done. However, you may find adding an artificial 'instance' scope useful when you need to get the data out of the CFC as a struct for DAO to persist your object.

<cfcomponent>
  <!--- does nothing in CF8 other then for documentation purposes --->
  <cfproperty name="id">
  <cfproperty name="department">

  <!--- if you want to type your param and return type for functions in CF8, use CFML --->
  <cfscript>
    function init (id, department)
    {
      setId(id);
      setDepartment(department);

      return this;
    }

    function getId() {
        return variables.id;
    }

    function setId(id) {
         variables.id = arguments.id;
    }

    // do the same for dept
    // make use of Snippet in CFEclipse / CF Builder to gen for you

  </cfscript>

</cfcomponent>

can I also have methods in my
component that deals with the whole
(like a search function for all
individuals). ...or should I separate
the two?

Usually in the CF world (inspired by the Java world), we'd separate them into FooService with no state, and cache your FooService as singleton in Application scope. Then implement the Create Read Update Delete (CRUD) methods that talk to the DB in FooDAO (data access object) layer. Your FooService would then call the CRUD methods in FooDAO to read (and populate) a Foo object for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文