使用 DAAB 处理动态连接字符串
我正在为 ASP.Net 2.0 Web 应用程序添加数据访问层,该应用程序几乎完全使用内联 SQL 调用和大量使用复制/粘贴来编写。 由于业务问题,我一次只能重构应用程序的一小部分(无法适应并进行大规模的重新设计),因此我必须接受某些设计决策,直到事情足够松散耦合做出重大改变并正确测试它们。
我决定使用企业库数据访问应用程序块作为未来数据访问层的管道,而之前的一个此类设计决策给我带来了问题。 目前,我们根据用户提供的帐户 ID 从管理数据库获取应用程序的“主”连接字符串,以便我们可以允许应用程序的单个安装访问多个数据库。 我的问题涉及尝试找出将连接字符串(或帐户 ID)获取到 DAL 的最佳实践方法。
以前的实现是将加密的连接字符串存储在 cookie 中,因此我当前的黑客方法是从那里获取连接字符串,然后按以下方式使用 DAAB:
Public Shared Function GetResultsByKeywords(ByVal key1 As String, ByVal key2 As String, ByVal key3 As String, ByVal key4 As String) As DataTable
Dim db As SqlDatabase = New SqlDatabase(Connection.GetConnectString())
Dim dt As DataTable = New DataTable
Using dr As IDataReader = db.ExecuteReader("sel_eHELP_ITEMS", key1, key2, key3, key4)
dt.Load(dr)
End Using
Return dt
End Function
其中 Connection.GetConnectString() 正在从 cookie 中获取连接字符串。
我知道这远不是最好的方法,我需要解决它,但我正在努力寻找“正确”的方法来完成它。 也许创建一个基类,我可以使用连接字符串进行初始化并从中继承所有其他类,但我不确定这会是什么样子以及我将首先在哪里初始化该类的实例。 在访问任何 DAL 功能之前在每个页面上?
任何指导将不胜感激。 谢谢。
更新
让我澄清一下。 一次安装最多有 3 或 4 个不同的连接字符串。 “帐户 ID”不是用户 ID,由多个用户使用来本质上指定要连接到哪个数据库。
I'm in the process of adding a Data Access Layer for our ASP.Net 2.0 web application that was written almost exclusively using in-line SQL calls and copious use of copy/paste. Due to business concerns I am only able to re-factor small portions of the application at a time (can't settle in and do a huge re-design), so I have to live with certain design decisions until things are loosely coupled enough to make big changes and test them properly.
I've decided to use the Enterprise Library Data Access Application Block as the plumbing for the Data Access Layer going forward, and one such previous design decision is causing me problems. Currently we get the "main" connection string for our application from an admin database based on the account id provided by the user so that we can allow a single installation of the application to access multiple databases. My problem deals with trying to figure out the best practices way of getting that connection string (or the account id) to the DAL.
The previous implementation was storing the encrypted connection string in a cookie, so my current hack-approach is to get the connection string from there and then use DAAB in the following manner:
Public Shared Function GetResultsByKeywords(ByVal key1 As String, ByVal key2 As String, ByVal key3 As String, ByVal key4 As String) As DataTable
Dim db As SqlDatabase = New SqlDatabase(Connection.GetConnectString())
Dim dt As DataTable = New DataTable
Using dr As IDataReader = db.ExecuteReader("sel_eHELP_ITEMS", key1, key2, key3, key4)
dt.Load(dr)
End Using
Return dt
End Function
Where Connection.GetConnectString() is fetching the connection string from the cookie.
I know this is nowhere near the best way to do this and I need to fix it, but I'm struggling with the "right" way to get it done. Maybe create a base class that I can initialize with the connection string and inherit all other classes from, but I'm not sure how this would look and where I would first initialize an instance of that class. On every page before accessing any DAL functions?
Any guidance would be appreciated. Thank you.
Update
Let me clarify. At most there will be 3 or 4 different connection strings for an installation. The "account id" is not a user id and is used by multiple users to essentially specify which database to connect to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
根据您的最新更新,我仍然建议使用 DAAB 查看配置
http://davidhayden.com/blog/dave/archive/ 2006/01/23/2744.aspx
我很难找到/打开 Ent Lib 文档,但我会看看它是否具有与 SqlConnection 对象的 ChangeDatabase 方法等效的方法。 http://msdn.microsoft.com/ en-us/library/system.data.sqlclient.sqlconnection.changedatabase.aspx
这是一种方法。由于我认为您的 DAL 不应该了解您的切换数据库要求,所以我将使用以下命令设置您的 DAL ConnectionString 作为构造函数的参数。 使用包装器方法/属性来隐藏/封装该需求并获取您想要在应用程序中使用的 DAL 实例。
(请原谅 c#)
希望有帮助,祝你好运! 并且要非常小心放入 cookie 中的内容。 ;)
Edit:
With your latest update, I still recommend looking into the Configuration using the DAAB
http://davidhayden.com/blog/dave/archive/2006/01/23/2744.aspx
I'm having difficulty finding/opening the Ent Lib docs, but I would see if it has the equivalent of ChangeDatabase method of the SqlConnection object. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.changedatabase.aspx
Here's one way to do it .. Since I don't think your DAL should know about your switching DB requirement I would setup your DAL with a ConnectionString as a parameter of the constructor. Use a wrapper method/property to hide/encapsulate that requirement and get the instance of the DAL you would like to use in your app.
(please excuse the c#)
Hope that helps, good luck! And be very careful what you put in cookies. ;)