在 CFC 中使用数据源的最佳实践
我有一个使用上下文敏感数据源的应用程序。目前,我将数据源信息存储起来,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用CF9,您可以将Application.cfc中的this.datasource作为默认数据源。不幸的是,它似乎没有办法设置用户名/密码
A.
)使用依赖注入框架,例如ColdSpring(仅适合单例服务)、Lightwire或Coldbox自己的DI解决方案(Wirebox)。并通过
init
构造函数或设置器注入数据源/用户名/密码。B.) 在
Coldbox.xml.cfm
中设置
,请参阅:http://wiki.coldbox.org/wiki/ConfigurationFile.cfmWith 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>
inColdbox.xml.cfm
, see: http://wiki.coldbox.org/wiki/ConfigurationFile.cfm即使您的对象仅在请求级别初始化,似乎以这种方式使用应该不那么痛苦。
与以下情况相反:
如果数据对象存在于应用程序级别是安全的(此处假设对象的数据源在运行时不会更改并且您已编写线程安全的 CFC),则可以存储和初始化应用程序级别的 DAO,然后每个请求都有非常简单的代码,例如:
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.
As opposed to:
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: