【问题标题】:how to use ready-made Func<> delegate in QueryAsync?如何在 QueryAsync 中使用现成的 Func<> 委托?
【发布时间】:2020-12-16 15:07:11
【问题描述】:

我有一个问题,我的方法中有相同的 Func。我是单独拿出来的,但现在的问题是如何给它传参数。我想在几个地方使用它,但是我不知道如何从QueryAsync方法中获取必要的参数

我的 func 委托

private Func<Authors, AuthorInPrintingEditions, PrintingEditions, Authors> _relationDelegate =
        (Authors authors, AuthorInPrintingEditions relation, PrintingEditions printingEdition) =>
        {
            if (!_dictionaryResult.ContainsKey(authors.Id))
            {
                _dictionaryResult.Add(authors.Id, new Authors()
                {
                    Id = authors.Id,
                    Name = authors.Name,
                    AuthorInPrintingEditions = printingEdition == null
                        ? new List<AuthorInPrintingEditions>()
                        : new List<AuthorInPrintingEditions>()
                        {
                            new AuthorInPrintingEditions()
                            {
                                PrintingEdition = printingEdition
                            }
                        }
                });
            }
            else
            {
                if (printingEdition != null)
                {
                    _dictionaryResult[authors.Id].AuthorInPrintingEditions.Add(new AuthorInPrintingEditions()
                    {
                        PrintingEdition = printingEdition
                    });
                }
            }

            return authors;
        };

我的方法,当我想用​​的时候

        using (var conneсtion = CreateConnection())
        {
            await conneсtion.QueryAsync<Authors, AuthorInPrintingEditions, PrintingEditions, Authors>(query,
                (authors, authorInPrintingEditions, printingEdition), <--- in this row I want use delegate
                new { skip = skip, pageSize = pageSize }, splitOn: "Id,AuthorId,Id");

            return _dictionaryResult.Values.ToList();
        }

不明白怎么给它传参数,一般情况下是否可行,还是复制粘贴会比较容易

【问题讨论】:

  • 在那一行输入_relationDelegate
  • 参数化可重用委托的一个选项是将委托和参数公开为某个类的实例成员。然后,当您需要此委托时,创建该类的实例,设置参数并将委托传递给异步查询。
  • 全错了...返回IEnumerable&lt;AuthorPrintingEditionPair&gt; 然后使用ToDictionary(...) 扩展
  • @Dusan 是的,我做到了,它成功了

标签: c# sql .net sql-server dapper


【解决方案1】:

如果您希望重用委托并且还能够使用参数, 尝试使用这样的东西:

public class AuthorsMapper
{
    public int Parameter1;
    public string Parameter2;

    public Func<Authors, AuthorInPrintingEditions, PrintingEditions, Authors> Map =>
    // Your existing code that can now use parameters
}

并像这样使用它:

var mapper = new AuthorsMapper 
{
    Parameter1 = 742,
    Parameter2 = "House"
};

await conneсtion.QueryAsync<Authors, AuthorInPrintingEditions, PrintingEditions, Authors>
    (query, mapper.Map, ...);

【讨论】:

  • 我会称它为 Delegate 以外的其他名称。
  • 谢谢,Dapper 将此函数称为Map,因此我将根据该函数更改我的答案。
  • 给它起一个有意义的名字;类似PrintingEditionsDelegatePrintingEditionsMapAuthorEditionsMap
  • Deleagate 也具有误导性。因为这是在Dapper 的上下文中使用的,所以我会坚持在那里使用的命名。来自 Dapper 源代码:Func&lt;TFirst, TSecond, TThird, TReturn&gt; map
  • 那是参数的名字,不是参数的名字。无论如何,我已经说了我的平安。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 2014-01-10
  • 2019-10-07
相关资源
最近更新 更多