开发者

Java's DataSource Equivalent in .net

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 i开发者_StackOverflowt 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
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜