This is my first asp.net core web api and i did followed the Repository pattern but i don't know whether this approach is good practice.
public class TrainerRepo : IRepoTrainer
{
private readonly IConfiguration _config;
public TrainerRepo(IConfiguration config)
{
_config = config;
}
public IDbConnection dbConnection
{
get
{
return new SqlConnection(_config.GetConnectionString("MyConnString"));
}
}
public List<Trainer> GetTrainers()
{
string sql = "select trainer_id,trainer_name from trainer";
using(IDbConnection conn = dbConnection)
{
conn.Open();
var res = conn.Query<Trainer>(sql).ToList<Trainer>();
conn.Close();
return res;
}
}
public int NewTrainer(Trainer trainer)
{
string sql = "insert into trainer ( trainer_name) values ( @name)";
using(IDbConnection conn = dbConnection)
{
conn.Open();
var res = conn.Execute(sql, param: new
{
//id = trainer.trainer_id,
name = trainer.trainer_name
});
conn.Close();
return res;
}
}
public async Task<int> updateTrainer(Trainer trainer)
{
string sql = "update trainer set trainer_name = @tname where trainer_id = @tid";
using(IDbConnection conn = dbConnection)
{
conn.Open();
var res = await conn.ExecuteAsync(sql, param: new { tname = trainer.trainer_name, tid = trainer.trainer_id });
conn.Close();
return res;
}
}
public int deleteTrainer(int id)
{
string sql = "delete from trainer where trainer_id=@trainerId";
using(IDbConnection conn = dbConnection)
{
conn.Open();
var res = conn.Execute(sql, param: new { trainerId = id });
conn.Close();
return res;
}
}
}
IRepoTrainer interface
public interface IRepoTrainer
{
List<Trainer> GetTrainers();
int NewTrainer(Trainer trainer);
Task<int> updateTrainer(Trainer trainer);
int deleteTrainer(int id);
}
my problem is that reuse of following code block, is it good practice ?
using(IDbConnection conn = dbConnection)
{
conn.Open();
//
conn.Close();
return res;
}
IConfiguration
around for injection. Try creating aIConnectionFactory
or just have a connection injected into the repo \$\endgroup\$