What are the trade-offs for injecting connection strings vs. an instance of IDbConnection?
I use StructureMap to inject various services into my ASP.NET MVC application, most of which require database access for LINQ-to-SQL queries. Injecting an IDbConnection seems more testable and easier to configure for IoC than a generic connection string parameter, but I'm worried about open connections hanging around if I don't explicitly wrap the connection in a using block.
Are there any connection pooling advantages or disadvantages I should be aware of?
Injected Connection String
using (var con = new SqlConnection(InjectedConnectionString))
{
con.Execute("INSERT INTO Logs (...) VALUES (...)");
using (var db = new MyDataContext(con))
{
var records = from p in db.Products
select p;
}
}
Injected IDbConnection
con.Execute("INSERT INTO Logs (...) VALUES (...)");
using (var db = new MyDataContext(InjectedConnection))
{
var records = from p in db.Products
select p;
}
usingblock which ensures that the connection will always be closed. - Daiusing. When injecting anIDbConnection, I don't and this is part of the difference. I'll make it clearer in my question. - Petrus Theron