Esempio n. 1
0
        public static void SaveObject(SRO obj)
        {
            if (obj == null)
            {
                LogProxy.Warn(OBJECTISNULL);
                return;
            }

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    SaveSingleObject(obj);

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                obj.Reset();

                LogProxy.Error(ex, true);
            }

            obj.AfterSave();
        }
Esempio n. 2
0
        private static void Save(SRO obj, string sql, IMyDbParameter[] prams)
        {
            string tableName;
            Type   type = obj.GetType();

            string[]   aliases = obj.Aliases;
            IDbOperate dbOperate;

            if (aliases.Length == 0)
            {
                tableName = GetTableName(obj, null);

                dbOperate = DbProxy.Create(null, type);
                dbOperate.ExecuteNonQuery(string.Format(sql, tableName), prams);
            }
            else
            {
                foreach (string alias in aliases)
                {
                    tableName = GetTableName(obj, alias);

                    dbOperate = DbProxy.Create(alias, type);
                    dbOperate.ExecuteNonQuery(string.Format(sql, tableName), prams);
                }
            }
        }
Esempio n. 3
0
            public static string UpdateBuilder(SRO obj, out IMyDbParameter[] prams)
            {
                List <IMyDbParameter> pramList = new List <IMyDbParameter>();
                Type type = obj.GetType();

                StringBuilder sql = new StringBuilder("UPDATE [{0}] SET ");

                bool isModified = false;

                IMyPropertyInfo[] myPropertys = PropertyInfoProxy.GetProperties(obj.GetType());
                foreach (IMyPropertyInfo property in myPropertys)
                {
                    if (property.IsSave == false || property.Name == "CreatedDate" || property.Name == "Version")
                    {//判断是否保存
                        continue;
                    }

                    object pValue = property.GetValue(obj);
                    if (property.Name == "Id" || property.Name == "LastAlterDate")
                    {
                        pramList.Add(ParameterBuilder(property, pValue));
                        continue;
                    }

                    if (property.PropertyType == typeof(DateTime) && (DateTime)pValue == DateTime.MinValue && obj.GetOriginalValue(property.Name) == null)
                    {
                        continue;
                    }
                    else if (object.Equals(obj.GetOriginalValue(property.Name), pValue))
                    {
                        continue;
                    }

                    if (!isModified)
                    {
                        isModified = true;
                    }

                    sql.Append("[");
                    sql.Append(property.Name);
                    sql.Append("]=@");
                    sql.Append(property.Name);
                    sql.Append(",");

                    pramList.Add(ParameterBuilder(property, pValue));
                }

                if (isModified == false)
                {
                    prams = null;
                    return(null);
                }

                prams = pramList.ToArray();
                sql.Append(string.Format("[LastAlterDate]=@LastAlterDate,[Version]={0} WHERE [Id]=@Id AND [Version]={1}",
                                         ++obj.Version, (long)obj.GetOriginalValue("Version")));

                return(sql.ToString());
            }
Esempio n. 4
0
        private static void Insert(SRO obj)
        {
            Type type = obj.GetType();

            IMyDbParameter[] prams;
            string           insertSql = SqlBuilder.InsertBuilder(obj, out prams);

            Save(obj, insertSql, prams);
        }
Esempio n. 5
0
            public static string UpdateBuilder(SRO obj, out IMyDbParameter[] prams)
            {
                List <IMyDbParameter> pramList = new List <IMyDbParameter>();
                Type type = obj.GetType();

                StringBuilder sql = new StringBuilder("UPDATE [{0}] SET ");

                bool isModified = false;

                IMyPropertyInfo[] myPropertys = PropertyInfoProxy.GetProperties(obj.GetType());
                foreach (IMyPropertyInfo property in myPropertys)
                {
                    if (property.IsSave == false || property.Name == "CreatedDate" || property.Name == "LastAlterDate")
                    {//判断是否保存
                        continue;
                    }

                    object pValue = property.GetValue(obj);
                    if (property.Name == "Id")
                    {
                        pramList.Add(ParameterBuilder(property, pValue));
                        continue;
                    }

                    if (object.Equals(obj.GetOriginalValue(property.Name), pValue))
                    {
                        continue;
                    }

                    isModified = true;

                    sql.Append("[");
                    sql.Append(property.Name);
                    sql.Append("]=@");
                    sql.Append(property.Name);
                    sql.Append(",");

                    pramList.Add(ParameterBuilder(property, pValue));
                }

                if (isModified == false)
                {
                    prams = null;
                    return(null);
                }

                prams = pramList.ToArray();
                sql.Append(string.Format("[LastAlterDate]='{0}' WHERE [Id]=@Id", obj.LastAlterDate));

                return(sql.ToString());
            }
Esempio n. 6
0
        private static void SaveSingleObject(SRO obj)
        {
            obj.LastAlterDate = DateTime.Now;
            obj.BeforeSave();

            if (obj.IsNew)
            {
                Insert(obj);
            }
            else
            {
                Update(obj);
            }
        }
Esempio n. 7
0
        private static void Update(SRO obj)
        {
            Type type = obj.GetType();

            IMyDbParameter[] prams;
            string           updateSql = SqlBuilder.UpdateBuilder(obj, out prams);

            if (string.IsNullOrEmpty(updateSql))
            {
                LogProxy.InfoFormat(OBJECTSISNOTMODIFIED, obj.GetType(), obj.Id);
                return;
            }

            Save(obj, updateSql, prams);
        }
Esempio n. 8
0
        private static void AddCache(SRO obj)
        {
            try
            {
                if (CacheService != null)
                {
                    obj.Bestrow();

                    CacheService.Set(obj);
                }
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, false);
            }
        }
Esempio n. 9
0
        private static void Save(SRO obj, string sql, IMyDbParameter[] prams)
        {
            string tableName;
            string executeSql;
            int    executeCnt;
            Type   type = obj.GetType();

            string[]   aliases = obj.Aliases;
            IDbOperate dbOperate;

            if (aliases.Length == 0)
            {
                tableName  = GetTableName(obj, null);
                executeSql = string.Format(sql, tableName);

                dbOperate  = DbProxy.Create(null, type);
                executeCnt = dbOperate.ExecuteNonQuery(executeSql, prams);
                if (executeCnt != 1)
                {
                    string jsonPrams = JsonConvert.SerializeObject(prams);
                    LogProxy.Error(string.Format(SAVEFAIL, executeSql, jsonPrams), true);
                }
            }
            else
            {
                foreach (string alias in aliases)
                {
                    tableName  = GetTableName(obj, alias);
                    executeSql = string.Format(sql, tableName);

                    dbOperate  = DbProxy.Create(alias, type);
                    executeCnt = dbOperate.ExecuteNonQuery(executeSql, prams);
                    if (executeCnt != 1)
                    {
                        string jsonPrams = JsonConvert.SerializeObject(prams);
                        LogProxy.Error(string.Format(SAVEFAIL, executeSql, jsonPrams), true);
                    }
                }
            }
        }
Esempio n. 10
0
        private static SRO LoadObject(Type type, IDataReader dataReader)
        {
            SRO obj = CreateObject(type) as SRO;

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

            IMyPropertyInfo[] myPropertys = PropertyInfoProxy.GetProperties(type);
            foreach (IMyPropertyInfo property in myPropertys)
            {
                if (property.IsLoad == false)
                {//判断是否加载
                    continue;
                }

                object value = dataReader[property.Name];

                if (value == DBNull.Value)
                {
                    continue;
                }

                property.SetValue(obj, value);
                obj.SetOriginalValue(property.Name, property.GetValue(obj));
            }

            IMyPropertyInfo isNew = PropertyInfoProxy.GetProperty(type, "IsNew");

            isNew.SetValue(obj, false);


            AddCache(obj);

            return(obj);
        }
Esempio n. 11
0
            private static IMyDbParameter[] InsertParametersBuilder(SRO obj)
            {
                List <IMyDbParameter> prams = new List <IMyDbParameter>();

                IMyPropertyInfo[] myPropertys = PropertyInfoProxy.GetProperties(obj.GetType());

                foreach (IMyPropertyInfo property in myPropertys)
                {
                    if (property.IsSave == false)
                    {//判断是否保存
                        continue;
                    }

                    object pValue = property.GetValue(obj);
                    if (pValue == null)
                    {
                        pValue = DBNull.Value;
                    }

                    prams.Add(ParameterBuilder(property, pValue));
                }

                return(prams.ToArray());
            }
Esempio n. 12
0
        public void SaveObject(SRO obj)
        {
            if (obj == null)
            {
                LogProxy.Warn(OBJECTISNULL);
                return;
            }

            try
            {
                if (BeforeSaveMothed != null)
                {
                    BeforeSaveMothed(new SRO[] { obj });
                }
            }
            catch (Exception ex)
            {
                LogProxy.Error(ex, false);
            }

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    SaveSingleObject(obj);

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                obj.Reset();

                LogProxy.Error(ex, true);
            }

            AddCache(obj);

            if (!obj.IsNew && obj.SaveMode != SROSaveMode.NoChange && SynClientService != null)
            {
                SynClientService.Add(obj);
            }

            obj.IsNew = false;

            if (obj.SaveMode != SROSaveMode.NoChange)
            {
                try
                {
                    obj.AfterSave();
                }
                catch (Exception ex)
                {
                    LogProxy.Error(ex, false);
                }

                try
                {
                    if (AfterSaveMothed != null)
                    {
                        AfterSaveMothed(new SRO[] { obj });
                    }
                }
                catch (Exception ex)
                {
                    LogProxy.Error(ex, false);
                }
            }
        }
Esempio n. 13
0
 public static string InsertBuilder(SRO obj, out IMyDbParameter[] prams)
 {
     prams = InsertParametersBuilder(obj);
     return(GetInsertSql(obj.GetType()));
 }
Esempio n. 14
0
 private static string GetTableName(SRO obj, string alias)
 {
     return(obj.GetTableName(alias));
 }
Esempio n. 15
0
        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();
                }
            }
        }