This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions. But the code given as an exemple use a SqlConnection which is
You are using Dapper-Extensions; following code is with Dapper only. But it does not change the entire concept. Just instead of sql
you need to pass poco
.
Refer this answer for how I implemented IUnitOfWork
and DalSession
. In below code, BaseDal
is just like BaseRepository
.
public abstract class BaseDal
{
internal BaseDal(IUnitOfWork unitOfWork)
{
dapperHandler = new DapperHandler(unitOfWork);
}
DapperHandler dapperHandler = null;
protected T Get(string sql, DynamicParameters param) where T : class
{
var result = dapperHandler.Query(sql, param).FirstOrDefault();
return result;
}
protected List GetList(string sql, DynamicParameters param) where T : class
{
var result = dapperHandler.Query(sql, param).ToList();
return result;
}
protected int Insert(string sql, DynamicParameters param)
{
var result = dapperHandler.Execute(sql, param);
return result;
}
}
Edit 1 For example code with Dapper-Extensions, refer this answer that I recently posted.