コード例 #1
0
        private static IEnumerable <DirectRelationship> GetDirectRelationships(OneToManyRelationship oneToManyRelationship, ManyToOneRelationship manyToOneRelationship)
        {
            List <DirectRelationship> list = new List <DirectRelationship>(oneToManyRelationship.DirectRelationships);

            list.AddRange(manyToOneRelationship.DirectRelationships);
            return(list);
        }
コード例 #2
0
        // Specialty(DepartmentId)-Department(Id, CollegeId)-College(Id, UniversityId)-University(Id)
        protected static T Create <T>(string value) where T : Relationship
        {
            Relationship relationship;

            if (typeof(T) == typeof(ManyToOneRelationship))
            {
                IEnumerable <ManyToOneDirectRelationship> rels = GenerateDirectRelationships <ManyToOneDirectRelationship>(value);
                relationship = new ManyToOneRelationship(rels);
            }
            else if (typeof(T) == typeof(OneToManyRelationship))
            {
                IEnumerable <OneToManyDirectRelationship> rels = GenerateDirectRelationships <OneToManyDirectRelationship>(value);
                relationship = new OneToManyRelationship(rels);
            }
            else if (typeof(T) == typeof(OneToOneRelationship))
            {
                IEnumerable <OneToOneDirectRelationship> rels = GenerateDirectRelationships <OneToOneDirectRelationship>(value);
                relationship = new OneToOneRelationship(rels);
            }
            else
            {
                IEnumerable <DirectRelationship> rels = GenerateDirectRelationships <DirectRelationship>(value);
                relationship = new Relationship(rels);
            }
            return(relationship as T);
        }
コード例 #3
0
        private static ManyToManyRelationship CreateManyToManyRelationship(XElement relationshipSchema)
        {
            string entity        = relationshipSchema.Attribute(SchemaVocab.Entity).Value;
            string relatedEntity = relationshipSchema.Attribute(SchemaVocab.RelatedEntity).Value;

            XElement oneToManySchema = new XElement(SchemaVocab.Relationship);

            oneToManySchema.SetAttributeValue(SchemaVocab.Type, SchemaVocab.OneToMany);
            oneToManySchema.SetAttributeValue(SchemaVocab.Entity, entity);
            oneToManySchema.SetAttributeValue(SchemaVocab.RelatedEntity, string.Empty);
            oneToManySchema.Add(relationshipSchema.Elements(SchemaVocab.Relationship).Where(x => x.Attribute(SchemaVocab.Type).Value == SchemaVocab.OneToMany));
            OneToManyRelationship oneToManyRelationship = (OneToManyRelationship)CreatePlainRelationship(oneToManySchema);

            oneToManySchema.SetAttributeValue(SchemaVocab.RelatedEntity,
                                              oneToManyRelationship.DirectRelationships[oneToManyRelationship.DirectRelationships.Length - 1].RelatedEntity);

            XElement manyToOneSchema = new XElement(SchemaVocab.Relationship);

            manyToOneSchema.SetAttributeValue(SchemaVocab.Type, SchemaVocab.ManyToOne);
            manyToOneSchema.SetAttributeValue(SchemaVocab.Entity, string.Empty);
            manyToOneSchema.SetAttributeValue(SchemaVocab.RelatedEntity, relatedEntity);
            manyToOneSchema.Add(relationshipSchema.Elements(SchemaVocab.Relationship).Where(x => x.Attribute(SchemaVocab.Type).Value == SchemaVocab.ManyToOne));
            ManyToOneRelationship manyToOneRelationship = (ManyToOneRelationship)CreatePlainRelationship(manyToOneSchema);

            manyToOneSchema.SetAttributeValue(SchemaVocab.Entity, manyToOneRelationship.DirectRelationships[0].Entity);

            ManyToManyRelationship relationship = new ManyToManyRelationship(oneToManyRelationship, manyToOneRelationship);

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

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

            return(relationship);
        }
コード例 #4
0
 public ManyToManyRelationship(OneToManyRelationship oneToManyRelationship, ManyToOneRelationship manyToOneRelationship)
     : base(oneToManyRelationship.Entity, manyToOneRelationship.RelatedEntity, GetDirectRelationships(oneToManyRelationship, manyToOneRelationship))
 {
     OneToManyRelationship = oneToManyRelationship;
     ManyToOneRelationship = manyToOneRelationship;
 }
コード例 #5
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);
        }
コード例 #6
0
 public ManyToOneRelationship(OneToManyRelationship relationship)
     : base(relationship.RelatedEntity, relationship.Entity, ((ManyToOneRelationship)relationship.Reverse()).DirectRelationships)
 {
 }