当直接从数据库提供 IEnumerable 时,控制器如何工作?哪个代码更正确、更优化?假设数据库非常慢并且正在进行其他操作。
这个示例非常简单,因此执行时间可能没有足够的差异,但我正在尝试学习最佳实践。
#1
public Task<Application[]> Find(Expression<Func<Application, bool>> predicate)
{
return DatabaseContext.Applications
.Where(predicate)
.ToArrayAsync();
}
...
public Task<Application[]> Find(...)
{
return ApplicationService.Find(...);
}
#2
public Task<List<Application>> Find(Expression<Func<Application, bool>> predicate)
{
return DatabaseContext.Applications
.Where(predicate)
.ToListAsync();
}
...
public async Task<IActionResult> Find(...)
{
var applications = await ApplicationService.Find(...)
return Ok(applications);
}
#3
public IEnumerable<Application> Find(Expression<Func<Application, bool>> predicate)
{
return DatabaseContext.Applications;
}
...
public IActionResult<IEnumerable<Application>> Find(...)
{
var applications = ApplicationService.Find(...);
return Ok(applications);
}
呼啦一阵风
拉风的咖菲猫
相关分类