Пример #1
0
        public static void FetchREFEntity(object objEntity, REFAttribute refAtt, DataTable table)
        {
            int rindex = -1;//根据objEntity的Id属性,在table中找到对应的行数

            for (int i = 0; i < table.Rows.Count; i++)
            {
                if (((BaseEntity)objEntity).Id == Convert.ToInt64(table.Rows[i]["E_" + objEntity.GetType().Name + "Id"]))
                {
                    rindex = i;
                    break;
                }
            }
            //对象的该引用属性为空值
            if (table.Rows[rindex]["E_" + refAtt.REFEntityName + "Id"] == DBNull.Value)
            {
                return;
            }

            object objRef = DLLAnalysis.GetEntityInstance(refAtt.REFEntityName);

            ((BaseEntity)objRef).Id = Convert.ToInt64(table.Rows[rindex]["E_" + refAtt.REFEntityName + "Id"]);
            EntityInfo refeinfo = DLLAnalysis.GetEntityInfoByType(objRef.GetType());

            foreach (FieldAttribute fieldAtt in refeinfo.Properties)
            {
                FetchPropertyValue(objRef, fieldAtt, table, rindex);
            }
            objEntity.GetType().GetProperty(refAtt.PropertyName).SetValue(objEntity, objRef, null);
        }
Пример #2
0
        /// <summary>
        /// 根据从数据库检索出的数据集从中组装出实体对象实例
        /// </summary>
        /// <param name="einfo">实体对象媒数据</param>
        /// <param name="fetchType">关联对象加载方式</param>
        /// <param name="table">数据集</param>
        /// <param name="rowIndex">实体对象所处的数据集行数</param>
        /// <returns>实体对象实例</returns>
        public static BaseEntity FetchEntity(EntityInfo einfo, Fetch fetchType, DataTable table, int rowIndex)
        {
            BaseEntity entity = (BaseEntity)DLLAnalysis.GetEntityInstance(einfo.EntityName);

            entity.Id = Convert.ToInt64(table.Rows[rowIndex][einfo.IdNameInSelect]);
            foreach (FieldAttribute pinfo in einfo.Properties)
            {
                DBSQLiteHelper.FetchPropertyValue(entity, pinfo, table, rowIndex);
            }
            foreach (REFAttribute refAtt in einfo.References)
            {
                if ((fetchType == Fetch.Default && !refAtt.LazyLoad) || fetchType == Fetch.REFS || fetchType == Fetch.REFSandSets)
                {
                    DBSQLiteHelper.FetchREFEntity(entity, refAtt, table);
                }
            }
            foreach (SETAttribute setAtt in einfo.Sets)
            {
                if ((fetchType == Fetch.Default && !setAtt.LazyLoad) || fetchType == Fetch.SETS || fetchType == Fetch.REFSandSets)
                {
                    DBSQLiteHelper.FetchSet(entity, setAtt, table);
                }
            }
            //如果实体有对自身实体对象的引用
            if (einfo.REFSelf)
            {
                if (fetchType == Fetch.REFS)
                {
                    FetchParent(entity, table, rowIndex);
                }
                else if (fetchType == Fetch.SETS)
                {
                    FetchChildren(entity, table);
                }
                else if (fetchType == Fetch.REFSandSets)
                {
                    FetchParent(entity, table, rowIndex);
                    FetchChildren(entity, table);
                }
            }
            return(entity);
        }
Пример #3
0
        /// <summary>
        /// 获取实体的父对象
        /// </summary>
        /// <param name="objEntity">宿主实体</param>
        /// <param name="table">数据集</param>
        /// <param name="rIndex">取数据集的行数</param>
        public static void FetchParent(object objEntity, DataTable table, int rIndex)
        {
            EntityInfo einfo = DLLAnalysis.GetEntityInfoByType(objEntity.GetType());
            object     pid   = table.Rows[rIndex]["PTId"];

            if (pid == DBNull.Value || string.IsNullOrEmpty(pid.ToString()))
            {
                return;
            }
            BaseEntity pEntity = (BaseEntity)DLLAnalysis.GetEntityInstance(einfo.EntityName);

            pEntity.Id = Convert.ToInt64(pid);
            try
            {
                pEntity.GetType().GetProperty(einfo.Properties[0].FieldName).SetValue(pEntity, table.Rows[rIndex]["PTC"], null);
                objEntity.GetType().GetProperty("Parent" + einfo.EntityName).SetValue(objEntity, pEntity, null);
            }
            catch (Exception ex) { }
            return;
        }
Пример #4
0
        public static void FetchChildren(object objEntity, DataTable table)
        {
            EntityInfo einfo = DLLAnalysis.GetEntityInfoByType(objEntity.GetType());
            ArrayList  list  = new ArrayList();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                //如果该实体的此集合属性为空
                if (table.Rows[i]["CTId"] == DBNull.Value || table.Rows[i]["CTPId"] == DBNull.Value)
                {
                    continue;
                }
                if (((BaseEntity)objEntity).Id == Convert.ToInt64(table.Rows[i]["CTPId"]))
                {
                    object objElement = DLLAnalysis.GetEntityInstance(einfo.EntityName);
                    //取自身引用子集合时,每个子对象只有Id是载入的,其他属性都没获取
                    ((BaseEntity)objElement).Id = Convert.ToInt64(table.Rows[i]["CTId"]);
                    //objElement.GetType().GetProperty(einfo.Properties[0].FieldName).SetValue(objElement, table.Rows[i]["CTC"], null);
                    list.Add(objElement);
                }
            }
            objEntity.GetType().GetProperty("Child" + einfo.EntityName + "s").SetValue(objEntity, list, null);
        }
Пример #5
0
        public static void FetchSet(object objEntity, SETAttribute setAtt, DataTable table)
        {
            ArrayList list = new ArrayList();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                //如果该实体的此集合属性为空
                if (table.Rows[i][setAtt.ElementEntityId] == DBNull.Value)
                {
                    continue;
                }
                if (((BaseEntity)objEntity).Id == Convert.ToInt64(table.Rows[i][setAtt.ElementEntityId]))
                {
                    object objElement = DLLAnalysis.GetEntityInstance(setAtt.ElementEntityName);
                    if (Convert.IsDBNull(table.Rows[i]["E_" + setAtt.ElementEntityName + "Id"]))
                    {
                        continue;
                    }
                    ((BaseEntity)objElement).Id = Convert.ToInt64(table.Rows[i]["E_" + setAtt.ElementEntityName + "Id"]);
                    EntityInfo elementEinfo = DLLAnalysis.GetEntityInfoByType(objElement.GetType());
                    foreach (FieldAttribute fieldAtt in elementEinfo.Properties)
                    {
                        FetchPropertyValue(objElement, fieldAtt, table, i);
                    }
                    foreach (REFAttribute refAtt in elementEinfo.References)
                    {
                        if (refAtt.REFEntityName.Equals(objEntity.GetType().Name))
                        {
                            objElement.GetType().GetProperty(refAtt.PropertyName).SetValue(objElement, objEntity, null);
                        }
                    }
                    list.Add(objElement);
                }
            }
            objEntity.GetType().GetProperty(setAtt.PropertyName).SetValue(objEntity, list, null);
        }