将 Dapper 与 Enterprise Library Data Access Access 块进行比较,以通过存储过程获取数据。使用 Dapper 时,我看到了大约 40% 的整体性能优势,这有点令人惊讶。
但是,在比较迭代并从 IEnumerable(IDataRecord) 与 IEnumerable(dynamic) 获取数据时,IEnumerable(IDataRecord) 大约快一个数量级。这种行为是否很好理解并且可以预期,或者这里有什么不对的地方?
结果:
IEnumerable(IDataRecord)
IEnumerable(dynamic) - 使用 dapperObject.propertyName
现在有趣的部分是,当使用 dapperObject["propertyName"] 时,性能与 IDataRecord 相当。完全不是我所期望的。
分析代码的相关部分
using System;
using System.Collections.Generic;
using System.Linq;
using Dapper.DataAccess;
using System.Data;
using tophat;
namespace Dapper.TestRunner
{
class Program
{
static void Main(string[] args)
{
var connectionString = "data source=WEBDBdev3,1866; User id=hsbmhw;Password=gEner4Y&M;Persist Security Info='true'; initial catalog=myhomeworks;";
//The following uses Tophat to create a singleton connection instance.
Database.Install<SqlServerConnectionFactory>(connectionString, ConnectionScope.ByRequest);
DapperTest();
DapperTest2();
EnterpriseLibraryIDataRecordTest();
}
private static void DapperTest()
{
for (int i = 0; i < 100; i++)
{
IEnumerable<dynamic> users = MyRepository.GetUsersDapper();
PopulateBusinessObjectsDynamic(users);
}
}
private static void DapperTest2()
{
for (int i = 0; i < 100; i++)
{
IEnumerable<dynamic> users = MyRepository.GetUsersDapper();
PopulateBusinessObjectsDynamic2(users);
}
}
private static void EnterpriseLibraryIDataRecordTest()
{
for (int i = 0; i < 100; i++)
{
IEnumerable<IDataRecord> users = MyRepository.GetUsersEntlib();
PopulateBusinessObjectsIDataRecord(users);
}
}
private static void PopulateBusinessObjectsDynamic(IEnumerable<dynamic> users)
{
foreach (var user in users)
{
BusinessObject bo = new BusinessObject(user);
}
}
private static void PopulateBusinessObjectsDynamic2(IEnumerable<dynamic> users)
{
foreach (var user in users)
{
BusinessObject bo = new BusinessObject(user);
}
}
private static void PopulateBusinessObjectsIDataRecord(IEnumerable<IDataRecord> users)
{
foreach (var user in users)
{
BusinessObject bo = new BusinessObject(user);
}
}
}
public class BusinessObject
{
public DateTime CreateDate { get; set; }
public String CreateDateString { get; set; }
public String FirstName { get; set; }
public bool IsApproved { get; set; }
public bool IsLockedOut { get; set; }
public DateTime LastActivityDate { get; set; }
public DateTime LastLoginDate { get; set; }
public String LastName {get;set;}
public String Organization{get;set;}
public int OrganizationId{get;set;}
public int PersonId{get;set;}
public String ProfileLastUpdatedBy{get;set;}
public DateTime ProfileLastUpdatedDate{get;set;}
public String RoleName{get;set;}
public long RowNumber{get;set;}
public int TotalCount{get;set;}
public Guid UserId{get;set;}
public string UserName {get;set;}
public string UserStatus{get;set;}
public BusinessObject(dynamic user)
{
CreateDate=user.CreateDate;
CreateDateString = user.CreateDateString;
FirstName = user.FirstName;
IsApproved = user.IsApproved;
IsLockedOut = user.IsLockedOut;
LastActivityDate= user.LastActivityDate;
LastLoginDate = user.LastLoginDate;
LastName = user.LastName;
Organization = user.organization;
OrganizationId=user.organization_id;
PersonId = user.party_id;
ProfileLastUpdatedBy = user.ProfileLastUpdatedBy;
ProfileLastUpdatedDate = user.ProfileLastUpdatedDate;
RoleName = user.RoleName;
RowNumber = user.RowNumber;
TotalCount = user.TotalCount;
UserId = user.UserId;
UserName= user.UserName;
UserStatus = user.UserStatus;
}
public BusinessObject(bool x, dynamic user)
{
CreateDate = user["CreateDate"];
CreateDateString = user["CreateDateString"];
FirstName = user["FirstName"];
IsApproved = user["IsApproved"];
IsLockedOut = user["IsLockedOut"];
LastActivityDate = user["LastActivityDate"];
LastLoginDate = user["LastLoginDate"];
LastName = user["LastName"];
Organization = user["organization"];
OrganizationId = user["organization_id"];
PersonId = user["party_id"];
ProfileLastUpdatedBy = user["ProfileLastUpdatedBy"];
ProfileLastUpdatedDate = user["ProfileLastUpdatedDate"];
RoleName = user["RoleName"];
RowNumber = user["RowNumber"];
TotalCount = user["TotalCount"];
UserId = user["UserId"];
UserName = user["UserName"];
UserStatus = user["UserStatus"];
}
public BusinessObject(IDataRecord user)
{
CreateDate = (DateTime)user["CreateDate"];
CreateDateString = (string)user["CreateDateString"];
FirstName = (string)user["FirstName"];
IsApproved = (bool)user["IsApproved"];
IsLockedOut = (bool)user["IsLockedOut"];
LastActivityDate = (DateTime)user["LastActivityDate"];
LastLoginDate = (DateTime)user["LastLoginDate"];
LastName = (string)user["LastName"];
Organization = (string)user["organization"];
OrganizationId = (int)user["organization_id"];
PersonId = (int)user["party_id"];
ProfileLastUpdatedBy = (string)user["ProfileLastUpdatedBy"];
ProfileLastUpdatedDate = (DateTime)user["ProfileLastUpdatedDate"];
RoleName = (string)user["RoleName"];
RowNumber = (long)user["RowNumber"];
TotalCount = (int)user["TotalCount"];
UserId = (Guid)user["UserId"];
UserName = (string)user["UserName"];
UserStatus = (string)user["UserStatus"];
}
}
}