I'm trying to implement the Repository pattern using ORMLite. I initially started off with:
public List<Todo> GetByIds(long[] ids)
{
using (IDbConnection dbConn = dbFactory.OpenDbConnection())
{
return dbConn.Ids<Todo>(ids).ToList();
}
}
But having this under every method in my Repository seemed a bit repetitive? So I created a data context class which all my repositories inherit from:
public class TodoRepository : DataContext
Here is that DataContext
class:
public class DataContext
{
protected OrmLiteConnectionFactory dbFactory = new
OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["AppDb"].
ConnectionString, SqlServerOrmLiteDialectProvider.Instance);
protected IDbConnection dbConn;
public DataContext()
{
dbConn = dbFactory.OpenDbConnection();
}
}
I then simply have to do this in my methods:
public List<Todo> GetByIds(long[] ids)
{
return dbConn.Ids<Todo>(ids).ToList();
}
I was just curious if this is a good design pattern and what other's have come up with when using the Repository pattern for data access with ORMLite.
Repository<T> : IDisposable where T : new()
? As my repo's will have the same functionality (CRUD operations)? – Taffeta