c# - Avoiding duplicate methods for Task and Task<T> -
i have logic task
, task<t>
. there way avoid duplicating code ?
my current code following:
public async task<socialnetworkuserinfo> getme() { return await wrapexception(() => new socialnetworkuserinfo()); } public async task authenticateasync() { await wrapexception(() => _facebook.authenticate()); } public async task<t> wrapexception<t>(func<task<t>> task) { try { return await task(); } catch (facebooknointernetexception ex) { throw new noresponseexception(ex.message, ex, true); } catch (facebookexception ex) { throw new socialnetworkexception("social network call failed", ex); } } public async task wrapexception(func<task> task) { try { await task(); } catch (facebooknointernetexception ex) { throw new noresponseexception(ex.message, ex, true); } catch (facebookexception ex) { throw new socialnetworkexception("social network call failed", ex); } }
you can make task
overload call other one, , return dummy value.
public async task wrapexception(func<task> task) { await wrapexception<object>(async () => { await task(); return null; }); }
or, since async
keyword unnecessary here:
public task wrapexception(func<task> task) { return wrapexception<object>(async () => { await task(); return null; }); }
Comments
Post a Comment