.net 中的 Java 数据源等效项
在 .net 下(实际上是 C#),是否有与 Java 的 DataSource 类等效的类?我习惯于创建单个数据源(池化或非池化)并将其传递给需要创建新数据库连接的对象。在解耦/依赖注入情况下很有帮助。
然而在.net下,实例化一个新的SqlConnection似乎来自一个池 如果您使用相同的连接字符串。这是否意味着您应该将连接字符串(或连接字符串生成器)传递给 DAO 模式类,只需传递单个 Connection 对象或创建一个新的 ConnectionProvider 类?
例如
class SomethingDao {
DataSource dataSource;
Something getSomething(int id) {
connection = dataSource.GetConnection();
connection.CreateCommand();
... etc
}
}
Under .net (specifically C# really), is there an equivalent to Java's DataSource class? I'm used to creating a single DataSource (pooled or non-pooled) and passing it around to objects that require creating new database connections. Helpful in decoupled/dependency injection situations.
However under .net, instantiating a new SqlConnection seems to come from a pool if you use the same connection string. Does that mean you should pass around a connection string (or connection string builder) to your DAO pattern classes, just pass around the single Connection object or create a new ConnectionProvider like class?
eg
class SomethingDao {
DataSource dataSource;
Something getSomething(int id) {
connection = dataSource.GetConnection();
connection.CreateCommand();
... etc
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
企业库几乎会为您处理所有这些细节,因此我建议您考虑使用它并遵循此处显示的示例代码:
http://msdn.microsoft.com/en-us/library/ff953187%28v=PandP.50%29.aspx
这个链接将引导您逐步使用它。使用 Ent Lib 的等效项是 Database 类。它包含了所有的代码示例,所以我在这里不再重复。
The Enterprise Library takes care of virtually all of these details for you, so I recommend you consider using it and following the example code shown here:
http://msdn.microsoft.com/en-us/library/ff953187%28v=PandP.50%29.aspx
This link walks you through using it step-by-step. The equivalent using Ent Lib would be the Database class. It has all the code examples, so I won't repeat them here.