Beispiel #1
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);
            }