Dapper 与 ADO.Net 的反射哪个更快?

Dapper vs ADO.Net with reflection which is faster?(Dapper 与 ADO.Net 的反射哪个更快?)
本文介绍了Dapper 与 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 代码(虽然说实话:大多数人不写写得很好的 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 解析、复杂模型配置或任何那些倾向于使完整的 ORM 更灵活但更昂贵的其他事情.

    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 与 ADO.Net 的反射哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

    相关文档推荐

    DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
    Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
    Programmatically show the desktop(以编程方式显示桌面)
    c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
    InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
    LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)