通过DB连接对象的方法 [英] passing DB Connection object to methods

查看:120
本文介绍了通过DB连接对象的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在想,如果它是re​​comended是传递一个数据库连接对象(其他模块),或者让法(其他模块)需要设置它的照顾。我倾向于让该方法将其设置为不具备使用它,只是有来电传递任何需要的数据,这将需要建立的连接调用方法前检查连接的状态。

Was wondering if it is recomended to pass a database connection object around(to other modules) or let the method (in the other module) take care of setting it up. I am leaning toward letting the method set it up as to not have to check the state of the connection before using it, and just having the caller pass any needed data to the calling method that would be needed to setup the connection.

推荐答案

我个人喜欢用限定范围的连接;打开它们下旬,使用它们,并关闭他们(在使用块,全部在本地方法)。连接池将处理重新使用在大多数情况下,连接,所以有在该方法中没有真正的开销。

Personally I like to use tightly scoped connections; open them late, use them, and close them (in a "using" block, all within the local method). Connection pooling will deal with re-using the connection in most cases, so there is no real overhead in this approach.

在通过连接的主要优点的使用的是,让你可以通过周围的交易;然而,<一个href="http://stackoverflow.com/questions/224689/transactions-in-net#224702"><$c$c>TransactionScope是方法之间共享事务一个简单的方法。

The main advantage in passing connections used to be so that you could pass the transaction around; however, TransactionScope is a simpler way of sharing a transaction between methods.

由于类是具体实施,我会写每一个打开它自己的本地事务。否则,您可以使用ado.net工厂方法来创建配置文件中的相应类型(提供程序名称)。

Since the classes are implementation specific, I'd write each to open it's own native transaction. Otherwise, you can use the ado.net factory methods to create the appropriate type from the config file (the provider name).

这篇关于通过DB连接对象的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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