示例#1
0
        public DirectRelationship Reverse()
        {
            DirectRelationship relationship = this;

            if (relationship is ManyToOneDirectRelationship)
            {
                return(new OneToManyDirectRelationship(relationship as ManyToOneDirectRelationship));
            }
            else if (relationship is OneToManyDirectRelationship)
            {
                return(new ManyToOneDirectRelationship(relationship as OneToManyDirectRelationship));
            }
            else if (relationship is OneToOneDirectRelationship)
            {
                return(new OneToOneDirectRelationship(relationship.RelatedEntity, relationship.Entity,
                                                      relationship.RelatedProperties.ToArray(), relationship.Properties.ToArray()));
            }
            else
            {
                throw new NotSupportedException(relationship.GetType().ToString()); // never
            }
        }
示例#2
0
        private static PlainRelationship CreatePlainRelationship(XElement relationshipSchema)
        {
            PlainRelationship relationship;

            if (relationshipSchema.Elements(SchemaVocab.Relationship).Any())
            {
                string type          = relationshipSchema.Attribute(SchemaVocab.Type).Value;
                string entity        = relationshipSchema.Attribute(SchemaVocab.Entity).Value;
                string relatedEntity = relationshipSchema.Attribute(SchemaVocab.RelatedEntity).Value;

                List <DirectRelationship> relList = new List <DirectRelationship>();
                foreach (XElement xDirectRelationship in relationshipSchema.Elements(SchemaVocab.Relationship))
                {
                    DirectRelationship rel = DirectRelationship.Create(xDirectRelationship, type);
                    relList.Add(rel);
                }
                IEnumerable <DirectRelationship> rels = DirectRelationship.Connect(relList);

                switch (type)
                {
                case SchemaVocab.ManyToOne:
                    relationship = new ManyToOneRelationship(entity, relatedEntity, rels);
                    break;

                case SchemaVocab.OneToMany:
                    relationship = new OneToManyRelationship(entity, relatedEntity, rels);
                    break;

                case SchemaVocab.OneToOne:
                    relationship = new OneToOneRelationship(entity, relatedEntity, rels);
                    break;

                default:
                    throw new NotSupportedException(type);
                }
            }
            else
            {
                DirectRelationship        rel     = DirectRelationship.Create(relationshipSchema);
                List <DirectRelationship> relList = new List <DirectRelationship>()
                {
                    rel
                };
                if (rel is ManyToOneDirectRelationship)
                {
                    relationship = new ManyToOneRelationship(rel.Entity, rel.RelatedEntity, relList);
                }
                else if (rel is OneToManyDirectRelationship)
                {
                    relationship = new OneToManyRelationship(rel.Entity, rel.RelatedEntity, relList);
                }
                else if (rel is OneToOneDirectRelationship)
                {
                    relationship = new OneToOneRelationship(rel.Entity, rel.RelatedEntity, relList);
                }
                else
                {
                    throw new NotSupportedException(rel.GetType().ToString()); // never
                }
            }

            XAttribute attr = relationshipSchema.Attribute(SchemaVocab.Name);

            if (attr != null)
            {
                relationship.Name = attr.Value;
            }

            return(relationship);
        }