4

I am struggling with this issue:

I have a list of NHibernate objects called "Project". These objects contain a lazy - loaded list of "Branches". I am trying to pass a list of Projects to a WCF service so I am using AutoMapper to transform them to flat objects.

The problem is that even though the destination objects called "ProjectContract" does not contain a list of Branches, Automapper still invokes this collection and a lot of queries are made to the database because NHibernate fires the lazy - loading and loads the Branches collection for each project.

Here are the classes and the 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 to not touch the "Branches" collection because I don't care about it and that is a proxy that will trigger many database calls?

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

Thank you, Cosmin

1 Answer 1

2

Yes, The AutoMapper Ignore function.

Mapper.CreateMap<Source, Destination>()
    .ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());
Sign up to request clarification or add additional context in comments.

5 Comments

I think this only works if the property is also on the destination. In any case, even if I put the "Branches" on the destination and use the Ignore option, the collection is still accessed and the loading triggered. I am interested if there is a similar function to ignore the "source" member.
@noir, "There's nothing you need to do here, AutoMapper only really cares about the destination members. It only tries to map what it finds on the destination type." taken from automapper.codeplex.com/workitem/4031
That was what I thought as well, why bother with that collection that isn't even on the destination. But I did this simple test getting a list of projects and doing a mapping on the list to Projects Contracts. I used NhProf. First is the query that loads the projects, then for each project there is a query that loads the branches. If I look at the stack trace, it points to AutoMapper.Internal.PropertyGetter.GetValue... so I am trying to think either I am not seeing something or this is a bug.
@noir, ask AutoMapper: github.com/AutoMapper/AutoMapper/issues please be kind and inform me what they answered.
Problem solved. This is what was happening: In the ProjectContract, I had a property int BranchesCount {get;set; } that I was going to set later. I forgot AutoMapper is so smart as to deduce that I want that property mapped to Project.Branches.Count. Sorry, my bad. It took me two days to figure this out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.