在 CFC 中使用数据源的最佳实践

发布于 2024-10-01 12:05:54 字数 391 浏览 4 评论 0原文

我有一个使用上下文敏感数据源的应用程序。目前,我将数据源信息存储起来,

reqeust.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername"
request.DB.Password = "DatasourcePassword"

然后根据上下文覆盖变量,因此每个 cfquery 标签都有属性 datasource="#request.DB.Datesource#" ...等等...

我想开始移动到更多以 CFC 为中心的框架,如 Coldbox,但我只是不知道这是如何工作的。

我是否需要将数据源对象传递到 CFC 的 init 语句中?这看起来像是一个超级皮塔饼。

I have an application which uses context sensitive datasources. Currently I keep the datasource information stored a such

reqeust.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername"
request.DB.Password = "DatasourcePassword"

I then overwrite the variables depending on the context, so each cfquery tag has the attributes datasource="#request.DB.Datesource#" ... etc ...

I want to start moving to more CFC centric frameworks like Coldbox, but I just don't see how this would work.

Do I need to pass in a datasource object into the init statement of the CFC? This seems like it would be a super PITA.

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

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

发布评论

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

评论(2

几味少女 2024-10-08 12:05:54

使用CF9,您可以将Application.cfc中的this.datasource作为默认数据源。不幸的是,它似乎没有办法设置用户名/密码

A.

)使用依赖注入框架,例如ColdSpring(仅适合单例服务)、Lightwire或Coldbox自己的DI解决方案(Wirebox)。并通过 init 构造函数或设置器注入数据源/用户名/密码。

B.) 在 Coldbox.xml.cfm 中设置 ,请参阅:http://wiki.coldbox.org/wiki/ConfigurationFile.cfm

<!--Datasource Setup, you can then retreive a datasourceBean
   via the getDatasource("name") method: -->
<Datasources>
  <Datasource alias="MyDSNAlias" 
                  name="real_dsn_name" 
                  dbtype="mysql" 
                  username=""
                  password="" />
</Datasources>

With CF9, you can this.datasource in Application.cfc as the default datasource. Unfortunately, it doesn't seem to have a way to set username/password

Either

A.) use an Dependency Injection framework such as ColdSpring (only suitable for singleton Services), Lightwire or Coldbox's own DI solution (Wirebox). and inject the datasource/username/password through the init constructor or setters.

B.) set <Datasources> in Coldbox.xml.cfm, see: http://wiki.coldbox.org/wiki/ConfigurationFile.cfm

<!--Datasource Setup, you can then retreive a datasourceBean
   via the getDatasource("name") method: -->
<Datasources>
  <Datasource alias="MyDSNAlias" 
                  name="real_dsn_name" 
                  dbtype="mysql" 
                  username=""
                  password="" />
</Datasources>
最美的太阳 2024-10-08 12:05:54

即使您的对象仅在请求级别初始化,似乎以这种方式使用应该不那么痛苦。

<cfscript>
request.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername";
request.DB.Password = "DatasourcePassword";

request.randomDAO = createObject('component','DAOStuff.randomDAO');
request.randomDAO.init(DBObject = request.DB);

request.someQuery = request.randomDAO.someGetter();
request.someOtherQuery = request.randomDAO.someOtherGetter();
request.aThirdQuery = request.randomDAO.aThirdGetter();
</cfscript>

与以下情况相反:

<cfscript>
request.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername";
request.DB.Password = "DatasourcePassword";
</cfscript>

<cfquery name="request.someQuery" 
    datasource=request.DB.Datasource 
    username=request.DB.Username 
    password=request.DB.Password>
    --SOME SQL HERE
</cfquery>

<cfquery name="request.someOtherQuery" 
    datasource=request.DB.Datasource 
    username=request.DB.Username 
    password=request.DB.Password>
    --SOME SQL HERE
</cfquery>

<cfquery name="request.aThirdQuery" 
    datasource=request.DB.Datasource 
    username=request.DB.Username 
    password=request.DB.Password>
    --SOME SQL HERE
</cfquery>

如果数据对象存在于应用程序级别是安全的(此处假设对象的数据源在运行时不会更改并且您已编写线程安全的 CFC),则可以存储和初始化应用程序级别的 DAO,然后每个请求都有非常简单的代码,例如:

<cfscript>
request.someQuery = application.randomDAO.someGetter();
request.someOtherQuery = application.randomDAO.someOtherGetter();
request.aThirdQuery = application.randomDAO.aThirdGetter();
</cfscript>

Even if your objects only get initialized at request level, it seems like it should be less of a pain to work with in this fashion.

<cfscript>
request.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername";
request.DB.Password = "DatasourcePassword";

request.randomDAO = createObject('component','DAOStuff.randomDAO');
request.randomDAO.init(DBObject = request.DB);

request.someQuery = request.randomDAO.someGetter();
request.someOtherQuery = request.randomDAO.someOtherGetter();
request.aThirdQuery = request.randomDAO.aThirdGetter();
</cfscript>

As opposed to:

<cfscript>
request.DB.Datasource = "DatasourceName";
request.DB.Username = "DatasourceUsername";
request.DB.Password = "DatasourcePassword";
</cfscript>

<cfquery name="request.someQuery" 
    datasource=request.DB.Datasource 
    username=request.DB.Username 
    password=request.DB.Password>
    --SOME SQL HERE
</cfquery>

<cfquery name="request.someOtherQuery" 
    datasource=request.DB.Datasource 
    username=request.DB.Username 
    password=request.DB.Password>
    --SOME SQL HERE
</cfquery>

<cfquery name="request.aThirdQuery" 
    datasource=request.DB.Datasource 
    username=request.DB.Username 
    password=request.DB.Password>
    --SOME SQL HERE
</cfquery>

If it is safe for your data objects to exist at an application level (assuming here that the data source for the object will not change at run-time and that you have written thread-safe CFCs) You can store and initialize DAOs at application level and then each request has wonderfully simple code like:

<cfscript>
request.someQuery = application.randomDAO.someGetter();
request.someOtherQuery = application.randomDAO.someOtherGetter();
request.aThirdQuery = application.randomDAO.aThirdGetter();
</cfscript>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文