Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any problems with wrapping Task returning method

Are there any issues that I might encounter by wrapping a method that returns a

Task<T> where T : ClassA

with a method that returns a

Task<T> where T : IClassA

In other words wrapping a method that returns a Task of some type with another method that returns a Task of the interface of that type as below:

public new Task<ITspIdentity> FindByIdAsync(string id)
{
    return new Task<ITspIdentity>(() => base.FindByIdAsync(id).Result);
}

where base.FindByIdAsync(id) would return

Task<TspIdentity>.

Im having a go at decoupling an ASP.NET MVC applications Presentation tier from a dependency on ASP.Identity by using interfaces.

like image 557
rism Avatar asked Sep 20 '26 02:09

rism


1 Answers

As long as the calling code doesn't depend on a member that is avaliable only via ClassA and not avaliable via IClassA, there shouldn't be a problem.

You are creating and returning a Cold Task which will run an async method synchronously which is a waste of resources. You can refactor that code and simply do:

public new async Task<ITspIdentity> FindByIdAsync(string id)
{
   var tspIdentity = await base.FindByIdAsync(id).ConfigureAwait(false);
   return (ITspIdentity) tspIdentity;
}
like image 181
Yuval Itzchakov Avatar answered Sep 22 '26 17:09

Yuval Itzchakov