I have an interface
public interface IUserFactory
{
User GetUser(string id);
void DeleteUser(string id);
}
Current class which implements uses functions which are synchronous.
I am now going to have an implementation which uses HttpClient, whose calls are async.
What I’m doing now is to have the new UserFactoryWeb class implement two sets of functions:
Task<User> GetUserAsync(string id)
Task DeleteUserAsync(string id)
and
User GetUser(string id)
void DeleteUser(string id)
the interface methods. In the interface methods I use the trick:
var t = Task.Run(async code...)
t.Wait();
in order to prevent deadlocks.
Are there other options?
Also, how can you support async calls in interfaces? It seems crazy that async isnt part of an interface signature.
question from:
https://stackoverflow.com/questions/65947264/interfaces-and-async-practices 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…