本文介绍了具有递归映射的ThisMember复合对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我有以下对象:

I have a question, I have a following object:

public class Category {
    public long Id { get; set; }
    public string Title { get; set; }
    public int MaxDepth { get; set; }

    public virtual Category Parent { get; set; }
    public virtual IList<Category> ChildrenNodes { get; set; }
}

我正在使用NHibernate来检索此数据,因为我的列表是 LazyLoading
应该重新映射到 CategoryDTO btw:也供内部使用。

I'm using NHibernate to retrieve the data for this, because my list is LazyLoading,it should be remapped into CategoryDTO btw: for internal uses too.

这就是DTO的样子... (例如,我并未将所有内容放入其中)

This is how to the DTO looks like... (for the example I didn't put everything inside)

public class CategoryDTO {
    public long Id { get; set; }
    public string Title { get; set; }
    public virtual CategoryDTO Parent { get; set; }
    public IList<CategoryDTO> ChildrenNodes { get; set; }
}

我正在使用 这是它的参考。

I'm using ThisMember here is the reference on it.

顺便说一句,感谢朱利安(Julian)出色的工具。

我正在使用此扩展方法,

I'm using it with this extension method that I wrote.

 public static TOut Map<TIn, TOut>(this TIn source)
        where TIn : class
        where TOut : new() {
        return Mapper.Map<TIn, TOut>(source);
 }

现在是一个问题:

如何使用递归映射复合对象?
在朱利安(Julian)示例中我还没有找到答案。

How to Map a Composite object with recursion?I haven't found an answer for this within Julian examples.

任何帮助将不胜感激。

您可以建议其他映射器,但它必须是 ThisMember 速度相同或更快的速度。

You can suggest other mappers but it must be ThisMember speed equivalent or faster.

请先谢谢。

推荐答案

这应该相当简单:

var catDTO = new CategoryDTO {
  Title = "Parent",
  ChildrenNodes = new List<CategoryDTO> { new CategoryDTO { Title = "Child" }}
};

var mapper = new MemberMapper();
mapper.CreateMap<CategoryDTO, Category>(category => new Category {
  ChildrenNodes =
    category.ChildrenNodes == null ? null :
      category.ChildrenNodes.Select(c => mapper.Map<CategoryDTO, Category>(c)).ToList()
});

var mappedCategory = mapper.Map<CategoryDTO, Category>(catDTO);

这篇关于具有递归映射的ThisMember复合对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:05