c# - Async-await - Am I over doing it? -


recently i've developed doubts way i'm implementing async-await pattern in web api projects. i've read async-await should "all way" , that's i've done. it's starting seem redundant , i'm not sure i'm doing correctly. i've got controller calls repository , calls data access (entity framework 6) class - "async way". i've read lot of conflicting stuff on , cleared-up.

edit: referenced possible duplicate post, not specific enough needs. included code illustrate problem. seems difficult decisive answer on this. nice if put async-await in 1 place , let .net handle rest, can't. so, on doing or not simple.

here's i've got:

controller:

public async task<ihttpactionresult> getmessages() {     var result = await _messagerepository.getmessagesasync().configureawait(false);     return ok(result); } 

repository:

public async task<list<string>> getmessagesasync()     {     return await _referralmessagedata.getmessagesasync().configureawait(false); } 

data:

public async task<list<string>> getmessagesasync() {                return await _context.messages.select(i => i.message).tolistasync().configureawait(false); } 

public async task<list<string>> getmessagesasync()     {     return await _referralmessagedata.getmessagesasync().configureawait(false); }  public async task<list<string>> getmessagesasync() {                return await _context.messages.select(i => i.message).tolistasync().configureawait(false); } 

if calls asynchronous methods tail-calls, don't need await:

public task<list<string>> getmessagesasync()     {     return _referralmessagedata.getmessagesasync(); }  public task<list<string>> getmessagesasync() {                return _context.messages.select(i => i.message).tolistasync(); } 

about thing lose stack-trace information, that's useful. remove await instead of generating state-machine handles waiting pass task produced called method calling method, , calling method can await on that.

the methods can inlined now, or perhaps have tail-call optimisation done on them.

i'd go far turn non-task-based paths task-based if relatively simple so:

public async task<list<string>> getmeesagesasync() {    if(messagecache != null)      return messagecache;    return await _referralmessagedata.getmessagesasync().configureawait(false); } 

becomes:

public task<list<string>> getmeesagesasync() {    if(messagecache != null)      return task.fromresult(messagecache);    return _referralmessagedata.getmessagesasync(); } 

however, if @ point need results of task further work, awaiting way go.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -