什么是在.NET中管理数据库连接的最佳实践? [英] What are best practices on managing database connections in .NET?

查看:169
本文介绍了什么是在.NET中管理数据库连接的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在.NET应用程序中管理数据库连接的最佳实践 - 我知道,一般来说,传递连接对象很不方便。



但是,我有一些特定的好奇心:






强> 1。我有两个实例的业务
对象,不同的类,在
父子关系(子
是私有的)。下面哪个
是最好的? strong>




  • 保持一个私人静态连接打开和共享,由两个对象使用,并保持打开,直到父亲被处置。


  • 保持两个私有静态连接打开,每个对象一个,而不是
    关闭,直到对象被处理。


  • 不要保留静态连接;打开并随后为需要它的每个方法关闭一个新的
    连接。但是,我的大多数方法只运行1-3次查询,所以这看起来效率不高...?







2。我的第二个问题基本上是一样的,但是对于一个单一的形式。




  • 在表单的整个生命周期内打开并共享一个私有静态连接。 p>


  • 不要保持静态连接;打开并随后以需要它的形式为每个方法关闭连接(再次,每个方法只有1-3个查询)。



解决方案

(评论)...



理论是你不应该访问数据库业务逻辑 - 它应该在一个单独的数据访问类中。 (例如,以后你需要将它们以XML格式离线存储,或者使用Oracle而不是SQL Server ...你不想重写你的业务逻辑!)



您的业务对象不应具有与其关联的数据库连接。应该在一些DAL工厂类型方法中打开连接,检索/构建对象,然后关闭连接并返回对象。



业务对象本身应包含业务逻辑字段和方法,它们可能回调数据访问层,这将为每个DAL方法创建一个新的数据库连接。



您的低效率恐惧可以休息通过使用连接池,这意味着如果你打开和关闭一个连接数百次,他们将使用相同的机会。但是你不应该一直保持数据库连接 - 特别是不作为类成员。



希望有帮助!


Regarding best practice for managing database connections in a .NET application -- I know that, in general, it's bad to pass around a connection object.

However, I have some specific curiosities:


1. I have two instances of business objects, of different classes, in a parent-child relationship (the child is private.) Which of the following is best?

  • Keep one private static connection open and shared, used by both objects, and left open until the parent is disposed.

  • Keep two private static connections open, one for each object, not to be closed until the object is disposed.

  • Do not keep static connections; open and subsequently close a new connection for every method that requires it. However, most of my methods only run 1-3 queries, so this seems inefficient... ?


2. My second question is essentially the same, but for a single form. What's best here?

  • Keep one private static connection open and shared for the lifetime of the form.

  • Do not keep a static connection; open and subsequently close a connection for every method in the form that requires it (again, a mere 1-3 queries per method.)

解决方案

(Was a comment)...

The theory is you should not be accessing the database from your business logic - it should be in a separate data-access class. (Say for example in the future you need to store them offline in XML, or use Oracle rather than SQL Server... you don't want to re-write your business logic!)

Your business objects should not have database connections associated with them. The connection should be opened in some DAL factory-type method, the object retrieved/built, then the connection closed and the object returned.

The business objects themselves should contain business logic fields and methods, which might call back to the Data Access Layer, which should create a new database connection for each DAL method.

Your inefficiency fears can be put to rest by using Connection Pooling, which means that if you open and close a connection hundreds of times, chances are they will all use the same one. But you should not be keeping database connections hanging around at all - especially not as members on a class.

Hope that helps!

这篇关于什么是在.NET中管理数据库连接的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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