Beispiel #1
0
 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)
     };
 }
Beispiel #2
0
        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];
        }
Beispiel #3
0
 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 ();
     }
 }