2013-02-08 176 views
0

所以有具有模型对象TreeNode数据映射邻接表模型AutoMapper

Public Class TreeNode{ 
    Public int NodeId {get;set;} 
    Public String Name {get;set;} 
    Public int ParentId {get;set;} 
    Public TreeNode Parent {get;set;} 
    Public List<TreeNode> Children {get;set;} 
} 

该结构是通过使用一个Adjacency List Pattern数据库供电。我使用的WCF服务与AutoMapper填充我的模型类。

我想要做这样的事情:

public static void ConfigureMappings() 
{ 
    Mapper.CreateMap<TreeNodeDto, Taxonomy>() 
    .AfterMap((s, d) => 
    { 
    //WCF service calls to get parent and children 
    d.Children = Mapper.Map<TreeNodeDto[], TreeNode[]>(client.GetTreeChildren(s)).ToList(); 
    d.Parent = Mapper.Map<TreeNodeDto, TreeNode>(client.GetTreeParent(s)); 
    }); 
} 

但很明显,这将导致一个无限循环(如果我只图孩子寿它的工作)。有什么方法可以使用AutoMapper填充我的树结构吗?

回答

0

我发现这个部分解决方案。起初我虽然这是我正在寻找,但进一步检查后,它只适用于如果你开始在树的顶部。如果您从中间开始,它不填充父节点。

How to assign parent reference to a property in a child with AutoMapper

public static void ConfigureMappings() 
{ 
    Mapper.CreateMap<TreeNodeDto, Taxonomy>() 
    .AfterMap((s, d) => 
    { 
    //WCF service calls to get parent and children 
    d.Children = Mapper.Map<TreeNodeDto[], TreeNode[]>(client.GetTreeChildren(s)).ToList(); 
    foreach(var child in d.Children) 
    { 
     child.Parent = d; 
    } 
} 
+0

嘛。经过进一步检查,我意识到这种解决方案只适用于从树顶开始的工作。 – NSjonas 2013-02-08 19:33:07