【发布时间】:2018-02-08 18:52:46
【问题描述】:
在我们的代码中,我们将域实体映射到 API DTO 对象。在转换实体的过程中返回了大量的记录。当我们尝试将这些记录映射到 DTO 时,会花费大量时间。 这是一个例子
Class test
{
public virtual int ID { get; protected internal set; }
public virtual Employee Employee { get; protected internal set; }
public virtual BusinessDate Start { get; protected internal set; }
protected internal virtual IList<ABC> _abc { get; set; }
public virtual IEnumerable<ABC> ABCRange{
get
{
return _abc;
}
}
public IEnumerable<test> GetByEmployees(IEnumerable<int> employees)
{
return ChunkIDsQueryForSQLServer2100Limit(employees, empArray =>
(from ar in GetAllAsQueryable()
where empArray.Contains(ar.Employee.ID)
select ar).ToList());
}
class ABC
{
public virtual int ID { get; protected internal set; }
public virtual int DayOfWeek { get; protected internal set; }
public virtual bool StartTimeOffset { get; protected internal set; }
public virtual bool EndTimeOffset { get; protected internal set; }
public virtual AvailabilityType Type { get; protected internal set; }
}
DTO Mapper:
return new AvailabilityRequestCollectionResource
{
AvailabilityRequests = availabilityRequest.Select(AvailabilityRequestMap).ToList()
};
private static TEST2 AvailabilityRequestMap(TEST test)
{
var availabilityRequestResource = new TEST2
{
ID = availabilityRequest.ID,
EmployeeID = availabilityRequest.Employee.ID,
GeneralAvailability = AvailabilityTypeMap(availabilityRequest.ABCRange.Where(f => f.Type == AvailabilityType.General)),
PreferredAvailability = AvailabilityTypeMap(availabilityRequest.ABCRange.Where(f => f.Type == AvailabilityType.Preferred))
};
}
private static List<XYZ> AvailabilityTypeMap(IEnumerable<ABC> abc)
{
var availList = new List<XYZ>();
availList.AddRange(abc.Select(x =>
new XYZ
{
ID = x.ID,
DayOfWeek = (Day)x.DayOfWeek,
StartTimeOffset = x.StartTimeOffset,
EndTimeOffset = x.EndTimeOffset,
WeekNumber = 1
}
));
return availList;
}
现在的问题是上述方法需要将近 15 秒才能创建 10k 个 XYZ 对象。我们的性能瓶颈是 2 秒,并且每次都没有。的对象可能会有所不同。怎么做? 我们尝试了并行性和任务分配,但这些都没有帮助,因为从域中进行了延迟加载,我们无法修改。
注意:ABC 和 XYZ 的属性集不同
谢谢
【问题讨论】:
-
你能分享你如何得到你的
IEnumerable<ABC>(你可能是从一些DbContext或其他东西加载它),并显示你的ABC类的定义。 -
我们有一个由存储库方法加载的域实体类。公共虚拟 IEnumerable
abc { 获取 { 返回 _abc; } } 受保护的内部虚拟 IList _availabilityRanges { get;放; } public IEnumerable GetByEmployees(IEnumerable employees) { return ChunkIDsQueryForSQLServer2100Limit(employees, empArray => (from ar in GetAllAsQueryable() where empArray.Contains(ar.Employee.ID) select ar).ToList()); } 是存储库调用 -
添加了实际代码
标签: c# .net performance