I'm a Beginner with Dapper and I have some doubts about best practices. My project is a Asp.net WebApi.
Opening Connection String
In this thread the connection with the database is opened like this,inside of Controller, but it is a simple project, not meant to be a WebService :
static IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString);
But I found other examples with using
statement :
using (IDbConnection connection = new SqlConnection(stringConnection))
{
//do something
}
Since this project is a WebApi the using statement would be better cu'z it would Dispose
the request ?
Listing Data
In the same thread above shows how to retrieve a list based on static IDbConnection db
property :
var res = (List<ShippDetails>)db.Query<ShippDetails>(query, new { id });
Or would be better to use .AsList()
?
var res = connection.Query<ShippDetails>(query, new { id }).AsList();
The Action of the Controller
For all my Action it goes like :
[Route("FF")]
[HttpGet]
public async Task<HttpResponseMessage> get()
{
var response = new HttpResponseMessage();
int id = 1;
var res = (List<ShippDetails>)db.Query<ShippDetails>(query, new { id });
if (res.Count > 0)
{
response = Request.CreateResponse(HttpStatusCode.OK, res);
}
else
{
response = Request.CreateResponse(HttpStatusCode.NoContent);
}
var task = new TaskCompletionSource<HttpResponseMessage>();
task.SetResult(response);
return await task.Task;
}
It could cause some kinda of Delay? Or the way I'm handling my Action is "Good"? Thanks!