private Dictionary <long, PredefinedRelationType> GetPredefinedRelationTypes(Entity entity) { Type entityType = Entity.GetEntityType(entity); if (!predefinedRelationTypes.ContainsKey(entityType)) { predefinedRelationTypes[entityType] = new Dictionary <long, PredefinedRelationType> (); // Find all the properties that are marked as predefined relations. foreach (PropertyInfo property in entityType.GetProperties()) { object[] attributes = property.GetCustomAttributes(typeof(PredefinedRelationAttribute), false); if (attributes.Any()) { // The property represents predefined relation, take just the first attribute. PredefinedRelationAttribute attribute = (PredefinedRelationAttribute)attributes[0]; long relationTypeId = (long)attribute.RelationTypeValue; // We know that the attribute is applied on ICollection<TEntity>. Type objectiveEntityType = property.PropertyType.GetGenericArguments()[0]; predefinedRelationTypes[entityType][relationTypeId] = new PredefinedRelationType(property) { Id = relationTypeId, Name = attribute.RelationTypeValue.ToString(), SubjectiveEntityType = entityType, ObjectiveEntityType = objectiveEntityType }; } } } return(predefinedRelationTypes[entityType]); }
/// <summary> /// Deletes predefined or generic relation. /// </summary> /// <param name="relationTypeId">Id of the relation type.</param> /// <param name="entity">The entity whose relation should be removed</param> /// <param name="relationId"> /// Id of the relation in case of generic relation type. Id of the related entity if the relation is predefined. /// </param> public void DeleteRelation(long relationTypeId, Entity entity, long relationId) { RelationType relationType = GetRelationType(relationTypeId, entity); if (relationType == null) { return; } PredefinedRelationType predefinedRelationType = relationType as PredefinedRelationType; if (predefinedRelationType != null) { IEnumerable <Entity> relatedEntities = predefinedRelationType.GetRelatedEntities(entity); long relatedEntityId = relationId; Entity relatedEntity = relatedEntities.FirstOrDefault(e => e.Id == relatedEntityId); if (relatedEntity != null) { // Even though the type of objectiveEntities is IEnumerable<Entity>, it is sure that it is also // ICollection<relationType.ObjectiveEntityType> so the Remove method is surely found. MethodInfo remove = relatedEntities.GetType().GetMethod("Remove"); remove.Invoke(relatedEntities, new object[] { relatedEntity }); Db.SaveChanges(); } } else { Relation relation = GetGenericRelation(relationType, relationId); if (relation != null) { Db.Relations.Remove(relation); Db.SaveChanges(); } } }
public void AddRelation(long relationTypeId, Entity entity, long relatedEntityId) { RelationType relationType = GetRelationType(relationTypeId, entity); if (relationType == null) { return; } Entity relatedEntity = RemoteFacade.Instance.GetEntityModel(relationType.ObjectiveEntityType, Db).GetEntityById(relatedEntityId); if (relatedEntity == null) { return; } PredefinedRelationType predefinedRelationType = relationType as PredefinedRelationType; if (predefinedRelationType != null) { AddPredefinedRelation(predefinedRelationType, entity, relatedEntity); } else { AddGenericRelation(relationType, entity, relatedEntity); } }
private TypedRelations GetPredefinedRelations(PredefinedRelationType relationType, Entity entity) { IEnumerable <Entity> relatedEntities = relationType.GetRelatedEntities(entity).Where(e => !e.IsDeleted); return(new TypedRelations(relationType) { Relations = relatedEntities.ToDictionary <Entity, Relation, Entity> (e => new Relation(), e => e) }); }
private void AddPredefinedRelation(PredefinedRelationType relationType, Entity entity, Entity relatedEntity) { IEnumerable <Entity> relatedEntities = relationType.GetRelatedEntities(entity); if (!relatedEntities.Contains(relatedEntity)) { // Even though the type of objectiveEntities is IEnumerable<Entity>, it is sure that it is also // ICollection<relationType.ObjectiveEntityType> so the Add method is surely found. MethodInfo add = relatedEntities.GetType().GetMethod("Add"); add.Invoke(relatedEntities, new object[] { relatedEntity }); Db.SaveChanges(); } }
public TypedRelations GetRelations(long relationTypeId, Entity entity) { RelationType relationType = GetRelationType(relationTypeId, entity); if (relationType == null) { return(null); } PredefinedRelationType predefinedRelationType = relationType as PredefinedRelationType; if (predefinedRelationType != null) { return(GetPredefinedRelations(predefinedRelationType, entity)); } return(GetGenericRelations(relationType, entity)); }
public IEnumerable <Entity> GetRelatableEntities(long relationTypeId, Entity entity, string needle) { RelationType relationType = GetRelationType(relationTypeId, entity); if (relationType == null) { return(Enumerable.Empty <Entity> ()); } // Reflexive relations aren't supported so the entity id is initially added to the unrelatable entity ids. List <long> unrelatableEntityIds = new List <long> (new long[] { entity.Id }); PredefinedRelationType predefinedRelationType = relationType as PredefinedRelationType; if (predefinedRelationType != null) { unrelatableEntityIds.AddRange(predefinedRelationType.GetRelatedEntities(entity).Select <Entity, long> (e => e.Id)); } // Return the entities that are not unrelatable. IEntityModel entityModel = RemoteFacade.Instance.GetEntityModel(relationType.ObjectiveEntityType, Db); return(entityModel.GetEntitiesByIdsAndNeedle(unrelatableEntityIds, needle)); }