AutoParter and NHibernate Lazy Loading

I am struggling with this problem:

I have a list of NHibernate objects called "Project". These objects contain a lazy list of "Branches". I am trying to transfer a list of projects to the WCF service, so I use AutoMapper to convert them to flat objects.

The problem is that even if the targets, called "ProjectContract", do not contain a list of branches, Automapper still calls this collection and a lot of queries come into the database because NHibernate starts lazy loading and loads the collection branches for each project.

Here are the classes and mapping:

public class Project { public virtual int ID { get; set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual IList<Branch> Branches { get; set; } } [DataContract] public class ProjectContract { [DataMember] public virtual int ID { get; set; } [DataMember] public virtual string Name { get; set; } [DataMember] public virtual string Description { get; set; } } public class ProjectMappings : Profile { protected override void Configure() { Mapper.CreateMap<Project, ProjectContract>(); } } 

My question is: is there a way to tell AutoMapper not to touch the โ€œBranchesโ€ collection, because it doesnโ€™t bother me, and is it a proxy server that will cause a lot of database queries?

I temporarily fixed this with MaxDepth (0), but there are other objects that have collections that I want to transfer, and collections that I don't want to touch, like this one. In this case, MaxDepth (0) will not work.

Thanks Cosmin

+4
source share
1 answer

Yes, the AutoMapper ignore feature.

 Mapper.CreateMap<Source, Destination>() .ForMember(dest => dest.SomeValuefff, opt => opt.Ignore()); 
+2
source

Source: https://habr.com/ru/post/1384511/


All Articles