MongoDB 存储库的异步等待用法
Async await usage for MongoDB repository
我有一个 MongoDB 存储库 class,如下所示:
public class MongoDbRepository<TEntity> : IRepository<TEntity> where TEntity : EntityBase
{
private IMongoClient client;
private IMongoDatabase database;
private IMongoCollection<TEntity> collection;
public MongoDbRepository()
{
client = new MongoClient();
database = client.GetDatabase("Test");
collection = database.GetCollection<TEntity>(typeof(TEntity).Name);
}
public async Task Insert(TEntity entity)
{
if (entity.Id == null)
{
entity.Id = Guid.NewGuid();
}
await collection.InsertOneAsync(entity);
}
public async Task Update(TEntity entity)
{
await collection.FindOneAndReplaceAsync(x => x.Id == entity.Id, entity, null);
}
public async Task Delete(TEntity entity)
{
await collection.DeleteOneAsync(x => x.Id == entity.Id);
}
public IList<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return collection.Find(predicate, null).ToListAsync<TEntity>().Result;
}
public IList<TEntity> GetAll()
{
return collection.Find(new BsonDocument(), null).ToListAsync<TEntity>().Result;
}
public TEntity GetById(Guid id)
{
return collection.Find(x => x.Id == id, null).ToListAsync<TEntity>().Result.FirstOrDefault();
}
}
我在下面的代码片段中使用这个 class:
public void Add()
{
if (!string.IsNullOrEmpty(word))
{
BlackListItem blackListItem =
new BlackListItem()
{
Word = word,
CreatedAt = DateTime.Now
};
var blackListItemRepository = new MongoDbRepository<BlackListItem>();
blackListItemRepository.Insert(blackListItem);
Word = string.Empty;
GetBlackListItems();
}
}
它工作正常,但我收到以下关于 blackListItemRepository.Insert(blackListItem);
行的警告
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
我是 await 和 async 关键字的新手,我不确定我是否正确使用了它们。您对我的代码库 class 和我的用法有什么建议吗?
提前致谢,
当你调用异步方法时,你应该等待返回的任务,你只能在异步方法等中这样做。等待任务确保你只在操作完成后继续执行,否则操作和代码之后它会 运行 同时。
因此您的代码可能如下所示:
public async Task AddAsync()
{
if (!string.IsNullOrEmpty(word))
{
BlackListItem blackListItem =
new BlackListItem()
{
Word = word,
CreatedAt = DateTime.Now
};
var blackListItemRepository = new MongoDbRepository<BlackListItem>();
await blackListItemRepository.InsertAsync(blackListItem);
Word = string.Empty;
GetBlackListItems();
}
}
此外,异步方法有一个命名约定,即在名称中添加 "Async" 后缀。所以 AddAsync
而不是 Add
,InsertAsync
而不是 Insert
等等。
我有一个 MongoDB 存储库 class,如下所示:
public class MongoDbRepository<TEntity> : IRepository<TEntity> where TEntity : EntityBase
{
private IMongoClient client;
private IMongoDatabase database;
private IMongoCollection<TEntity> collection;
public MongoDbRepository()
{
client = new MongoClient();
database = client.GetDatabase("Test");
collection = database.GetCollection<TEntity>(typeof(TEntity).Name);
}
public async Task Insert(TEntity entity)
{
if (entity.Id == null)
{
entity.Id = Guid.NewGuid();
}
await collection.InsertOneAsync(entity);
}
public async Task Update(TEntity entity)
{
await collection.FindOneAndReplaceAsync(x => x.Id == entity.Id, entity, null);
}
public async Task Delete(TEntity entity)
{
await collection.DeleteOneAsync(x => x.Id == entity.Id);
}
public IList<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate)
{
return collection.Find(predicate, null).ToListAsync<TEntity>().Result;
}
public IList<TEntity> GetAll()
{
return collection.Find(new BsonDocument(), null).ToListAsync<TEntity>().Result;
}
public TEntity GetById(Guid id)
{
return collection.Find(x => x.Id == id, null).ToListAsync<TEntity>().Result.FirstOrDefault();
}
}
我在下面的代码片段中使用这个 class:
public void Add()
{
if (!string.IsNullOrEmpty(word))
{
BlackListItem blackListItem =
new BlackListItem()
{
Word = word,
CreatedAt = DateTime.Now
};
var blackListItemRepository = new MongoDbRepository<BlackListItem>();
blackListItemRepository.Insert(blackListItem);
Word = string.Empty;
GetBlackListItems();
}
}
它工作正常,但我收到以下关于 blackListItemRepository.Insert(blackListItem);
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
我是 await 和 async 关键字的新手,我不确定我是否正确使用了它们。您对我的代码库 class 和我的用法有什么建议吗?
提前致谢,
当你调用异步方法时,你应该等待返回的任务,你只能在异步方法等中这样做。等待任务确保你只在操作完成后继续执行,否则操作和代码之后它会 运行 同时。
因此您的代码可能如下所示:
public async Task AddAsync()
{
if (!string.IsNullOrEmpty(word))
{
BlackListItem blackListItem =
new BlackListItem()
{
Word = word,
CreatedAt = DateTime.Now
};
var blackListItemRepository = new MongoDbRepository<BlackListItem>();
await blackListItemRepository.InsertAsync(blackListItem);
Word = string.Empty;
GetBlackListItems();
}
}
此外,异步方法有一个命名约定,即在名称中添加 "Async" 后缀。所以 AddAsync
而不是 Add
,InsertAsync
而不是 Insert
等等。