I using dapper in my ASP.NET WebForms solution.
All my classes which handles data retrieves an open connection from this base class
public abstract class SalesDb : IDisposable
{
protected static IDbConnection OpenConnection()
{
IDbConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesDb"].ConnectionString);
connection.Open();
return connection;
}
public void Dispose()
{
OpenConnection().Dispose();
}
}
Example of a service class that uses this base class
public class LeadService : SalesDb
{
public IEnumerable<LeadDto> Select()
{
using (IDbConnection connection = OpenConnection())
{
return connection.Query<LeadDto>("Lead_Select",
null, null, true, null, CommandType.StoredProcedure);
}
}
}
Is there any disadvantage that OpenConnection(
) method in the base class is static and return a new instance for every call?