Dapper vs ADO.Net用反射哪个更快? [英] Dapper vs ADO.Net with reflection which is faster?

查看:432
本文介绍了Dapper vs ADO.Net用反射哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了有关Dapper和ADO.NET的信息,并对二者进行了选择测试,发现ADO.NET有时比Dapper更快,而有时却相反。我了解这可能是数据库问题,因为我正在使用SQL Server。据说反射很慢,我在ADO.NET中使用反射。那么谁能告诉我哪种方法最快?

I have studied about Dapper and ADO.NET and performed select tests on both and found that sometimes ADO.NET is faster than Dapper and sometimes is reversed. I understand this could be database issues as i am using SQL Server. As it is stated that reflection is slow and i am using reflection in ADO.NET. So can anyone tell me which approach is the fastest?

这是我编写的代码。


  1. 使用ADO.NET

  1. Using ADO.NET

DashboardResponseModel dashResp = null;
SqlConnection conn = new SqlConnection(connStr);
try
{
    SqlCommand cmd = new SqlCommand("spGetMerchantDashboard", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@MID", mid);
    conn.Open();
    var dr = cmd.ExecuteReader();

List<MerchantProduct> lstMerProd = dr.MapToList<MerchantProduct>();
List<MerchantPayment> lstMerPay = dr.MapToList<MerchantPayment>();

if (lstMerProd != null || lstMerPay != null)
{
    dashResp = new DashboardResponseModel();
    dashResp.MerchantProduct = lstMerProd == null ? new 
    List<MerchantProduct>() : lstMerProd;
    dashResp.MerchantPayment = lstMerPay == null ? new 
    List<MerchantPayment>() : lstMerPay;
}

dr.Close();

}

return dashResp;


  • 使用Dapper

  • Using Dapper

    DashboardResponseModel dashResp = null;
    
    var multipleresult = db.QueryMultiple("spGetMerchantDashboard", new { mid = 
    mid }, commandType: CommandType.StoredProcedure);
    var merchantproduct = multipleresult.Read<MerchantProduct>().ToList();
    var merchantpayment = multipleresult.Read<MerchantPayment>().ToList();
    
    if (merchantproduct.Count > 0 || merchantpayment.Count > 0)
    dashResp = new DashboardResponseModel { MerchantProduct = 
    merchantproduct, MerchantPayment = merchantpayment };
    
    return dashResp;
    



  • 推荐答案

    Dapper基本上将ADO.NET扩展为一个非常薄的抽象-因此,从理论上讲,它不能比编写良好的ADO.NET代码更快(尽管说实话:大多数人都不是。

    Dapper basically straddles ADO.NET as a very thin abstraction - so in theory it can't be faster than well written ADO.NET code (although to be honest: most people don't write well written ADO.NET code).

    尽管如此,它实际上几乎无法区分。假设您使用的是 just Dapper(不是上面的任何东西),那么它不包括任何查询生成,表达式树/ DSL解析,复杂的模型配置或任何

    It can be virtually indistinguishable, though; assuming you're using just Dapper (not any of the things that sit on top of it) then it doesn't include any query generation, expression tree / DSL parsing, complex model configuration, or any of those other things that tend to make full ORMs more flexible but more expensive.

    相反:它只专注于执行用户提供的查询和映射结果;它的作用是通过IL-emit生成所有实现代码(如何将 MerchantProduct 映射到您的列)并将其缓存在某个地方。同样,它以相同的方式准备许多参数准备代码。因此,在运行时,通常 只是从缓存中获取两个委托实例,然后调用它们。

    Instead: it focuses just on executing user-supplied queries and mapping results; what it does is to generate all of the materialization code (how to map MerchantProduct to your columns) via IL-emit and cache that somewhere. Likewise it prepares much of the parameter preparation code in the same way. So at runtime it is usually just fetching two delegate instances from cache and invoking them.

    由于(延迟到RDBMS +查询,执行成本+结果的网络带宽成本)将远远高于从词典中获取两个委托的开销,我们可以基本上忽略该成本。

    Since the combination of (latency to the RDBMS + query execution cost + network bandwidth cost of the results) is going to be much higher than the overhead of fetching two delegates from dictionaries, we can essentially ignore that cost.

    简而言之:很少会在这里测量出大量开销。

    In short: it would be rare that you can measure a significant overhead here.

    作为代码的次要优化:更喜欢 AsList() ToList(),以避免创建副本。

    As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy.

    这篇关于Dapper vs ADO.Net用反射哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆