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; }
}
我正在使用Automapper和NHibernate。在NHibernate中,DictionaryRecord属性被标记为延迟加载。
当我从DictionaryEntity映射到DictionaryDto时,Automapper会加载所有的DictionaryRecords。
但我不想要这种行为,有没有一种方式可以配置Automapper以在我真正访问该属性之前不解析此属性。
我针对这种情况的解决方法是将DictionaryEntity拆分为两个类并创建第二个Automapper映射。
public class DictionaryDto
{
public string DictionaryName { get; set; }
}
public class DictionaryDtoFull : DictionaryDto
{
public IList<DictionaryRecordEntity> DictionaryRecord { get; set; }
}
然后在代码中,根据需要调用AutoMapper.Map方法。
return Mapper.Map<DictionaryDto>(dict);
return Mapper.Map<DictionaryDtoFull>(dict);
有没有更好的解决方案来解决我的问题?
PreCondition
是最新版本中正确的方法。谢谢。 - Najera