如何确定ColdFusion对象所在的上下文?

Jak*_*sel 11 java coldfusion cfc cfml

所以,鉴于我有一个这个组件的实例:

foo.cfc

<cfcomponent>
  <cffunction name="locateMe">
    <cfreturn "I don't know where I live!">
  </cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

而且,这个其他组件,fooParent.cfc:

<cfcomponent>
  <cfset this.foo = createObject("component", "foo")>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

假设我用几种不同的方式创建"foo"的实例:

<cfset myStruct = {}>
<cfset myStruct.foo = createObject("component", "foo")>

<cfset myFoo = createObject("component", "foo")>

<cfset myFooParent = createObject("component", "fooParent")>

<cfoutput>
#myStruct.foo.locateMe()#<br>
#myFoo.locateMe()#<br>
#myFooParent.foo.locateMe()#<br>
</cfoutput>
Run Code Online (Sandbox Code Playgroud)

正如所料,这输出:

I don't know where I live!
I don't know where I live!
I don't know where I live!
Run Code Online (Sandbox Code Playgroud)

我想知道的是,我可以在foo.cfc中做些什么来告诉我关于它被调用的上下文的内容(任何事情!)?因为一切最终都存在于(至少)某种范围内,并且所有范围都是一种对象,我所说的是我真的想从某个给定的实例化对象中确定包含对象的某种方式.最后,从上面的示例代码片段构建foo.cfc的某种方式,以便这样的东西可以作为我的输出:

I live within a "class coldfusion.runtime.Struct" instance!
I live within a "class coldfusion.runtime.VariableScope" instance!
I live within a "component cfjunk.fooParent" instance!
Run Code Online (Sandbox Code Playgroud)

其中每个值都可以通过检查传递getMetaData实际包含对象引用的结果来确定.

更新正如Micah在评论中所建议的,我已经添加了"Java"标签,因为我怀疑他可能是正确的,因为解决方案可能在于使用Java进行内省.

更新

让我解释为什么我需要这个,而不是将其视为纯粹学术讨论.

我正在使用带有包含的CFWheels ORM来获取对我的数据的引用,如下所示:

var user = model("User").findOne(where="id=123", include="AuthSource", returnAs="object");
Run Code Online (Sandbox Code Playgroud)

这将返回给我一个我可以这样引用的对象:

user.id // property of the "User" model
user.reset() // method on the "User" model
user.AuthSource.id // property of the "AuthSource" model
user.AuthSource.authenticate(password) // method on the "AuthSource" model
Run Code Online (Sandbox Code Playgroud)

现在,在我的"AuthSource.authenticate"方法中,我想知道我所包含的"用户"对象.否则,我最终将不得不像这样调用函数,而是:

user.AuthSource.authenticate(user, password) // note the redundancy in the use of "user"
Run Code Online (Sandbox Code Playgroud)

我应该能够依赖这样一个事实,即我通过User对象调用AuthSource模型上的方法,并实际从该方法中读取该对象.

fre*_*gas 4

自从我完成冷融合以来已经很长一段时间了,所以请原谅我的伪代码,但我认为在这种情况下通常会做的是让父级在实例化子级时将其自身的副本发送给子级。这在许多 OOP 设计模式中都有使用,其中两个对象需要以双向方式相互通信,而不仅仅是父对象调用子对象的方法。

所以你的子类将被定义为这样的:

<cfcomponent>
  <cffunction name="init">
     <cfargument name="parentParam" required="yes" type="object">
     <cfset this.parent = parentParam >
      <cfreturn this> 
   </cffuncton>
  <cffunction name="locateMe">
    <cfreturn "I belong to #this.parent.className# !">
  </cffunction>
 <cffunction name="doOtherStuff">
    <cfreturn "I do stuff with my parent: #this.parent.className# !">
  </cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)

然后当你使用它时...

<cfset myParent.child = createObject("component", "Object").init(myParent) />
#myparent.locateMe()#
#myparent.doOtherStuff()#
Run Code Online (Sandbox Code Playgroud)

ParentParam 将是名为“init”的构造函数方法中的必需参数,因此子级始终具有对其父级的引用。然后你的所有方法都可以利用 this.parent 来处理它。在我的代码示例中,我执行#this.parent.className#,但不知道 Coldfusion 对象具有这样的属性。也许您可以使用反射或某种元编程来完成同样的事情。

请注意:据我所知,coldfusion 不支持内置构造函数,因此我向您展示的是此站点的社区标准最佳实践:

http://www.iknowkungfoo.com/blog/index.cfm/2007/8/22/Object-Oriented-Coldfusion--1--Intro-to-Objectcfc

顺便说一句,我很抱歉你正在混淆......;)