I have an interface, implemented by a service, that I inject here and there in my codebase. I would like to give the opportunity to use either the normal method or its async equivalent.
public interface IServiceDoStuff
{
void DoSomeHeavyWork();
Task DoSomeHeavyWorkAsync();
}
In my implementation I just do it like this
public class Service: IServiceDoStuff
{
public void DoSomeHeavyWork(){ ... }
public Task DoSomeHeavyWorkAsync()
{
return Task.Run(() => { DoSomeHeavyWork();});
}
}
So my consumers just go
await injectedService.DoSomeHeavyWorkAsync();
My question is simply : is that a correct way to offer those two alternatives ?
Tasks
, async
and await
have been giving me headaches forever, as it has always been one of the most obscure .NET features I work with. I can never feel confident about the code I write using those features.