Async expressions C# -


i'm trying generate repository methods , including async versions ones, this:

    //method 1     public static list<myentity> getmyentitybydate(mycontexttype context)     {         var tmpmov = context.myentity             .asnotracking()             .tolist();          return tmpmov;       }      //method 2 v1     public async static list<myentity> getmyentitybydateasync(mycontexttype context)     {         var tmpmov = await context.myentity             .asnotracking()             .tolistasync();          return tmpmov;      }      //method 2 v2     public async static list<myentity> getmyentitybydateasync(mycontexttype context)     {         list<myentity> tmpmov = null;           await task.factory.startnew(() => {              tmpmov = getmyentitybydate(context);         });          return tmpmov;      } 

now have questions:

what pros , cons of using method 2 v1 vs method2 v2, in terms of performance , fluency?

before downvote question, yes i'm implementing repository pattern , want write less code, thats why i'm considering v2, way have 1 query maintain.

but have poor experience on async , obiously target best possible performance.

what pros , cons of using method 2 v1 vs method2 v2, in terms of performance , fluency?

method 2 v1 proper asynchronous method. method 2 v2 fake-asynchronous method executes blocking work on taskscheduler.current (which thread pool if there no current task scheduler).

as such, v2 runs afoul of best practice not expose asynchronous wrappers synchronous methods.

before downvote question, yes i'm implementing repository pattern , want write less code, thats why i'm considering v2, way have 1 query maintain.

i'd ideal scenario expose method 2 v1, , rid of method 1 completely. querying database inherently i/o-based, api naturally asynchronous.

if really need both synchronous , asynchronous apis, recommend using "bool argument hack" described in msdn article on brownfield async development.

this like:

private async static task<list<myentity>> dogetmyentitybydateasync(mycontexttype context, bool sync) {   var query = context.myentity       .asnotracking();   return sync ?       query.tolist() :       await query.tolistasync(); }  public static task<list<myentity>> getmyentitybydateasync(mycontexttype context) {   return dogetmyentitybydateasync(context, sync: false); }  public static list<myentity> getmyentitybydate(mycontexttype context) {   return dogetmyentitybydateasync(context, sync: true).getawaiter().getresult(); } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -