Ejemplo n.º 1
0
        private TypedRelations GetGenericRelations(RelationType relationType, Entity entity)
        {
            Expression <Func <Relation, bool> > relationSelector = r => r.SubjectiveEntityId == entity.Id;

            if (relationType.IsSymmetric)
            {
                relationSelector = r => r.SubjectiveEntityId == entity.Id || r.ObjectiveEntityId == entity.Id;
            }
            else if (relationType.IsInverse)
            {
                relationSelector = r => r.ObjectiveEntityId == entity.Id;
            }

            // The relationType.Id may be nagative in case of inverse relations, but it is positive in the database.
            IQueryable <Relation> relationsQuery = Db.Relations.Where(r => r.RelationTypeId == Math.Abs(relationType.Id));

            relationsQuery = relationsQuery.Where(relationSelector);

            // Retrieve all the relations and store them into the helper structure.
            Dictionary <long, List <Relation> > relationsByRelatedEntityIds = new Dictionary <long, List <Relation> > ();

            foreach (Relation relation in relationsQuery)
            {
                long relatedEntityId = relation.SubjectiveEntityId == entity.Id ? relation.ObjectiveEntityId : relation.SubjectiveEntityId;
                if (!relationsByRelatedEntityIds.ContainsKey(relatedEntityId))
                {
                    relationsByRelatedEntityIds[relatedEntityId] = new List <Relation> ();
                }
                relationsByRelatedEntityIds[relatedEntityId].Add(relation);
            }

            TypedRelations genericRelations = new TypedRelations(relationType);
            IEntityModel   entityModel      = RemoteFacade.Instance.GetEntityModel(relationType.ObjectiveEntityType, Db);

            foreach (Entity relatedEntity in entityModel.GetEntitiesByIds(relationsByRelatedEntityIds.Keys))
            {
                foreach (Relation relation in relationsByRelatedEntityIds[relatedEntity.Id])
                {
                    genericRelations.Relations[relation] = relatedEntity;
                }
            }
            return(genericRelations);
        }
Ejemplo n.º 2
0
        private TypedRelations GetGenericRelations(RelationType relationType, Entity entity)
        {
            Expression<Func<Relation, bool>> relationSelector = r => r.SubjectiveEntityId == entity.Id;
            if (relationType.IsSymmetric)
            {
                relationSelector = r => r.SubjectiveEntityId == entity.Id || r.ObjectiveEntityId == entity.Id;
            }
            else if (relationType.IsInverse)
            {
                relationSelector = r => r.ObjectiveEntityId == entity.Id;
            }

            // The relationType.Id may be nagative in case of inverse relations, but it is positive in the database.
            IQueryable<Relation> relationsQuery = Db.Relations.Where (r => r.RelationTypeId == Math.Abs (relationType.Id));
            relationsQuery = relationsQuery.Where (relationSelector);

            // Retrieve all the relations and store them into the helper structure.
            Dictionary<long, List<Relation>> relationsByRelatedEntityIds = new Dictionary<long, List<Relation>> ();
            foreach (Relation relation in relationsQuery)
            {
                long relatedEntityId = relation.SubjectiveEntityId == entity.Id ? relation.ObjectiveEntityId : relation.SubjectiveEntityId;
                if (!relationsByRelatedEntityIds.ContainsKey (relatedEntityId))
                {
                    relationsByRelatedEntityIds[relatedEntityId] = new List<Relation> ();
                }
                relationsByRelatedEntityIds[relatedEntityId].Add (relation);
            }

            TypedRelations genericRelations = new TypedRelations (relationType);
            IEntityModel entityModel = RemoteFacade.Instance.GetEntityModel (relationType.ObjectiveEntityType, Db);
            foreach (Entity relatedEntity in entityModel.GetEntitiesByIds (relationsByRelatedEntityIds.Keys))
            {
                foreach (Relation relation in relationsByRelatedEntityIds[relatedEntity.Id])
                {
                    genericRelations.Relations[relation] = relatedEntity;
                }
            }
            return genericRelations;
        }