/// <summary> /// 从ObjectDataList 创建可进行数据库操作的DbCommand。 /// </summary> /// <param name="baseRule">指定操作的业务类(为空将根据集合中的实体类型中的配置信息来自动创建DBCommand)。</param> /// <param name="db">数据库操作DB.</param> /// <param name="dataList">数据</param> /// <returns></returns> public IList <EntityDbCommandInfo> CreateCmdsFromObjectDataList(IBaseRule baseRule, Database db, ObjectDataList dataList) { RuleSettingAttribute mappingAtt = null; if (baseRule != null) { mappingAtt = AttributeConfigHelper.Instance.GetRuleSettingAtt(baseRule); } if (mappingAtt != null && mappingAtt.BaseDataType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 必须要先配置主表数据在单据中的类型值。", MB.Util.APPMessageType.SysErrInfo); } //获取存储变化的数据对象。 IEnumerable <ObjectDataInfo> vals = dataList.GetCanSaveAndOrder(); //必须要进行排序以获取主表的键值 List <EntityDbCommandInfo> cmds = new List <EntityDbCommandInfo>(); object mainKeyValue = null; bool currentIsMain = false; MB.Orm.Persistence.PersistenceManagerHelper persistenceManager = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance; foreach (ObjectDataInfo dataInfo in vals) { if (mappingAtt != null && (int)dataInfo.DataInDocType == (int)mappingAtt.BaseDataType)//获取基本对象的主键值。 { if (mappingAtt.GenerateKeyModel == GenerateKeyModel.OnDataSave) { //设置对象的主键 MB.Orm.Persistence.EntityIdentityHelper.NewInstance.FillEntityIdentity(dataInfo.ObjectData); } mainKeyValue = GetObjectKeyValue(baseRule, dataInfo); currentIsMain = true; } else { currentIsMain = false; } object data = dataInfo.ObjectData; //MB.Orm.Enums.OperationType operationType = ConvertDataStateToOperationType(dataInfo.DataState); //if (operationType == MB.Orm.Enums.OperationType.None) // continue; if (data == null) { continue; } if (data.GetType().IsSubclassOf(typeof(MB.Orm.Common.BaseModel))) { DbCommand[] acmd = null; if (string.IsNullOrEmpty(dataInfo.ExecuteXmlCfgSqlName)) { MB.Orm.Enums.OperationType operationType = ConvertEntityStateToOperationType((data as MB.Orm.Common.BaseModel).EntityState); if (operationType == MB.Orm.Enums.OperationType.None) { continue; } acmd = persistenceManager.GetDbCommand(db, data as MB.Orm.Common.BaseModel, operationType, dataInfo.SavePropertys); } else { acmd = persistenceManager.GetDbCommand(db, data as MB.Orm.Common.BaseModel, dataInfo.ExecuteXmlCfgSqlName); } //如果存在多个Command 上只需要记录一次主体对象就可以,避免多次进行引用约束判断 bool hasRegister = false; foreach (DbCommand d in acmd) { EntityDbCommandInfo ecmd = new EntityDbCommandInfo(data, d); if (!hasRegister) { ecmd.IsMainEntity = currentIsMain; hasRegister = true; } //目前只对主体数据进行删除的约束标记。 if (ecmd.IsMainEntity && dataInfo.DataState == ObjectDataState.Deleted) { ecmd.OperationType = OperationType.Delete; } ecmd.IsPartPropertyUpdate = (dataInfo.SavePropertys != null && dataInfo.SavePropertys.Length > 0); cmds.Add(ecmd); } } else if (data.GetType().IsSubclassOf(typeof(System.Data.DataSet))) { if (mainKeyValue == null) { throw new MB.Util.APPException("在批量存储对象明细前必须先获取主表键值!", MB.Util.APPMessageType.SysErrInfo); } if (baseRule == null) { throw new MB.Util.APPException("通过CreateCmdsFromObjectDataList 执行DataSet时,业务对象不能为空", Util.APPMessageType.SysErrInfo); } IList <EntityDbCommandInfo> tempCmds = CreateCmdsFromDataSet(db, baseRule, data as DataSet, mainKeyValue, dataInfo.DataInDocType); foreach (EntityDbCommandInfo tCmd in tempCmds) { cmds.Add(tCmd); } } else { throw new MB.RuleBase.Exceptions.DataTypeUnSupportException(data.GetType().FullName); } } return(cmds); }
/// <summary> /// 把保存在ObjectDataList 持久化到数据库中。 /// 特殊说明: 暂时先在这里进行服务的提交处理,以后需要进行修改。 /// </summary> /// <param name="baseRule">当前进行持久化操作的业务类。</param> /// <param name="dataList">存储进行数据库操作的ObjectDataList。</param> /// <returns>返回受影响的行。</returns> public int SaveObjectDataList(IBaseRule baseRule, ObjectDataList dataList) { if (dataList == null || dataList.Count == 0) { return(0); } // Create the Database object, using the default database service. The // default database service is determined through configuration. Database db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase(); IList <EntityDbCommandInfo> cmds = CreateCmdsFromObjectDataList(baseRule, db, dataList); if (cmds.Count == 0) { return(0); } bool succeed = false; int count = 0; List <DbCommand> tempCmds = new List <DbCommand>(); //using (DbConnection connection = db.CreateConnection()) { // connection.Open(); try { foreach (EntityDbCommandInfo cmd in cmds) { try { if (cmd.IsMainEntity) { //判断该实体对象是否允许保存。 MB.Orm.Persistence.EntityDistributedHelper.NewInstance.CheckEntityCanSave(cmd.DataEntity); if (baseRule != null) { //判断引用的上级对象是否还在提交状态 MB.RuleBase.Common.ObjectSubmitHelper.NewInstance.CheckParentHasSubmit(db, baseRule, cmd.DataEntity); //如果要删除主题单据的话,需要进行下级数据的引用判断。 if (cmd.OperationType == OperationType.Delete) { //检查是否已存在下级引用对象。 ObjectSubmitHelper.NewInstance.ObjectOwnerless(db, baseRule, cmd.DataEntity, LinkRestrictOption.CancelSubmit); } } } string cmdMsg = MB.Orm.Persistence.DbCommandExecuteTrack.Instance.CommandToTrackMessage(db, cmd.DbCommand); MB.Util.TraceEx.Write("正在执行:" + cmdMsg); int re = 0; using (new Util.MethodTraceWithTime(true, cmdMsg)) { re = db.ExecuteNonQuery(cmd.DbCommand); } //判断是否为部分属性修改,如果是的话,那么需要修改最后修改时间。 if (re > 0 && cmd.IsPartPropertyUpdate) { DbCommand tmpCmd = MB.Orm.Persistence.EntityDistributedHelper.NewInstance.GetSaveLastModifiedDateCommand(db, cmd.DataEntity); if (tmpCmd != null) { db.ExecuteNonQuery(tmpCmd); tempCmds.Add(tmpCmd); } } count += re; } catch (Exception ex) { //throw new MB.RuleBase.Exceptions.DatabaseExecuteException("ExecuteNonQuery 出错!", ex); throw; } } succeed = true; } catch (MB.RuleBase.Exceptions.DatabaseExecuteException dex) { throw; } catch (Exception ex) { //throw new MB.RuleBase.Exceptions.DatabaseExecuteException("SaveObjectDataList 出错!", ex); throw; } finally { //if (connection.State == System.Data.ConnectionState.Open) // connection.Close(); try { foreach (EntityDbCommandInfo cmd in cmds) { cmd.DbCommand.Dispose(); } foreach (DbCommand m in tempCmds) { m.Dispose(); } } catch { } } //if(succeed) // dataList.AcceptDataChanges(); return(count); // } }