Esempio n. 1
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);
            }
        }