【发布时间】:2019-05-01 20:34:09
【问题描述】:
每当我调用一个在 IQueryable 上接受 IEnumerable 的扩展方法时,使用该列表/实体的其余过程的速度非常慢。
POR QUEEEEE?!
问题似乎与代码的实际结构无关,因为它是最佳的,当我启动一个新数据库进行单元测试时,问题似乎没有出现。
这是扩展方法:
public static Bill FirstById(this IEnumerable<Bill> query, int billId)
{
return query.FirstOrDefault(r => r.Id == billId);
}
这就是它的使用方式:
public Bill GetDeposit(int depositId)
{
var deposit = _depositRepository
.AndSalesOrder()
.AndSalesOrderCustomerContact()
.AndDepositApplications()
.FirstById(depositId);
return deposit;
}
这就是实际实体的使用方式:
public bool ConvertDeposit(List<ConvertDepositSubmitModel> conversions)
{
if (conversions.Any())
{
var depositId = conversions.Select(c => c.DepositId)
.Distinct()
.Single();
var billPaymentIds = conversions.Select(c => c.BillPaymentId);
var deposit = _dataProvider.GetDeposit(depositId);
var billPayments = _dataProvider.GetBillPayments(billPaymentIds);
var totalConversionAmount = conversions.Sum(c => c.Amount);
var unappliedDepositAmount = (deposit.BillStatusId == BillStatus.Credited ? 0 : deposit.TotalSell - deposit.Balance) - deposit.DepositApplications.Select(a => a.Amount).DefaultIfEmpty(0).Sum();
if (unappliedDepositAmount != totalConversionAmount)
{
throw new Exception("The provided conversion amount would not fully convert the Deposit.");
}
_unitOfWork.TryTransactionAndCommit(() =>
{
foreach (var conversion in conversions)
{
var billPayment = billPayments.FirstByBillPaymentId(conversion.BillPaymentId);
this.CreateBillPaymentBill(deposit, conversion);
this.CreateMoneyOnAccountTransaction(deposit, conversion);
this.UpdateBillPayment(conversion, billPayment);
}
this.UpdateNetProceeds(billPayments);
this.ApplyCreditCardFees(billPaymentIds);
var customerCredit = this.CreateCustomerCredit(deposit, totalConversionAmount);
this.CreateCustomerCreditBill(deposit, customerCredit);
this.UpdateDeposit(deposit, totalConversionAmount);
});
}
else
{
throw new Exception("The provided set of bill payments was empty.");
}
return true;
}
我们看到,每种方法都经过严格测试,产生以下诊断结果:
PV2ANZAC
GetDeposit: 33434ms
GetBillPayments: 54ms
CreateBillPaymentBill1: 17775ms
CreateMoneyOnAccountTransaction1: 10774ms
UpdateBillPayment1: 10810ms
UpdateNetProceeds: 18130ms
ApplyCreditCardFees: 17206ms
Insert CustomerCredit: 10795ms
CustomerCredit SaveChanges: 16276ms
CreateCustomerCredit: 27075ms
CreateCustomerCreditBill: 10688ms
而且我们绝对希望一切都比实际情况至少小一个数量级。
【问题讨论】:
-
...当我启动一个新数据库进行单元测试... 那么您不是单元测试。单元测试没有依赖项。您正在描述集成测试。
标签: c# sql entity-framework extension-methods