BindAsync函数的功能是什么?

时间:2018-07-16 20:05:57

标签: c# azure asynchronous bind

BindAsync函数的功能是什么?

我有一个示例方法,在全部获取后使用此命令。

public async Task<Either<Exception, List<Clients>>> BuscarClientes()
{
    return await GetAllByAsync(x => true)
                    .BindAsync(clients => 
        {
            clients = clients.Where(x => x.active == "S")
                             .OrderBy(x => x.Name)
            return clients
        }
}

此方法创建一条规则,该规则返回按名称排序的活动客户端列表。

它通过GetAllByAsync方法返回此列表:

public virtual async Task<Either<Exception, IEnumerable<TEntity>>> GetAllByAsync(Expression<Func<TEntity, bool>> parameter)
{
    try
    {
        return Right<Exception, Option<TEntity>>(await DBEntity.Where(parameter).ToListAsync());
    }
    catch
    {
        return ex;
    }
}

谁能告诉我这种BindAsync方法的作用,它如何工作?

1 个答案:

答案 0 :(得分:1)

我假设BindAsync扩展功能来自C#的Monacs功能扩展。

文档中描述的方法的目的是

/// <summary>
/// Transforms the <paramref name="result"/> into another <see cref="Result{T}"/> using the <paramref name="binder"/> function.
/// If the input result is Ok, returns the value of the binder call (which is <see cref="Result{T}"/> of <typeparamref name="TOut"/>).
/// Otherwise returns Error case of the Result of <typeparamref name="TOut"/>.
/// </summary>
/// <typeparam name="TIn">Type of the value in the input result.</typeparam>
/// <typeparam name="TOut">Type of the value in the returned result.</typeparam>
/// <param name="result">The result to bind with.</param>
/// <param name="binder">Function called with the input result value if it's Ok case.</param>
public static async Task<Result<TOut>> BindAsync<TIn, TOut>(this Result<TIn> result, Func<TIn, Task<Result<TOut>>> binder) =>
    result.IsOk ? await binder(result.Value) : Error<TOut>(result.Error);

意思是,在应用以Task传入的函数之后,它采用传入的异步操作的结果并将其包装在binder中公开。