2011-05-27 50 views
2

我一直在嘗試編寫一些通用代碼來創建Habanero業務對象的xml包。該代碼當前可以處理組合關係,但我需要手動添加關聯關係。有沒有什麼方法可以通過更通用的方式添加沒有複合反向關係的關聯關係。Habanero中的關係

這是組合關係是如何加入

private static void AddRelatedCompositionObjects(Package package, IBusinessObject businessObject) 
    { 
     businessObject.Relationships 
      .Where(rel => rel.RelationshipType == RelationshipType.Composition) 
      .Where(rel => rel is IMultipleRelationship) 
      .Select(rel => (IMultipleRelationship)rel) 
      .ForEach(rel => rel.BusinessObjectCollection 
           .AsEnumerable<IBusinessObject>() 
           //.ForEach(package.Add)); 
           .ForEach(bo => BuildPackage(package, bo))); 

     businessObject.Relationships 
      .Where(rel => rel.RelationshipType == RelationshipType.Composition) 
      .Where(rel => rel is ISingleRelationship) 
      .Select(rel => (ISingleRelationship)rel) 
      //.ForEach(rel => package.Add(rel.GetRelatedObject())); 
      .ForEach(rel => BuildPackage(package, rel.GetRelatedObject())); 
    } 

然後我手動添加關聯關係

   var package = new Package(); 
       foreach (var returnDelivery in returnDeliveries) 
       { 
        package.Add(returnDelivery); 
        if (returnDelivery != null) 
        { 
         var materials = returnDelivery.DeliveryItems.Select(item => item.Material).Distinct(); 
         materials.ToList().ForEach(material => 
         { 
          package.Add(material); 
          material.EWTMaterials.ForEach(package.Add); 
         }); 
         package.Add(returnDelivery.Customer); 
        } 
       } 

回答

2

實現第一件事是 1)哈瓦那並不需要你有定義了一個反向關係。雖然如果您從Firestarter生成類定義,您將擁有一個。

我已經從Habanero.BO的ClassDefValidator被盜此示例代碼段,因此可能不是你想要什麼,並理所當然地可以概括爲架構。 這段代碼剪斷確實是得到一個relationshipDef

這個代碼反向relationshipDef是Habanero.BO.ClassDefValidator

CheckRelationshipsForAClassDef方法,如果你看看這裏你會看到代碼來獲取relatedClassDef。把它轉換成你需要的東西應該很容易。

如果您有任何問題,然後給我留言。

 if (!HasReverseRelationship(relationshipDef)) return; 

     string reverseRelationshipName = relationshipDef.ReverseRelationshipName; 
     if (!relatedClassDef.RelationshipDefCol.Contains(reverseRelationshipName)) 
     { 
      throw new InvalidXmlDefinitionException 
       (string.Format 
        ("The relationship '{0}' could not be loaded for because the reverse relationship '{1}' defined for class '{2}' is not defined as a relationship for class '{2}'. Please check your ClassDefs.xml or fix in Firestarter.", 
         relationshipDef.RelationshipName, reverseRelationshipName, relatedClassDef.ClassNameFull)); 
     } 

     var reverseRelationshipDef = relatedClassDef.RelationshipDefCol[reverseRelationshipName]; 

佈雷特

+0

感謝@GloryDe​​v,我會檢查出代碼這個週末,看看它的伎倆。 – Andrew 2011-05-27 14:34:14