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
 public T GetByKeys <T>(T entity) where T : new()
 {
     PreconditionAssert.IsNotNull(entity, ErrorMessages.NullReferenceException);
     ORMHelper.EntityIsMappingDatabase(entity.GetType(), ErrorMessages.EntityMappingError);
     try
     {
         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);
         }
     }
     catch (Exception ex)
     {
         throw new ORMException("Get object by keys error, keys value:\n\t" + ORMHelper.GetEntityInfoMessage(entity), ex);
     }
 }
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
        internal virtual void Init(Type objectType, IDBAccess dbAccess)
        {
            TypeSchema entityInfo = ORMSchemaCache.GetTypeSchema(objectType);
            SchemaItem fieldInfo  = entityInfo.GetFieldInfoByPropertyName(this._propertyName);

            if (fieldInfo == null)
            {
                throw new ArgumentException(string.Format("{0} - Property \"{1}\" not define a mapping field.", this.GetType(), _propertyName));
            }
            Type type = fieldInfo.ProInfo.PropertyType;

            type            = Nullable.GetUnderlyingType(type) ?? type;
            this._dbType    = ORMHelper.GetDbTypeByName(type);
            this._fieldName = fieldInfo.MappingFieldAttribute.FieldName;
            this._dbAccess  = dbAccess;
        }
Esempio n. 5
0
 public int GetAllCount <T>()
 {
     ORMHelper.EntityIsMappingDatabase(typeof(T), ErrorMessages.EntityMappingError);
     try
     {
         SelectCommandCreator dbCommandCreator = new SelectCommandCreator(dbAccess);
         dbCommandCreator.ObjectType = typeof(T);
         dbCommandCreator.SelectType = SelectType.GetAllCount;
         DbCommand dbCommand = dbCommandCreator.GetDbCommand();
         return((int)dbAccess.GetRC1ByCommand(dbCommand));
     }
     catch (Exception ex)
     {
         throw new ORMException("Get object all count error.", ex);
     }
 }
        protected DbParameter GetDbParameter(PropertyInfo fieldProperty, object entity)
        {
            DbParameter parameter = DbAccess.CreateDbParameter();
            Type        tempType  = fieldProperty.PropertyType;

            if (Nullable.GetUnderlyingType(tempType) != null)//如果是 int? 类型则进行处理
            {
                tempType = Nullable.GetUnderlyingType(tempType);
            }

            parameter.ParameterName = "@" + fieldProperty.Name;
            parameter.DbType        = ORMHelper.GetDbTypeByName(tempType);
            object value = fieldProperty.GetValue(entity, null);

            parameter.Value = value ?? DBNull.Value;    //如果对象的属性为null,则把此参数设置为DBNull
            return(parameter);
        }
Esempio n. 7
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. 8
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);
            }
        }
        protected DbCommand GetDbCommandByKeyValue(TypeSchema entityInfo, object keyValue)
        {
            DbCommand dbCommand = DbAccess.CreateDbCommand();

            foreach (SchemaItem mfi in entityInfo.GetKeyFieldInfos())
            {
                Type type = mfi.ProInfo.PropertyType;
                type = Nullable.GetUnderlyingType(type) ?? type;
                DbType      dbType    = ORMHelper.GetDbTypeByName(type);
                DbParameter parameter = DbAccess.CreateDbParameter();
                parameter.ParameterName = "@" + mfi.ProInfo.Name;
                parameter.DbType        = dbType;

                //parameter.Value = Convert.ChangeType(keyValue, fieldProperty.PropertyType);
                parameter.Value = Convert.ChangeType(keyValue, type);
                dbCommand.Parameters.Add(parameter);
            }
            return(dbCommand);
        }
Esempio n. 10
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. 11
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. 12
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. 13
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);
            }
        }