Beispiel #1
0
        private T LoadObjectDataBaseDirect <T>(string alias, string tableName, string id, bool isNolock) where T : SRO, new()
        {
            Type type = typeof(T);

            IDbOperate  dbOperate  = DbProxy.Create(alias, type);
            IDataReader dataReader = null;

            try
            {
                string sql = string.Format(isNolock ? SELECTSINGLEOBJECTWITHNOLOCK : SELECTSINGLEOBJECT, tableName);

                IMyDbParameter[] prams =
                {
                    DbParameterProxy.Create("@Id", SqlDbType.NVarChar, 32, id)
                };

                dataReader = dbOperate.GetDataReader(sql, prams);

                if (!dataReader.Read())
                {
                    return(default(T));
                }

                T obj = LoadObject <T>(dataReader);
                obj.CurrentAlias     = alias;
                obj.CurrentTableName = tableName;

                return(obj);
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, true);
                throw;
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
            }
        }
        public static T[] LoadObjects <T>(string alias, string tableName, int top, string condition, IMyDbParameter[] prams, bool isNolock) where T : SRO, new()
        {
            Type type = typeof(T);

            IDbOperate  dbOperate  = DbProxy.Create(alias, type);
            IDataReader dataReader = null;

            try
            {
                string sql = string.Format(isNolock ? SELECTOBJECTSWITHNOLOCK : SELECTOBJECTS,
                                           (top <= 0 ? "" : "TOP " + top.ToString()),
                                           tableName, condition);

                dataReader = dbOperate.GetDataReader(sql, prams);

                List <T> list = new List <T>();
                while (dataReader.Read())
                {
                    list.Add(LoadObject <T>(dataReader));
                }

                return(list.ToArray());
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, true);
                throw;
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
            }
        }
Beispiel #3
0
        public T[] LoadObjects <T>(string alias, string tableName, int top, string condition, IMyDbParameter[] prams, bool isLock, LoadType loadType) where T : SRO, new()
        {
            IDataReader dataReader = null;

            try
            {
                string cacheId   = null;
                bool   needCache = false;

                if (loadType == LoadType.CacheFirst && CacheService != null)
                {
                    needCache = CacheService.IsNeedCached <ArrayCache <T> >();
                    if (needCache)
                    {
                        string jsonPrams = JsonConvert.SerializeObject(prams);
                        cacheId = string.Format("{0}-{1}-{2}-{3}-{4}-{5}", alias, tableName, top, condition, jsonPrams, isLock);
                        cacheId = Tools.MD5(cacheId);

                        T[] objs = LoadCacheObjects <T>(cacheId);
                        if (objs != null)
                        {
                            return(objs);
                        }
                    }
                }

                Type   type = typeof(T);
                string format;
                if (isLock)
                {
                    if (string.IsNullOrEmpty(condition))
                    {
                        format = SELECTOBJECTS;
                    }
                    else
                    {
                        format = SELECTOBJECTSHASCONDITION;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(condition))
                    {
                        format = SELECTOBJECTSWITHNOLOCK;
                    }
                    else
                    {
                        format = SELECTOBJECTSWITHNOLOCKHASCONDITION;
                    }
                }

                string sql = string.Format(format,
                                           (top <= 0 ? "" : "TOP " + top.ToString()),
                                           tableName, condition);

                IDbOperate dbOperate = DbProxy.Create(alias, type);
                dataReader = dbOperate.GetDataReader(sql, prams);

                List <T> list = new List <T>();
                while (dataReader.Read())
                {
                    list.Add(LoadObject <T>(dataReader));
                }

                T[] t = list.ToArray();
                if (needCache)
                {
                    AddCache <T>(cacheId, t);
                }
                return(t);
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, true);
                throw;
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
            }
        }
        public static SRO LoadObject(Type type, string alias, string tableName, string id, LoadType loadType, bool isNolock)
        {
            if (string.IsNullOrEmpty(id))
            {
                LogProxy.Error(IDISNULL, true);
                return(null);
            }

            if (loadType != LoadType.DataBaseDirect)
            {
                SRO obj = LoadCacheObject(type, id) as SRO;

                if (loadType == LoadType.OnlyCache)
                {
                    return(obj);
                }

                if (obj != null)
                {
                    return(obj);
                }
            }

            IDbOperate  dbOperate  = DbProxy.Create(alias, type);
            IDataReader dataReader = null;

            try
            {
                string sql = string.Format(isNolock ? SELECTSINGLEOBJECTWITHNOLOCK : SELECTSINGLEOBJECT, tableName);

                IMyDbParameter[] prams =
                {
                    DbParameterProxy.Create("@Id", SqlDbType.NVarChar, 32, id)
                };

                dataReader = dbOperate.GetDataReader(sql, prams);

                if (!dataReader.Read())
                {
                    return(null);
                }

                SRO obj = LoadObject(type, dataReader);
                if (obj == null)
                {
                    return(null);
                }
                obj.CurrentAlias     = alias;
                obj.CurrentTableName = tableName;
                return(obj);
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, true);
                throw;
            }
            finally
            {
                if (dataReader != null)
                {
                    dataReader.Close();
                }
            }
        }