Пример #1
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);
        }
Пример #2
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();
            }
        }
Пример #3
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;
                }
            }
        }