2

我一直在尝试编写一些通用代码来创建 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);
                    }
                }
4

1 回答 1

2

首先要意识到 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];

布雷特

于 2011-05-27T14:28:06.507 回答