Exemplo n.º 1
0
        /// <summary>
        /// Converts each property in the object that is to be saved to the database to a parameter to pass to a command
        /// </summary>
        /// <param name="includeId">Value determining whether or not to include the object's unique identifier in the returned array</param>
        /// <returns>Array containing a command parameter for each property in the obect that is to be saved to the database</returns>
        internal virtual sp.ParameterCollection GetParametersForStoredProcedure(Boolean includeId)
        {
            sp.ParameterCollection    parameters = new sp.ParameterCollection();
            DbObjectMetaDataAttribute metaData   = GetType().GetCustomAttribute <DbObjectMetaDataAttribute>();

            if (includeId)
            {
                parameters.AddWithValue(metaData.IdParameterName, metaData.IdParameterDataType, Id);
            }

            foreach (PropertyInfo pi in GetType().GetProperties().Where(p => Attribute.IsDefined(p, typeof(sp.DataColumnAttribute))))
            {
                sp.DataColumnAttribute attribute = pi.GetCustomAttribute <sp.DataColumnAttribute>();
                parameters.AddWithValue(attribute.ColumnName, attribute.DataType, pi.GetValue(this));
            }

            return(parameters);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads the values in the in the data row into the object
        /// </summary>
        /// <param name="conn">Open connection to the database</param>
        /// <param name="row">Data row containing the data for the object</param>
        internal virtual void LoadPropertiesFromDataRow(DbConnection conn, sp.DataRow row)
        {
            Type type = GetType();

            //get the value of the ID property
            DbObjectMetaDataAttribute metaData = type.GetCustomAttribute <DbObjectMetaDataAttribute>();
            PropertyInfo idPi = type.GetProperty("Id");

            idPi.SetValue(this, row[metaData.IdParameterName]);

            //load other properties
            foreach (PropertyInfo pi in type.GetProperties().Where(p => Attribute.IsDefined(p, typeof(sp.DataColumnAttribute))))
            {
                sp.DataColumnAttribute attribute = pi.GetCustomAttribute <sp.DataColumnAttribute>();
                pi.SetValue(this, row[attribute.ColumnName]);
            }

            IsInDatabase = true;
        }