简体   繁体   English

Car[] (array) 和 IEnumerable 有区别吗<Car>什么时候使用 AutoMapper?

[英]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 包AutoMapperAutoMapper.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:

  • IEnumerable: 21 milliseconds; IEnumerable:21 毫秒;
  • Array: 7 milliseconds;数组:7 毫秒;

Measuring Array first:首先测量阵列:

  • IEnumerable: 7 milliseconds; IEnumerable:7 毫秒;
  • Array: 21 milliseconds;数组:21 毫秒;

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.)首先映射与此对象相关的集合(以预热映射、初始化缓存等)

  • IEnumerable: 57704 ticks; IEnumerable:57704 个滴答声;
  • Array: 122014 ticks;数组:122014 个滴答声;

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:当输入是数组时:

  • IEnumerable: 18642 ticks; IEnumerable:18642 个滴答声;
  • Array: 1392 ticks;数组:1392 个滴答声;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM