[英]Is there a difference between Car[] (array) and IEnumerable<Car> when using AutoMapper?
In several projects I am using the mapping functions of AutoMapper.在几个项目中,我使用了 AutoMapper 的映射功能。 I will use the object Car as an example through the full question.我将使用对象Car作为整个问题的示例。
First my set-up :首先我的设置:
I define my maps with profiles:我用配置文件定义我的地图:
public class CarProfile : Profile
{
public CarProfile()
{
CreateMap<Car, CarResponse>();
}
}
I initialize AutoMapper in ASP.NET Core Web API with:我在 ASP.NET Core Web API 中初始化AutoMapper:
services.AddAutoMapper();
Using the NuGet packages AutoMapper
and AutoMapper.Extensions.Microsoft.DependencyInjection
使用 NuGet 包AutoMapper
和AutoMapper.Extensions.Microsoft.DependencyInjection
The mapper is automically injected in the constructor and set as a private field _mapper
.映射器自动注入构造函数并设置为私有字段_mapper
。
Eventually I map a collection with the following piece of code:最终,我使用以下代码映射了一个集合:
_mapper.Map<IEnumerable<Car>, IEnumerable<CarResponse>>(_context.Cars);
It's also worth to mention that _context.Cars
is in this case an DbSet<T>
.还值得一提的是_context.Cars
在这种情况下是一个DbSet<T>
。
Now my actual question is: would it be different if I would use an array
instead of a IEnumerable
.现在我的实际问题是:如果我使用array
而不是IEnumerable
会有所不同。
Example given :给出的例子:
_mapper.Map<Car[], CarResponse[]>(_context.Cars);
Please consider performance, clean code and behavioural differences.请考虑性能、干净的代码和行为差异。
Behavioural differences行为差异
I generated the execution plan and I can conclude that there at least is a difference.我所产生的执行计划,我可以得出结论,至少是有区别的。 See this link for my comparison: https://www.diffnow.com/?report=elujt .请参阅此链接以进行比较: https : //www.diffnow.com/?report=elujt 。 Analyzing the C# code makes clear that there happens more when using an array.分析 C# 代码清楚地表明,使用数组时会发生更多情况。
Performance表现
Since this still does not give any insights in performance, I measured that using Stopwatch .由于这仍然没有提供任何性能方面的见解,因此我使用Stopwatch 进行了测量。
var watchienumerable = Stopwatch.StartNew();
var ienumerable = _mapper.Map<IEnumerable<Car>, IEnumerable<CarResponse>>(contextDevices);
watchienumerable.Stop();
Measuring IEnumerable first:首先测量IEnumerable:
Measuring Array first:首先测量阵列:
These results are not representative, because the mapper still needs to be warmed up.这些结果并不具有代表性,因为mapper还需要预热。
Mapping a collection related to this object first (to warm up the mapping, initialize caching etc.)首先映射与此对象相关的集合(以预热映射、初始化缓存等)
I can conclude that mapping to an IEnumerable from (something that is not an array) is faster than mapping to an array.我可以得出结论,从(不是数组的东西)映射到 IEnumerable 比映射到数组更快。 With hindsight, it sounds quite obvious.事后看来,这听起来很明显。
When the input is an array:当输入是数组时:
It's also worth to mention that if use the actual type of _context.Cars
which is DbSet<Car>
.还值得一提的是,如果使用_context.Cars
的实际类型,即DbSet<Car>
。 It is exactly as fast as when I use IEnumerable<Car>
.它和我使用IEnumerable<Car>
时一样快。
Word of advice: I would use the type that is closest to the input and still matches your desired output.忠告:我会使用最接近输入的类型,并且仍然与您想要的输出匹配。 In my case, I will stick to using IEnumerable.就我而言,我将坚持使用 IEnumerable。
Clean code干净的代码
This part of my answer is highly opiniated and very open to comments.我的这部分答案是高度固执的,并且非常欢迎评论。 But I think Car[]
reads better than IEnumerable<Car>
.但我认为Car[]
比IEnumerable<Car>
读起来更好。
However since there are behavioural and performance differences I don't think it's clean to use an array here.但是,由于存在行为和性能差异,我认为在这里使用数组并不干净。 It also changes the intent.它也改变了意图。 So using IEnumerable<T>
would be better.所以使用IEnumerable<T>
会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.