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