Esempio n. 1
0
 public IList <T> GetList <T>(Criteria criteria, params SortInfo[] orderBy)
     where T : new()
 {
     PreconditionAssert.IsNotNull(criteria, ErrorMessages.NullReferenceException);
     try
     {
         SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
         dbCommandCreator.OrderBy    = orderBy;
         dbCommandCreator.ObjectType = typeof(T);
         dbCommandCreator.Cri        = criteria;
         dbCommandCreator.SelectType = SelectType.GetList;
         DbCommand dbCommand = dbCommandCreator.GetDbCommand();
         DataTable dt        = dbAccess.GetDataTableByCommand(dbCommand);
         return(ORMHelper.DataTableToList <T>(dt));
     }
     catch (Exception ex)
     {
         StringBuilder errMsgBuilder = new StringBuilder();
         errMsgBuilder.AppendLine("Select object list by criteria error, detail:");
         errMsgBuilder.AppendLine("Criteria Info:");
         errMsgBuilder.AppendLine(ORMHelper.GetCriteriaMessage(criteria));
         errMsgBuilder.AppendLine("Sort Info:");
         errMsgBuilder.AppendLine(ORMHelper.GetOrderInfoMessage(orderBy));
         throw new ORMException(errMsgBuilder.ToString(), ex);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// 检查中奖号码格式是否正确
        /// </summary>
        /// <param name="winNumber">中奖号码</param>
        /// <param name="gameType">玩法</gameType>
        /// <param name="errMsg">错误消息</param>
        /// <returns>格式是否正确</returns>
        public bool CheckWinNumber(ISportResult winNumber, string gameType, out string errMsg)
        {
            if (!string.IsNullOrEmpty(gameType) && gameType != GameType)
            {
                var orderAnalyzer = KaSon.FrameWork.Analyzer.AnalyzerFactory.AnalyzerFactory.GetSportAnteCodeChecker(GameCode, gameType);
                return(orderAnalyzer.CheckWinNumber(winNumber, null, out errMsg));
            }
            else
            {
                // 前置验证 - 彩种、玩法、投注号码
                PreconditionAssert.IsNotEmptyString(GameCode, "检查中奖号码格式前,必须设置彩种");
                PreconditionAssert.IsNotNull(winNumber, "必须传入非空的比赛结果");
                PreconditionAssert.IsNotEmptyString(winNumber.GetMatchId(GameCode), "必须传入比赛编号");

                _winNumbers = EffectiveWinNumber.Split(Spliter);
                var result = winNumber.GetMatchResult(GameCode, GameType);
                if (result != CancelMatchResultFlag && !_winNumbers.Contains(result))
                {
                    errMsg = string.Format("错误的比赛结果格式,允许的格式:{0},取消结果格式:{1},实际为:'{2}'.", EffectiveWinNumber, CancelMatchResultFlag, result);
                    return(false);
                }
                errMsg = "";
                return(true);
            }
        }
Esempio n. 3
0
 public IList <T> GetAll <T>(int pageIndex, int pageSize, out int totalCount, params SortInfo[] orderBy) where T : new()
 {
     PreconditionAssert.IsNotNull(orderBy, ErrorMessages.NullReferenceException);
     ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);
     try
     {
         SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
         dbCommandCreator.ObjectType = typeof(T);
         dbCommandCreator.OrderBy    = orderBy;
         dbCommandCreator.SelectType = SelectType.GetAll;
         dbCommandCreator.NeedPaged  = true;
         dbCommandCreator.PageIndex  = pageIndex + 1;
         dbCommandCreator.PageSize   = pageSize;
         DbCommand dbCommand = dbCommandCreator.GetDbCommand();
         DataTable dt        = dbAccess.GetDataTableByCommand(dbCommand);
         if (dt.Rows.Count > 0)
         {
             totalCount = (int)dt.Rows[0]["TotalCount"];
         }
         else
         {
             totalCount = 0;
         }
         return(ORMHelper.DataTableToList <T>(dt));
     }
     catch (Exception ex)
     {
         throw new ORMException("Get all object list error, detail:\n\t" + ORMHelper.GetOrderInfoMessage(orderBy), ex);
     }
 }
Esempio n. 4
0
        public static T ConvertDataRowToInfo <T>(DataRow dataRow) where T : new()
        {
            //断言传入的datarow不能为null
            PreconditionAssert.IsNotNull(dataRow, ErrorMessages.NullReferenceException);

            Type infoType = typeof(T);
            T    tempT    = new T();

            //var tempT = Activator.CreateInstance(infoType);
            foreach (var item in infoType.GetProperties())
            {
                var propertyName = item.Name;
                if (!dataRow.Table.Columns.Contains(propertyName))
                {
                    continue;
                }

                if (dataRow[propertyName].Equals(DBNull.Value))
                {
                    item.SetValue(tempT, null, null);
                }
                else
                {
                    //Type type = Nullable.GetUnderlyingType(item.PropertyType) ?? item.PropertyType;
                    item.SetValue(tempT, dataRow[propertyName], null);
                }
            }
            return(tempT);
        }
Esempio n. 5
0
        /// <summary>
        /// 检查一个单独号码的格式
        /// </summary>
        /// <param name="antecodeNumber">号码</param>
        /// <param name="errMsg">错误信息</param>
        /// <returns>格式是否正确</returns>
        public bool CheckAntecodeNumber(ISportAnteCode antecode, out string errMsg)
        {
            // 前置验证 - 彩种、玩法、投注号码
            PreconditionAssert.IsNotEmptyString(GameCode, "检查中奖号码格式前,必须设置彩种");
            PreconditionAssert.IsNotNull(antecode, "必须传入非空的比赛结果");
            PreconditionAssert.IsNotEmptyString(antecode.MatchId, "必须传入比赛编号");
            PreconditionAssert.IsNotEmptyString(antecode.AnteCode, "必须传入比赛投注号码");

            var tmpList = antecode.AnteCode.Split(Spliter);

            if (tmpList.Length != tmpList.GroupBy(c => c).Count())
            {
                errMsg = "有重复的投注号码 - " + antecode.AnteCode;
                return(false);
            }
            _anteCodes = EffectiveAnteCode.Split(Spliter);
            foreach (var item in tmpList)
            {
                if (!_anteCodes.Contains(item))
                {
                    errMsg = "比赛投注号码必须在\"" + EffectiveAnteCode + "\"范围内,实际为 - " + item;
                    return(false);
                }
            }
            errMsg = "";
            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// 通过主键和指定类型从数据库获取对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="keyValue">主键值(通常为Guid),不能为空</param>
        /// <returns>从数据库中得到的对象,如果数据库中没找到对象会返回一个空对象</returns>
        /// 前置条件
        /// 1.keyValue不能为空
        /// 2.传入的对象T只能有一个主键
        public T GetByKey <T>(object keyValue)
            where T : new()
        {
            PreconditionAssert.IsNotNull(keyValue, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.KeyValue   = keyValue;
            dbCommandCreator.SelectType = SelectType.GetOneByKeyValue;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            if (dt.Rows.Count == 0)
            {
                return(default(T));
            }
            else if (dt.Rows.Count == 1)
            {
                return(ORMHelper.ConvertDataRowToEntity <T>(dt.Rows[0]));
            }
            else
            {
                //引发数据获取结果错误
                throw new ORMException(ErrorMessages.ResultNotUniqueMessage);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 通过对象已有的主键值(可以多个主键)和指定类型从数据库获取对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="entity">外部构造的对象,不能为空</param>
        /// <returns>从数据库中得到的对象,如果数据库中没找到对象会返回一个空对象</returns>
        /// 前置条件
        /// 传入的entity对象不能为空
        public T GetByKeys <T>(T entity) where T : new()
        {
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);

            ORMHelper.CheckEntityKey(entity);
            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.Entity     = entity;
            dbCommandCreator.SelectType = SelectType.GetOneByEntityKey;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            if (dt.Rows.Count == 0)
            {
                return(default(T));
            }
            else if (dt.Rows.Count == 1)
            {
                return(ORMHelper.ConvertDataRowToEntity <T>(dt.Rows[0]));
            }
            else
            {
                throw new ORMException(ErrorMessages.ResultNotUniqueMessage);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 将数据库中的行数据转换成一个实体对象
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="dataRow">数据库中的行数据</param>
        /// <returns>指定类型的实体对象</returns>
        public static T ConvertDataRowToEntity <T>(DataRow dataRow) where T : new()
        {
            //断言传入的datarow不能为null
            PreconditionAssert.IsNotNull(dataRow, ErrorMessages.NullReferenceException);

            T          tempT      = new T();
            Type       entityType = typeof(T);
            string     fieldName;
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType);

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                fieldName = mfi.MappingFieldAttribute.FieldName;
                if (string.IsNullOrEmpty(fieldName))
                {
                    continue;
                }

                if (dataRow.Table.Columns.Contains(fieldName))
                {
                    if (dataRow[fieldName].Equals(DBNull.Value))
                    {
                        mfi.ProInfo.SetValue(tempT, null, null);
                    }
                    else
                    {
                        Type type = Nullable.GetUnderlyingType(mfi.ProInfo.PropertyType) ?? mfi.ProInfo.PropertyType;
                        mfi.ProInfo.SetValue(tempT, dataRow[fieldName], null);
                    }
                }
            }
            return(tempT);
        }
Esempio n. 9
0
 public void SaveDataTable(DataTable dt)
 {
     PreconditionAssert.IsNotNull(dt, ErrorMessages.CommandTextIsNullOrEmpty);
     PreconditionAssert.IsNotEmptyString(dt.TableName, ErrorMessages.TableNameIsEmpty);
     DoTransaction(() =>
     {
         DoSaveDataTable(dt);
     });
 }
Esempio n. 10
0
        public void SaveDataTable(DataTable datatb)
        {
            PreconditionAssert.IsNotNull(datatb, ErrorMessages.CommandTextIsNullOrEmpty);
            PreconditionAssert.IsNotNullOrEmpty(datatb.TableName, ErrorMessages.TableNameIsEmpty);

            DoTransaction(() =>
            {
                DoSaveDataTable(datatb);
            });
        }
Esempio n. 11
0
 public object GetRC1ByCommand(DbCommand cmd)
 {
     PreconditionAssert.IsNotNull(cmd, ErrorMessages.CommandTextIsNullOrEmpty);
     try
     {
         _engine.BeginAction();
         return(this.DoGetRC1Value(cmd));
     }
     finally
     {
         _engine.FinishAction();
     }
 }
Esempio n. 12
0
        /// <summary>
        /// 根据表达式删除指定类型的数据
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="cri">表达式</param>
        /// 前置条件
        /// 参数cri不允许为空
        public void Delete <T>(Criteria cri)
        {
            PreconditionAssert.IsNotNull(cri, ErrorMessages.NullReferenceException);
            ORMHelper.CheckEntityIsNotReadOnly(typeof(T), ErrorMessages.EntityReadOnly);

            DeleteCommandCreator dcc = new DeleteCommandCreator(_dbAccess);

            dcc.EntityType = typeof(T);
            dcc.criteria   = cri;
            dcc.deleteType = DeleteType.DeleteByCriteria;
            DbCommand dbCommand = dcc.GetDbCommand();

            _dbAccess.ExecCommand(dbCommand);
        }
Esempio n. 13
0
        /// <summary>
        /// 修改一个数据库中已有的对象,该对象的主键必须有值
        /// 该对象为空或主键为空或修改失败会抛出异常
        /// </summary>
        /// <param name="entity">需要修改的实体对象</param>
        /// 前置条件
        /// 参数entity不允许为空
        public void Modify(object entity)
        {
            //断言参入的参数为null或者空字符串(RErrorCode.NullReference - 0x00000001)
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
            ORMHelper.CheckEntityIsNotReadOnly(entity.GetType(), ErrorMessages.EntityReadOnly);

            ModifyCommandCreator mcc = new ModifyCommandCreator(_dbAccess);

            mcc.Entity = entity;
            DbCommand dbCommand = mcc.GetDbCommand();

            _dbAccess.ExecCommand(dbCommand);
        }
Esempio n. 14
0
        public int ExecCommand(DbCommand cmd)
        {
            PreconditionAssert.IsNotNull(cmd, ErrorMessages.CommandTextIsNullOrEmpty);

            try
            {
                _engine.BeginAction();
                return(this.DoExecCommand(cmd));
            }
            finally
            {
                _engine.FinishAction();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// 通过指定类型,过滤条件和排序方式从数据库中获取该指定类型的数据条数
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="filterProterties">过滤条件</param>
        /// <returns>指定类型的数据条数</returns>
        /// 前置条件
        /// 传入的参数entity不允许为空
        /// filterProterties,orderBy可以为空,如果为空则没有where条件查询和不进行排序
        public int GetCount(object entity, string[] filterProterties)
        {
            //断言传入的entity对象为null或者为空字符串,出现空引用异常
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.Entity           = entity;
            dbCommandCreator.FilterProterties = filterProterties;
            dbCommandCreator.SelectType       = SelectType.GetCount;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();

            return((int)_dbAccess.GetRC1ByCommand(dbCommand));
        }
Esempio n. 16
0
        /// <summary>
        /// 根据表达式,查询对象集合
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="criteria">表达式</param>
        /// <param name="orderBy">排序</param>
        /// <returns>指定类型的数据集合</returns>
        public IList <T> GetList <T>(Criteria criteria, params SortInfo[] orderBy)
            where T : new()
        {
            PreconditionAssert.IsNotNull(criteria, ErrorMessages.NullReferenceException);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.OrderBy    = orderBy;
            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.Cri        = criteria;
            dbCommandCreator.SelectType = SelectType.GetListByExpression;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            return(ORMHelper.DataTableToList <T>(dt));
        }
Esempio n. 17
0
        /// <summary>
        /// 获取数据表中的所有数据集合,并按指定排序方式进行排序
        /// </summary>
        /// <typeparam name="T">指定的类型</typeparam>
        /// <param name="orderBy">排序方式</param>
        /// <returns>数据表中的所有数据集合</returns>
        /// 前置条件
        /// 传入的orderBy对象可以为空,为空的时候则不进行排序
        public IList <T> GetAll <T>(params SortInfo[] orderBy) where T : new()
        {
            PreconditionAssert.IsNotNull(orderBy, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.ObjectType = typeof(T);
            dbCommandCreator.OrderBy    = orderBy;
            dbCommandCreator.SelectType = SelectType.GetAll;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();

            DataTable dt = _dbAccess.GetDataTableByCommand(dbCommand);

            return(ORMHelper.DataTableToList <T>(dt));
        }
Esempio n. 18
0
 public void SaveDataRow(string tableName, DataRow row)
 {
     PreconditionAssert.IsNotNull(row, ErrorMessages.CommandTextIsNullOrEmpty);
     PreconditionAssert.IsNotEmptyString(tableName, ErrorMessages.TableNameIsEmpty);
     _engine.DoCommand((cmd) =>
     {
         using (DbDataAdapter adapter = _engine.CreateDataAdapter())
         {
             adapter.SelectCommand = cmd;
             using (DbCommandBuilder dbCmdBuilder = _engine.CreateCommandBuilder())
             {
                 this.DoSaveDataRow(adapter, dbCmdBuilder, tableName, row);
             }
         }
     });
 }
Esempio n. 19
0
 public void Add(object entity)
 {
     //断言参入的参数为null或者空字符串(RErrorCode.NullReference - 0x00000001)
     PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
     ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
     ORMHelper.CheckEntityIsNotReadOnly(entity.GetType(), ErrorMessages.EntityReadOnly);
     try
     {
         InsertCommandCreator icc = new InsertCommandCreator(dbAccess);
         icc.Entity = entity;
         DbCommand dbCommand = icc.GetDbCommand();
         dbAccess.ExecCommand(dbCommand);
     }
     catch (Exception ex)
     {
         throw new ORMException("Add entity failed, detail:\n\t" + ORMHelper.GetEntityInfoMessage(entity), ex);
     }
 }
Esempio n. 20
0
        public void Modify(object entity)
        {
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
            ORMHelper.CheckEntityIsNotReadOnly(entity.GetType(), ErrorMessages.EntityReadOnly);

            try
            {
                ModifyCommandCreator mcc = new ModifyCommandCreator(dbAccess);
                mcc.Entity = entity;
                DbCommand dbCommand = mcc.GetDbCommand();
                dbAccess.ExecCommand(dbCommand);
            }
            catch (Exception ex)
            {
                throw new ORMException("Update entity failed, detail:\n\t" + ORMHelper.GetEntityInfoMessage(entity), ex);
            }
        }
Esempio n. 21
0
        /// <summary>
        /// 通过指定类型,过滤条件和排序方式从数据库中获取该指定类型的数据集合
        /// </summary>
        /// <typeparam name="T">指定类型</typeparam>
        /// <param name="entity">实体对象</param>
        /// <param name="filterProterties">字段名称,可包含多个</param>
        /// <param name="orderBy">排序方式</param>
        /// <returns>指定类型的数据集合</returns>
        /// 前置条件
        /// 传入的参数entity,filterProterties不允许为空
        /// orderBy可以为空,如果为空则不进行排序
        public IList <T> GetList <T>(T entity, string[] filterProterties, params SortInfo[] orderBy)
            where T : new()
        {
            //断言传入的entity对象为null或者为空字符串,出现空引用异常
            PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
            PreconditionAssert.IsNotNull(filterProterties, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);

            SelectCommandCreator dbCommandCreator = new SelectCommandCreator(_dbAccess);

            dbCommandCreator.Entity           = entity;
            dbCommandCreator.FilterProterties = filterProterties;
            dbCommandCreator.OrderBy          = orderBy;
            dbCommandCreator.SelectType       = SelectType.GetList;
            DbCommand dbCommand = dbCommandCreator.GetDbCommand();
            DataTable dt        = _dbAccess.GetDataTableByCommand(dbCommand);

            return(ORMHelper.DataTableToList <T>(dt));
        }
Esempio n. 22
0
        public IList <T> GetAll <T>(params SortInfo[] orderBy) where T : new()
        {
            PreconditionAssert.IsNotNull(orderBy, ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);
            try
            {
                SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
                dbCommandCreator.ObjectType = typeof(T);
                dbCommandCreator.OrderBy    = orderBy;
                dbCommandCreator.SelectType = SelectType.GetAll;
                DbCommand dbCommand = dbCommandCreator.GetDbCommand();

                DataTable dt = dbAccess.GetDataTableByCommand(dbCommand);
                return(ORMHelper.DataTableToList <T>(dt));
            }
            catch (Exception ex)
            {
                throw new ORMException("Get all object list error, detail:\n\t" + ORMHelper.GetOrderInfoMessage(orderBy), ex);
            }
        }
Esempio n. 23
0
 public IList <T> GetList <T>(Criteria criteria, int pageIndex, int pageSize, out int totalCount, params SortInfo[] orderBy)
     where T : new()
 {
     PreconditionAssert.IsNotNull(criteria, ErrorMessages.NullReferenceException);
     try
     {
         SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
         dbCommandCreator.OrderBy    = orderBy;
         dbCommandCreator.ObjectType = typeof(T);
         dbCommandCreator.Cri        = criteria;
         dbCommandCreator.SelectType = SelectType.GetList;
         dbCommandCreator.NeedPaged  = true;
         dbCommandCreator.PageIndex  = pageIndex + 1;
         dbCommandCreator.PageSize   = pageSize;
         DbCommand dbCommand = dbCommandCreator.GetDbCommand();
         DataTable dt        = dbAccess.GetDataTableByCommand(dbCommand);
         if (dt.Rows.Count > 0)
         {
             totalCount = (int)dt.Rows[0]["TotalCount"];
         }
         else
         {
             totalCount = 0;
         }
         return(ORMHelper.DataTableToList <T>(dt));
     }
     catch (Exception ex)
     {
         StringBuilder errMsgBuilder = new StringBuilder();
         errMsgBuilder.AppendLine("Select object list by criteria error with page, detail:");
         errMsgBuilder.AppendLine("Criteria Info:");
         errMsgBuilder.AppendLine(ORMHelper.GetCriteriaMessage(criteria));
         errMsgBuilder.AppendLine("Page Info:");
         errMsgBuilder.AppendLine("PageIndex:" + pageIndex + "; PageSize:" + pageSize);
         errMsgBuilder.AppendLine("Sort Info:");
         errMsgBuilder.AppendLine(ORMHelper.GetOrderInfoMessage(orderBy));
         throw new ORMException(errMsgBuilder.ToString(), ex);
     }
 }
Esempio n. 24
0
        public static T ConvertDataRowToEntity <T>(DataRow dataRow) where T : new()
        {
            PreconditionAssert.IsNotNull(dataRow, ErrorMessages.NullReferenceException);

            T          tempT      = new T();
            Type       entityType = typeof(T);
            string     fieldName;
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(entityType);

            foreach (SchemaItem mfi in entityInfo.GetAllFieldInfos())
            {
                fieldName = mfi.MappingFieldAttribute.FieldName;
                if (string.IsNullOrEmpty(fieldName))
                {
                    continue;
                }

                if (dataRow.Table.Columns.Contains(fieldName))
                {
                    object value = null;
                    if (!dataRow[fieldName].Equals(DBNull.Value))
                    {
                        Type type = Nullable.GetUnderlyingType(mfi.ProInfo.PropertyType) ?? mfi.ProInfo.PropertyType;
                        if (type.IsEnum)
                        {
                            value = Enum.Parse(type, dataRow[fieldName].ToString());
                        }
                        else
                        {
                            value = Convert.ChangeType(dataRow[fieldName], type);
                        }
                    }
                    mfi.ProInfo.SetValue(tempT, value, null);
                }
            }
            return(tempT);
        }
Esempio n. 25
0
        public T GetByKey <T>(object keyValue)
            where T : new()
        {
            //断言参入的参数为null或者空字符串(RErrorCode.ArgmentesError - 0x00000014)
            PreconditionAssert.IsNotNull(keyValue, ErrorMessages.NullReferenceException);
            PreconditionAssert.IsNotNullOrEmpty(keyValue.ToString(), ErrorMessages.NullReferenceException);
            ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);
            try
            {
                SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
                dbCommandCreator.ObjectType = typeof(T);
                dbCommandCreator.KeyValue   = keyValue;
                dbCommandCreator.SelectType = SelectType.GetOneByKeyValue;
                DbCommand dbCommand = dbCommandCreator.GetDbCommand();
                DataTable dt        = dbAccess.GetDataTableByCommand(dbCommand);

                if (dt.Rows.Count == 0)
                {
                    return(default(T));
                }
                else if (dt.Rows.Count == 1)
                {
                    return(ORMHelper.ConvertDataRowToEntity <T>(dt.Rows[0]));
                }
                else
                {
                    //引发数据获取结果错误
                    throw new ORMException(ErrorMessages.ResultNotUniqueMessage);
                }
            }
            catch (Exception ex)
            {
                //其他未处理的异常
                throw new ORMException("Get object by key error - " + keyValue, ex);
            }
        }
Esempio n. 26
0
 public int GetCount(object entity, string[] filterProterties)
 {
     PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
     ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
     try
     {
         SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
         dbCommandCreator.Entity           = entity;
         dbCommandCreator.FilterProterties = filterProterties;
         dbCommandCreator.SelectType       = SelectType.GetCount;
         DbCommand dbCommand = dbCommandCreator.GetDbCommand();
         return((int)dbAccess.GetRC1ByCommand(dbCommand));
     }
     catch (Exception ex)
     {
         StringBuilder errMsgBuilder = new StringBuilder();
         errMsgBuilder.AppendLine("Get object count by entity error, detail:");
         errMsgBuilder.AppendLine("Entity Info:");
         errMsgBuilder.AppendLine(ORMHelper.GetEntityInfoMessage(entity));
         errMsgBuilder.AppendLine("Filter Proterties:");
         errMsgBuilder.AppendLine(string.Join(",", filterProterties));
         throw new ORMException(errMsgBuilder.ToString(), ex);
     }
 }
Esempio n. 27
0
        public JsonResult UpdateRoleFunciton(OperationRoleModel model)
        {
            CommonResult r = new CommonResult();

            try
            {
                if (!CheckRights("UPDATEROLE"))
                {
                    r.code    = (int)ResultCodeEnum.NoAuth;
                    r.message = "您没有修改角色的权限";
                    return(Json(r));
                }
                var roleInfo = new AdminRole();
                roleInfo.Name = PreconditionAssert.IsNotEmptyString(model.RoleName, "角色名不能为空");
                roleInfo.Id   = PreconditionAssert.IsNotEmptyString(model.RoleId, "角色编号不能为空");
                var roleById = RoleManager.FindById(model.RoleId);
                if (roleById == null)
                {
                    r.code    = (int)ResultCodeEnum.NotFound;
                    r.message = "指定的角色不存在";
                    return(Json(r));
                }
                var roleCount = RoleManager.GetRoleCountByRoleName(model.RoleName);
                if (roleCount >= 2)
                {
                    r.code    = (int)ResultCodeEnum.NotFound;
                    r.message = "指定的角色已经存在";
                    return(Json(r));
                }
                if (IdentityManager.RoleType.SuperAdmin == model.RoleType)
                {
                    var res = RoleManager.UpdateRole(roleInfo);// GlobalCache.ExternalClient.UpdateSystemRole(roleInfo, CurrentUser.UserToken);
                    if (res > 0)
                    {
                        r.code    = (int)ResultCodeEnum.OK;
                        r.message = "更新成功";
                    }
                    else
                    {
                        r.code    = (int)ResultCodeEnum.SystemError;
                        r.message = "更新失败";
                    }
                    return(Json(r));
                }
                var funIds = (List <string>)(PreconditionAssert.IsNotNull(model.FunctionIds, "功能编号集异常"));

                var pers = funIds.Select(p =>
                {
                    return(new AdminRolePermission()
                    {
                        RoleId = model.RoleId,
                        PermissionId = p,
                        CreationDate = DateTime.Now
                    });
                }).ToList();

                AdminRolePermissionManager m = new AdminRolePermissionManager();
                m.DeleteAndAddRolePermissions(model.RoleId, pers);
                AdminOperLogManager operLog = new AdminOperLogManager();
                operLog.AddOperationLog(new AdminOperationLog()
                {
                    CreationDate  = DateTime.Now,
                    Description   = "操作员【" + User.Identity.Name + "】修改角色,角色名称:" + model.RoleName + ",角色编号:" + model.RoleId,
                    OperationName = "角色管理",
                    OperUserId    = CurUser.UserId,
                    UserId        = ""
                });

                RoleManager.UpdateRole(roleInfo);
                return(Json(r));
            }
            catch (Exception ex)
            {
                logger.Error("UpdateRoleFunciton", ex);
                return(Json(new { IsSuccess = false, Msg = ex.Message }));
            }
        }