Пример #1
0
        /// <summary>
        /// Get all foreign key properties for the data entity type.
        /// </summary>
        /// <param name="dataEntity"></param>
        /// <returns></returns>
        private List <PropertyInfo> GetAllForeignKey(object dataEntity)
        {
            // Create a new property collection.
            List <PropertyInfo> foreignKey = new List <PropertyInfo>();

            // For each property member in the current type.
            foreach (PropertyInfo member in dataEntity.GetType().GetProperties())
            {
                // For each attribute on each property
                // in the type.
                foreach (object attribute in member.GetCustomAttributes(true))
                {
                    // If the attribute is the
                    // linq column attribute.
                    if (attribute is Nequeo.Data.Custom.DataColumnForeignKeyAttribute)
                    {
                        // Cast the current attribute.
                        Nequeo.Data.Custom.DataColumnForeignKeyAttribute att =
                            (Nequeo.Data.Custom.DataColumnForeignKeyAttribute)attribute;

                        // If the property attribute
                        // is a foreign key it is
                        // not a reference.
                        if (att.IsReference)
                        {
                            foreignKey.Add(member);
                        }
                    }
                }
            }

            // Return the collection of
            // foreign key properties.
            return(foreignKey);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataObject"></param>
        private void TranslateLazyLoading(object dataObject)
        {
            List <PropertyInfo> propertiesRef  = GetAllForeignKey(dataObject);
            List <PropertyInfo> propertiesData = GetAllColumnData(dataObject);

            foreach (PropertyInfo property in propertiesRef)
            {
                // Get the current foreign key attribute for the property
                Nequeo.Data.Custom.DataColumnForeignKeyAttribute data = GetForeignKeyAttribute(property);

                PropertyInfo propertyInfo = null;
                try
                {
                    propertyInfo = propertiesData.First(p => p.Name.ToLower() == data.ColumnName.ToLower());
                }
                catch { }
                if (propertyInfo != null)
                {
                    // Get the current referenced coulm in the current data object.
                    object value = propertyInfo.GetValue(dataObject, null);

                    // Set the reference type lazy loading data.
                    if (value != null)
                    {
                        // Set the current reference object back to
                        // the parent type
                        property.SetValue(dataObject, SetForeginKeyReferenceData(property, value, data), null);

                        // Get the new refrence type object instance.
                        object newRefObject = property.GetValue(dataObject, null);

                        // Has the current reference column type contain
                        // reference columns, if true then apply recursion
                        List <PropertyInfo> propertiesRefValue = GetAllForeignKey(newRefObject);
                        if (propertiesRefValue.Count > 0)
                        {
                            TranslateLazyLoading(newRefObject);
                        }
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Get the DataColumnForeignKeyAttribute data for the current property information.
        /// </summary>
        /// <param name="property">The current property to examine.</param>
        /// <returns>The data column foreign key attribute data else null if attribute does not exist.</returns>
        private Nequeo.Data.Custom.DataColumnForeignKeyAttribute GetForeignKeyAttribute(PropertyInfo property)
        {
            // For each attribute on each property
            // in the type.
            foreach (object attribute in property.GetCustomAttributes(true))
            {
                // If the attribute is the
                // linq column attribute.
                if (attribute is Nequeo.Data.Custom.DataColumnForeignKeyAttribute)
                {
                    // Cast the current attribute.
                    Nequeo.Data.Custom.DataColumnForeignKeyAttribute att =
                        (Nequeo.Data.Custom.DataColumnForeignKeyAttribute)attribute;
                    return(att);
                }
            }

            // Return null.
            return(null);
        }
Пример #4
0
        /// <summary>
        /// Get the translated foreign key reference data.
        /// </summary>
        /// <param name="property">The current property information</param>
        /// <param name="foreignKeyValue">The foreign key value for the referenced data entity.</param>
        /// <param name="data">The data column foreign key reference attribute data.</param>
        /// <returns>The translated data entity object.</returns>
        private object SetForeginKeyReferenceData(PropertyInfo property, object foreignKeyValue, Nequeo.Data.Custom.DataColumnForeignKeyAttribute data)
        {
            // Get the get method of the property.
            MethodInfo method = property.GetGetMethod();

            string             tableName          = DataTypeConversion.GetSqlConversionDataType(dataContext.ProviderConnectionDataType, data.Name.TrimStart('_').Replace(".", "].["));
            string             colunmName         = DataTypeConversion.GetSqlConversionDataType(dataContext.ProviderConnectionDataType, data.ReferenceColumnName.TrimStart('_'));
            DataTypeConversion dataTypeConversion = new DataTypeConversion(dataContext.ProviderConnectionDataType);

            // Execute the queryable provider and return the constructed
            // sql statement and return the data.
            string    statement = dataTypeConversion.GetSqlStringValue(LinqTypes.GetDataType(data.ColumnType, dataContext.ProviderConnectionDataType), foreignKeyValue);
            DataTable table     = dataContext.ExecuteQuery("SELECT * FROM " + tableName + " WHERE " + colunmName + " = " + statement);

            // Get the anonymous type translator from datarow
            // to the foreign key reference property return type.
            Nequeo.Data.Control.AnonymousTypeFunction typeFunction = new Nequeo.Data.Control.AnonymousTypeFunction();
            return((table.Rows.Count > 0) ? typeFunction.TypeTranslator(table.Rows[0], method.ReturnType) : null);
        }