Fastest way to map result of SqlDataReader to object

后端 未结 11 1096
花落未央
花落未央 2021-02-04 08:49

I\'m comparing materialize time between Dapper and ADO.NET and Dapper. Ultimately, Dapper tend to faster than ADO.NET, though the first time a given fetch query was executed

11条回答
  •  一个人的身影
    2021-02-04 09:08

    There is a SqlDataReader Mapper library in NuGet which helps you to map SqlDataReader to an object. Here is how it can be used (from GitHub documentation):

    var mappedObject = new SqlDataReaderMapper(reader)
        .Build();
    

    Or, if you want a more advanced mapping:

    var mappedObject = new SqlDataReaderMapper(reader)
         .NameTransformers("_", "")
         .ForMember("CurrencyId")
         .ForMember("CurrencyCode", "Code")
         .ForMember("CreatedByUser", "User").Trim()
         .ForMemberManual("CountryCode", val => val.ToString().Substring(0, 10))
         .ForMemberManual("ZipCode", val => val.ToString().Substring(0, 5), "ZIP")
         .Build();
    

    Advanced mapping allows you to use name transformers, change types, map fields manually or even apply functions to the object's data so that you can easily map objects even if they differ with a reader.

提交回复
热议问题