我一直在尝试编写一些通用代码来创建 Habanero 业务对象的 xml 包。该代码目前可以处理组合关系,但我需要手动添加关联关系。有什么方法可以以更通用的方式添加没有复合反向关系的关联关系。
组合关系是这样加起来的
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);
}
}
首先要意识到的是1)Habanero 不需要你定义反向关系。尽管如果您从 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];
布雷特
我是一名优秀的程序员,十分优秀!