Пример #1
0
        private static PlainRelationship Reverse(PlainRelationship relationship)
        {
            string entity        = relationship.RelatedEntity;
            string relatedEntity = relationship.Entity;
            List <DirectRelationship> relationships = new List <DirectRelationship>();

            for (int i = relationship.DirectRelationships.Length - 1; i >= 0; i--)
            {
                relationships.Add(relationship.DirectRelationships[i].Reverse());
            }

            if (relationship is ManyToOneRelationship)
            {
                return(new OneToManyRelationship(entity, relatedEntity, relationships));
            }
            else if (relationship is OneToManyRelationship)
            {
                return(new ManyToOneRelationship(entity, relatedEntity, relationships));
            }
            else if (relationship is OneToOneRelationship)
            {
                return(new OneToOneRelationship(entity, relatedEntity, relationships));
            }
            else
            {
                throw new NotSupportedException(relationship.GetType().ToString()); // never
            }
        }
Пример #2
0
        private static XElement PlainRelationshipToXml(PlainRelationship relationship)
        {
            XElement relationshipSchema = new XElement(SchemaVocab.Relationship);

            relationshipSchema.SetAttributeValue(SchemaVocab.Name, relationship.Name);
            string type;

            if (relationship is ManyToOneRelationship)
            {
                type = SchemaVocab.ManyToOne;
            }
            else if (relationship is OneToManyRelationship)
            {
                type = SchemaVocab.OneToMany;
            }
            else if (relationship is OneToOneRelationship)
            {
                type = SchemaVocab.OneToOne;
            }
            else
            {
                throw new NotSupportedException(relationship.GetType().ToString()); // never
            }
            relationshipSchema.SetAttributeValue(SchemaVocab.Type, type);
            relationshipSchema.SetAttributeValue(SchemaVocab.Entity, relationship.Entity);
            relationshipSchema.SetAttributeValue(SchemaVocab.RelatedEntity, relationship.RelatedEntity);
            if (relationship.DirectRelationships.Length == 1)
            {
                DirectRelationship rel = relationship.DirectRelationships[0];
                for (int i = 0; i < rel.Properties.Length; i++)
                {
                    XElement xProperty = new XElement(SchemaVocab.Property);
                    xProperty.SetAttributeValue(SchemaVocab.Name, rel.Properties[i]);
                    xProperty.SetAttributeValue(SchemaVocab.RelatedProperty, rel.RelatedProperties[i]);
                    relationshipSchema.Add(xProperty);
                }
            }
            else
            {
                foreach (DirectRelationship rel in relationship.DirectRelationships)
                {
                    XElement relSchema = new XElement(SchemaVocab.Relationship);
                    string   relType;
                    if (rel is ManyToOneDirectRelationship)
                    {
                        relType = SchemaVocab.ManyToOne;
                    }
                    else if (rel is OneToManyDirectRelationship)
                    {
                        relType = SchemaVocab.OneToMany;
                    }
                    else if (rel is OneToOneDirectRelationship)
                    {
                        relType = SchemaVocab.OneToOne;
                    }
                    else
                    {
                        throw new NotSupportedException(rel.GetType().ToString()); // never
                    }
                    relSchema.SetAttributeValue(SchemaVocab.Type, relType);
                    relSchema.SetAttributeValue(SchemaVocab.Entity, rel.Entity);
                    relSchema.SetAttributeValue(SchemaVocab.RelatedEntity, rel.RelatedEntity);
                    for (int i = 0; i < rel.Properties.Length; i++)
                    {
                        XElement xProperty = new XElement(SchemaVocab.Property);
                        xProperty.SetAttributeValue(SchemaVocab.Name, rel.Properties[i]);
                        xProperty.SetAttributeValue(SchemaVocab.RelatedProperty, rel.RelatedProperties[i]);
                        relSchema.Add(xProperty);
                    }
                    relationshipSchema.Add(relSchema);
                }
            }
            return(relationshipSchema);
        }