Andrew
Andrew

Reputation: 5277

Relationships in Habanero

I have been trying to write some generic code to create an xml package of Habanero business objects. The code can currently handle compostion relationships, but I need to add the Association relationships manually. Is there any way to add association relationsips that don't have a composite reverse relationship in a more generic way.

This is how the composition relationships are added

    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()));
    }

And then I manually add the association relationships

                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);
                    }
                }

Upvotes: 2

Views: 250

Answers (1)

GloryDev
GloryDev

Reputation: 680

First thing to realise is that 1) Habanero does not require you to have a reverse relationship defined. Although if you are generating your class definitions from Firestarter you will have one.

I have stolen this sample snippet from the ClassDefValidator in Habanero.BO so it might not be exactly what you want and could certainly be generalised into the architecture. What this code snipped does is get the reverse relationshipDef for a relationshipDef

this code is in Habanero.BO.ClassDefValidator

CheckRelationshipsForAClassDef method if you look here you will see code to get the relatedClassDef. It should be pretty easy to convert this into something you need.

If you have any problems then give me a shout.

        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];

Brett

Upvotes: 2

Related Questions