c# - Good practices with Dapper -


i'm beginner dapper , have doubts best practices. project asp.net webapi.

opening connection string

in this thread connection database opened this,inside of controller, simple project, not meant webservice :

static idbconnection db = new sqlconnection(configurationmanager.connectionstrings["sqlserverconnstring"].connectionstring);   

but found other examples using statement :

using (idbconnection connection = new sqlconnection(stringconnection)) {     //do } 

since project webapi using statement better cu'z dispose request ?

listing data

in same thread above shows how retrieve list based on static idbconnection db property :

var res = (list<shippdetails>)db.query<shippdetails>(query, new { id }); 

or better use .aslist() ?

var res = connection.query<shippdetails>(query, new { id }).aslist(); 

the action of controller

for action goes :

[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 cause kinda of delay? or way i'm handling action "good"? thanks!

using using block best practice. may not applicable in cases though. using webapi, consider using unitofwork if transaction spread across multiple classes or methods. refer this answer code sample in case interested.

using disposes object implemented idisposable; in case, database connection. not dispose request.

about second question, aslist() should practice.

about "action of controller", not or bad. not see reason causing delay there.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -