Ejemplo n.º 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;
                }
            }
        }
Ejemplo n.º 2
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));
 }