Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动制版机性能_C#_Performance_Automapper - Fatal编程技术网

C# 自动制版机性能

C# 自动制版机性能,c#,performance,automapper,C#,Performance,Automapper,我正在使用Automapper将我的业务模型映射到ViewModel 它能工作,但速度很慢 我有一个包含6893个对象和23个属性的集合(测试环境、生产应该有更多) 使用循环,需要00:02:32.8118534映射所有内容 var objects = // get all items (returns a collection of MyObj) List<MyViewModel> collection = new List<MyViewModel>(); foreac

我正在使用Automapper将我的业务模型映射到ViewModel

它能工作,但速度很慢

我有一个包含6893个对象和23个属性的集合(测试环境、生产应该有更多)

使用循环,需要
00:02:32.8118534
映射所有内容

var objects = // get all items (returns a collection of MyObj)
List<MyViewModel> collection = new List<MyViewModel>();
foreach (MyObj obj in objects)
{
     MyViewModel vm = Mapper.Map<MyObj, MyViewModel>(obj);
     collection.Add(vm);
}
MyObj:

public partial class MyObj
{
    public MyObj()
    {
        this.MyObjOtherObj= new HashSet<MyObjOtherObj>();
    }

    public long a{ get; set; }
    public short b{ get; set; }
    public string c{ get; set; }
    public string d{ get; set; }
    public string e{ get; set; }
    public string f{ get; set; }
    public string g{ get; set; }
    public string h{ get; set; }
    public string i{ get; set; }
    public string j{ get; set; }
    public string k{ get; set; }
    public string l{ get; set; }
    public string m{ get; set; }
    public bool n{ get; set; }
    public bool o{ get; set; }
    public bool p{ get; set; }
    public bool q{ get; set; }

    public virtual ICollection<MyObjOtherObj> MyObjOtherObj{ get; set; }
    public virtual Types Types { get; set; }
}
MyViewModelOtherObj:

public class MyViewModelOtherObj
{
    public long Id { get; set; }
    public long MyObjId { get; set; }
    public short x{ get; set; }
    public string z{ get; set; }
    public string SourceTypesDescription { get; set; }
}
编辑:

源类型:

public partial class SourceTypes
{
    public SourceTypes()
    {
        this.MyObjOtherObj = new HashSet<MyObjOtherObj>();
    }

    public short SourceTypeId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<MyObjOtherObj> MyObjOtherObj { get; set; }
}
公共部分类源类型
{
公共源类型()
{
this.MyObjOtherObj=new HashSet();
}
公共短源类型ID{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection MyObjOtherObj{get;set;}
}
类型:

public partial class Types
{
    public Types()
    {
        this.MyObj = new HashSet<MyObj>();
    }

    public short TypeId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<MyObj> MyObj{ get; set; }
}
公共部分类类型
{
公共类型()
{
this.MyObj=新的HashSet();
}
公共短TypeId{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection MyObj{get;set;}
}

5.0版本的AutoMapper有显著的性能提升。在我们的基准测试中,使用您在这里展示的非常类似的类型,我们可以在一秒钟多一点的时间内映射一百万个项目。在即将发布的5.1版本中,这会缩小得更多,我们只比手动映射慢3倍左右,主要是因为手动映射无法执行空检查


我要升级。

为了回应我们的评论,您需要立即加载您的收藏对象。请看以下文章,这将解决您的问题:


你能把两个物体的代码都贴出来吗?你有什么问题吗?你试过寻找热点吗?其他对象是如何创建的?你有没有检查过它们是完全加载的对象,也就是说,它没有因为ORM懒散加载附加对象而变慢?@RichLinnell是的,这些对象是完全加载的。你使用的是什么版本?我使用的是上一个版本(5.0.2),那么我真的很困惑,我刚刚在我们的基准测试中运行了您的精确对象结构,并在1.97秒内映射了1M个项目。这是我的基准:我应该在5.1alpha上加上这个,我们在它上面加了一点性能。我检查了你的要点,几乎是一样的。我不确定有什么不同。问题可能是objetcs类型和SourceType上的虚拟属性吗?两者都是实体框架创建的对象。类型有一个虚拟属性,它是MyObj的集合。和SourceType是一个虚拟属性,它是MyObjOtherObj的集合。不知道AutoMapper是如何工作的。但这可能是一个循环引用,某种程度上干扰了AutoMapper?不。。。问题可能是对象MyObjOtherObj上的虚拟财产吗?它也是实体框架创建的一个对象,并且有一个虚拟财产MyObj。非常感谢!我不知道那件事。我将其更改为
List myObjs=dbContext.MyObj.Include(“MyObjOtherObj”).ToList()现在整个请求需要13秒(17803个对象)。这不是完美的,但可以接受。
public partial class MyObjOtherObj
{
    public long id{ get; set; }
    public long MyObjId { get; set; }
    public short x{ get; set; }
    public string z{ get; set; }

    public virtual MyObj MyObj{ get; set; }
    public virtual SourceTypes SourceTypes { get; set; }
}
public class MyViewModelOtherObj
{
    public long Id { get; set; }
    public long MyObjId { get; set; }
    public short x{ get; set; }
    public string z{ get; set; }
    public string SourceTypesDescription { get; set; }
}
public partial class SourceTypes
{
    public SourceTypes()
    {
        this.MyObjOtherObj = new HashSet<MyObjOtherObj>();
    }

    public short SourceTypeId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<MyObjOtherObj> MyObjOtherObj { get; set; }
}
public partial class Types
{
    public Types()
    {
        this.MyObj = new HashSet<MyObj>();
    }

    public short TypeId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<MyObj> MyObj{ get; set; }
}