【问题标题】:What are the features of the BindAsync function?BindAsync 函数有哪些特点?
【发布时间】:2018-07-16 20:05:57
【问题描述】:

BindAsync函数有什么特点?

我有一个在 get all 后使用此命令的示例方法。

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 方法是干什么用的,它是如何工作的?

【问题讨论】:

    标签: c# azure asynchronous bind


    【解决方案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);
    

    意思是在应用作为binder 传入的函数之后,它获取传入的异步操作的结果并将其暴露在Task 中。

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 2017-01-05
      • 2013-12-11
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      相关资源
      最近更新 更多