【问题标题】:Dapper multi-mapping Async extensionDapper 多映射异步扩展
【发布时间】:2018-09-07 20:21:11
【问题描述】:

下面是我在 dapper 中对多映射(一对多关系)的扩展

public static IEnumerable<TParent> QueryParentChild<TParent, TChild, TParentKey>(
    this IDbConnection connection,
    string sql,
    Func<TParent, TParentKey> parentKeySelector,
    Func<TParent, IList<TChild>> childSelector,
    dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)
{
    Dictionary<TParentKey, TParent> cache = new Dictionary<TParentKey, TParent>();

    connection.Query<TParent, TChild, TParent>(
        sql,
        (parent, child) =>
            {
                if (!cache.ContainsKey(parentKeySelector(parent)))
                {
                    cache.Add(parentKeySelector(parent), parent);
                }

                TParent cachedParent = cache[parentKeySelector(parent)];
                IList<TChild> children = childSelector(cachedParent);
                children.Add(child);
                return cachedParent;
            },
        param as object, transaction, buffered, splitOn, commandTimeout, commandType);

    return cache.Values;
}

现在我想将其转换为异步方法。我尝试了很多方法。但出现了一些错误。请让我知道需要做的更改

【问题讨论】:

    标签: c# asp.net-core dapper


    【解决方案1】:

    你有没有尝试过这样的事情,见下文:

    public static async Task<IEnumerable<TParent>> QueryParentChildAsync<TParent, 
                                                                         TChild, 
                                                                         TParentKey>(
        this IDbConnection connection,
        string sql,
        Func<TParent, TParentKey> parentKeySelector,
        Func<TParent, IList<TChild>> childSelector,
        dynamic param = null, 
        IDbTransaction transaction = null, 
        bool buffered = true, 
        string splitOn = "Id", 
        int? commandTimeout = null, 
        CommandType? commandType = null)
    {
        var cache = new Dictionary<TParentKey, TParent>();
    
        await connection.QueryAsync<TParent, TChild, TParent>(
            sql,
            (parent, child) =>
                {
                    var key = parentKeySelector(parent);
                    if (!cache.ContainsKey(key ))
                    {
                        cache.Add(key, parent);
                    }    
                    var cachedParent = cache[key];
                    var children = childSelector(cachedParent);
                    children.Add(child);
                    return cachedParent;
                },
            param as object, 
            transaction, 
            buffered, 
            splitOn, 
            commandTimeout, 
            commandType);
    
        return cache.Values;
    }
    

    【讨论】:

    • 你能举例说明如何调用这个函数吗?还收到了通过返回 Task> 而不仅仅是 IEnumerable 更正的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多