我有一个TransactionOperator
公开以下静态异步方法的类:
public static async Task<bool> ProcessTransactionAsync(Transaction transaction)
{
var someTransactionOperator = ...; // get appropriate operator
// some code here
bool success = await someTransactionOperator.Process(transaction);
// some more code
return bool;
}
现在,我想在Transaction
类中提供一个包装器实例方法。我的问题是,哪一种是正确/推荐的写作方式?我倾向于#2,因为它感觉正确,但我没有任何支持该选择的论据。
// Option 1
public bool ProcessAsync()
{
return TransactionOperator.ProcessTransactionAsync(this).Result;
}
// Option 2
public Task<bool> ProcessAsync()
{
return TransactionOperator.ProcessTransactionAsync(this);
}
// Option 3 (compiler warning because there's no 'await' operator)
public async Task<bool> ProcessAsync()
{
return TransactionOperator.ProcessTransactionAsync(this).Result;
}
// Option 4
public async Task<bool> ProcessAsync()
{
return await TransactionOperator.ProcessTransactionAsync(this);
}