我有一个异步方法,它将通过 Api 查找作业调度服务的 jobId。
如果没有找到结果,是返回空任务还是 null 更好?
据我所知,当返回集合时,最好返回空集合而不是 null;使用对象时最好返回 null 而不是空对象;但对于任务,我不确定哪个更好。请参见附加的方法。
谢谢
public virtual Task<int> GetJobRunIdAsync(int jobId)
{
var jobMonRequest = new jobmonRequest(true, true, true, true, true,
true, true, true, true, true, true, true,
true,
true, true, true, DateTime.Today, jobId, null, 0, null, null,
null, null, 0, 0);
var jobMonResponseTask = Client.jobmonAsync(jobMonRequest);
var jobTask = jobMonResponseTask.ContinueWith(task =>
{
if (jobMonResponseTask.Result == null )
{
var empty = new Task<int>(() => 0); // as i understand creating a task with a predefined result will reduce overhead.
return empty.Result; // || is it better to just return null?
}
if (jobMonResponseTask.Result.jobrun.Length > 1)
{
throw new Exception("More than one job found, Wizards are abound.");
}
return jobMonResponseTask.Result.jobrun.Single().id;
});
return jobTask;
}
int?
(Nullable<int>
) 而不是 int。如果你想要 "真正" 的异步,你应该将方法签名写成public async Task<int?> GetJobRunIdAsync(int jobId)
。 - E. Moffat