internal static TypeRelationship GetRelationship(Type localInnerType, Type localType, Type foreignType)
        {
            var foreignRelationships = DataFunctions.GetRelatedProperties(foreignType);

            foreach (var fR in foreignRelationships)
            {
                if (fR.Item1.DeclaringType == foreignType)
                {
                    var localRelationships = DataFunctions.GetRelatedProperties(localType);
                    foreach (var lR in localRelationships)
                    {
                        if (lR.Item2.RelationshipName == fR.Item2.RelationshipName)
                        {
                            // we found a matching relationship
                            // TODO: Should also check type of relationship... eg, MANY_TO_ONE should match to ONE_TO_MANY, etc
                            return(new TypeRelationship()
                            {
                                LocalProperty = lR.Item1,
                                LocalRelationship = lR.Item2,
                                ForeignProperty = fR.Item1,
                                ForeignRelationship = fR.Item2
                            });
                        }
                    }
                }
            }

            throw new Exception("Cannot find data relationship.");
        }
示例#2
0
        /*
         * [Obsolete("Do not use the standard 'Add' method with a RelatedDataList, as it will not auto-populate the item's relationship field.", true)]
         * public new void Add(T item)
         * {
         *  base.Add(item);
         * }
         */

        public void AddAndSave(T item, DataItem parent)
        {
            var relationship = DataFunctions.GetRelationship(this, parent.GetType(), typeof(T));

            relationship.ForeignProperty.SetValue(item, relationship.LocalProperty.GetValue(parent));
            item.Save();
            base.Add(item);
        }
示例#3
0
 public void Delete()
 {
     // TODO: We shouldn't be deleting data!
     if (!_newRecord)
     {
         DataFunctions.DeleteData(this);
     }
 }
示例#4
0
        public void Save(DataItem parent)
        {
            // TODO: Currently this will save all rows, which is a bit wasteful. DataItem should
            //       be capable of determining if a save is necessary, and only save when required.
            var relationship = DataFunctions.GetRelationship(this, parent.GetType(), typeof(T));

            foreach (T item in this)
            {
                relationship.ForeignProperty.SetValue(item, relationship.LocalProperty.GetValue(parent));
                item.Save();
            }
        }
示例#5
0
        public DataItem(object Id)
        {
            var property = DataFunctions.GetPrimaryKey(this);

            if (property != null)
            {
                SqlFieldName sField = DataFunctions.GetPropertySqlFieldName(property);
                //DataFunctions.GetRecord<>
                object item = typeof(DataFunctions)
                              .GetMethod("GetRecord")
                              .MakeGenericMethod(this.GetType())
                              .Invoke(null, new object[] { Id });

                if (item == null)
                {
                    throw new Exception("Item cannot be found.");
                }

                DataFunctions.ShallowCopy(item, this);

                this._newRecord = false;
            }

            // look for relationships
            var relationships = DataFunctions.GetRelationshipDataLists(this.GetType());

            foreach (var r in relationships)
            {
                DataFunctions.TypeRelationship tR = DataFunctions.GetRelationship(r.PropertyType, this.GetType(), r.PropertyType.GetGenericArguments()[0]);

                if (tR.ForeignRelationship.Relationship == SqlRelatedTable.DataRelationship.MANY_TO_ONE && tR.LocalRelationship.Relationship == SqlRelatedTable.DataRelationship.ONE_TO_MANY)
                {
                    // we found the related property
                    SqlFieldName fieldName = DataFunctions.GetPropertySqlFieldName(tR.ForeignProperty);

                    // construct our generic type
                    List <object> retList = DataFunctions.GetData(r.PropertyType.GetGenericArguments()[0],
                                                                  0,
                                                                  0,
                                                                  "WHERE " + fieldName.Name + " = @fieldid", "",
                                                                  new List <SqlParameter>()
                    {
                        new SqlParameter("@fieldid", tR.LocalProperty.GetValue(this))
                    });

                    var converted = DataFunctions.ConvertList(retList, r.PropertyType);
                    r.SetValue(this, converted);
                    break;
                }
            }
        }
示例#6
0
        public void Save()
        {
            if (_newRecord)
            {
                DataFunctions.InsertData(this);
                _newRecord = false;
            }
            else
            {
                DataFunctions.UpdateData(this);
            }

            foreach (var dataListProperty in DataFunctions.GetRelationshipDataLists(this.GetType()))
            {
                dynamic dataList = dataListProperty.GetValue(this);
                if (dataList != null)
                {
                    dataList.Save(this);
                }
            }

            //getStoredValues();
        }
示例#7
0
 public static List <T> GetItems <T>(int numRecords, int pageNumber, string whereClause = "", List <SqlParameter> sqlParameters = null, string orderBy = "ORDER BY ??", string overrideTablename = "", bool distinct = false, Dictionary <string, string> selectOverrides = null) where T : DataItem, new()
 {
     return(DataFunctions.GetData <T>(numRecords, pageNumber, whereClause, overrideTablename, sqlParameters, orderBy, distinct, selectOverrides));
 }
示例#8
0
 public static T GetItem <T>(object id) where T : DataItem, new()
 {
     return(DataFunctions.GetRecord <T>(id));
 }