3

I have the following scenario.

public class DictionaryEntity
{
    public virtual string DictionaryName { get; set; }

    public virtual IList<DictionaryRecordEntity> DictionaryRecord { get; set; }
}

public class DictionaryDto
{
     public string DictionaryName { get; set; }

     public IList<DictionaryRecordEntity> DictionaryRecord { get; set; }
}

I'm using Automapper and NHibernate. In NHibernate the DictionaryRecord property is marked as lazy loaded.

When I make the mapping from DictionaryEntity -> DictionaryDto, Automapper loads all my DictionaryRecords.

But I don't want this behavior, is there a way to configure the Automapper in order to don't resolve this property until I really access this property.

My workaround for this situation consists of splitting the DictionaryEntity in 2 classes and create a second Automapper mapping.

public class DictionaryDto
{
     public string DictionaryName { get; set; }
}

public class DictionaryDtoFull : DictionaryDto
{
     public IList<DictionaryRecordEntity> DictionaryRecord { get; set; }
}

and then in the code, depending on the need, call AutoMapper.Map appropriately.

return Mapper.Map<DictionaryDto>(dict);            
return Mapper.Map<DictionaryDtoFull>(dict);

Does somebody have a better solution for my problem?

2 Answers 2

3

You must add a condition to validate if the collection is initialized to be mapped. You can read here more details: Automapper: Ignore on condition of.

AutoMapper.Mapper.CreateMap<DictionaryEntity, DictionaryDto>()
    .ForMember(dest => dest.DictionaryRecord, opt => opt.PreCondition(source =>
        NHibernateUtil.IsInitialized(source.DictionaryRecord)));
Sign up to request clarification or add additional context in comments.

3 Comments

Altough it's not exactly what I'm looking for, I think this is the best solution for what's possible. Thanks
@Najera are you sure this is correct? As I understand it Condition happens after the mapping and PreCondition should be used. See stackoverflow.com/questions/40658582/… (or maybe it was correct but things have changed with automapper?)
@Adam You are right, all I remember I made a tested and it worked for a couple of projects, but PreCondition is the correct way to do it in last versions. Thanks
0

You could ignore the property, if this is of use?

AutoMapper.Mapper.CreateMap<DictionaryEntity, DictionaryDto>()
    .ForMember(dest => dest.DictionaryRecord,
               opts => opts.Ignore());

http://cpratt.co/using-automapper-mapping-instances/

2 Comments

Thanks for your quick response. I think that's not useful for me. In this case the DictionaryRecord list would just be empty, right?
You're probably right, although you could have two methods one that has the above code and ignores one and one that loads all of the properties lazy style. I'm not sure AutoMapper can do what you're asking.

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.