Starting with the following (simplified) interface which has async/await in mind, I want to implement it by using LiteDB database.
public interface IDataService
{
Task<User> GetUserAsync(int key);
}
Unfortunately, LiteDB currently does not support async methods. All methods are synchronous.
I tried the following, but later ran into some issues with that (process did not really wait for whatever reason). Following that, I read that you should only use Task.Run()
for CPU bound algorithms, and therefore not for database access.
public Task<User> GetUserAsync(int key)
{
var task = Task.Run(() =>
{
return _users
.Find(x => x.Key == key)
.SingleOrDefault();
});
return task;
}
Even though I read a couple of blog articles and also SO questions, it is still unclear for me how to make a properly written awaitable method for synchronous IO based code.
So how can I write a properly awaitable method?
Note: I can't change the interface. This is given.