Esempio n. 1
0
        protected internal IEnumerable <object> GetRelationObjects(QuerySpec filter, TableSchema.Relation parentRelation, object parentObject)
        {
            var relations = Schema.BuildPreloadRelationSet();

            var objects = from o in DataProvider.GetObjects(filter.Native, Schema)
                          select Ir.WithLoadedRelations(Schema.UpdateObject(Activator.CreateInstance(Schema.ObjectType), o), relations);

            if (parentRelation?.ReverseRelation != null)
            {
                objects = from o in objects select parentRelation.ReverseRelation.SetField(o, parentObject);
            }

            if (filter.Code != null)
            {
                objects = from o in objects where filter.Code.IsFilterMatch(o) select o;
            }

            objects = objects.Select(o =>
            {
                Fire_ObjectRead(o);

                return(o);
            });

            return(objects);
        }
Esempio n. 2
0
        internal T Load(T obj, object key, params Expression <Func <T, object> >[] relationsToLoad)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            Dictionary <string, object> primaryKey;

            var keyTypeInspector = key.GetType().Inspector();

            if (keyTypeInspector.Is(TypeFlags.Numeric | TypeFlags.Enum | TypeFlags.String))
            {
                if (Schema.PrimaryKeys.Length != 1)
                {
                    throw new Exception($"Invalid key for {typeof (T)}");
                }

                primaryKey = new Dictionary <string, object>()
                {
                    { Schema.PrimaryKeys[0].MappedName, key }
                };
            }
            else
            {
                primaryKey = Schema.PrimaryKeys.ToDictionary(pk => pk.MappedName, pk => GetFieldOrPropertyValue(key, pk.FieldName));
            }

            var serializedEntity = DataProvider.ReadObject(primaryKey, Schema);

            if (serializedEntity == null)
            {
                return(default(T));
            }

            Schema.UpdateObject(obj, serializedEntity);

            var relations = LambdaRelationFinder.FindRelations(relationsToLoad, Schema);

            if (Schema.DatasetRelations != null)
            {
                if (relations == null)
                {
                    relations = new HashSet <TableSchema.Relation>(Schema.DatasetRelations);
                }
                else
                {
                    relations.UnionWith(Schema.DatasetRelations);
                }
            }

            return(Ir.WithLoadedRelations(obj, relations));
        }
Esempio n. 3
0
            internal object LoadRelation(object parentObject)
            {
                var localFieldValue = LocalField.GetField(parentObject);

                var foreignRepository = ForeignSchema.Repository;

                if (IsToOne)
                {
                    var serializedForeignObject = localFieldValue == null ? null : foreignRepository.DataProvider.ReadObject(new Dictionary <string, object> {
                        { ForeignField.MappedName, localFieldValue }
                    }, ForeignSchema);

                    var relationObject = serializedForeignObject != null?Ir.WithLoadedRelations(ForeignSchema.UpdateObject(Activator.CreateInstance(FieldType), serializedForeignObject), ForeignSchema.DatasetRelations) : null;

                    if (ReverseRelation != null)
                    {
                        relationObject = ReverseRelation.SetField(relationObject, parentObject);
                    }

                    return(relationObject);
                }

                if (RelationType == RelationType.OneToMany)
                {
                    var parameter = Expression.Parameter(ElementType, "x");

                    Expression exp1 = Expression.MakeMemberAccess(parameter, ForeignField.FieldInfo);
                    Expression exp2 = Expression.Constant(localFieldValue, LocalField.FieldType);

                    if (exp2.Type != exp1.Type)
                    {
                        exp2 = Expression.Convert(exp2, exp1.Type);
                    }

                    var filter = new FilterSpec(Expression.Lambda(Expression.Equal(exp1, exp2), parameter));

                    if (IsDataSet)
                    {
                        return(Activator.CreateInstance(typeof(DataSet <>).MakeGenericType(ElementType), ForeignSchema.Repository, filter, this, parentObject));
                    }
                    else
                    {
                        return(CreateCollection(foreignRepository.GetRelationObjects(foreignRepository.CreateQuerySpec(filter), this, parentObject)));
                    }
                }

                return(null);
            }
Esempio n. 4
0
 public object ObjectWithRelations(object o)
 {
     return(Ir.WithLoadedRelations(o, Relations));
 }