2013-02-08 112 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