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); } }
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); } }
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); } }
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); } }