/// <summary> /// 获取数据对象的主键。 /// </summary> /// <param name="baseRule"></param> /// <param name="dataInfo"></param> /// <returns></returns> public object GetObjectKeyValue(IBaseRule baseRule, ObjectDataInfo dataInfo) { string keyName = null; if (dataInfo.DataInDocType != null) { ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInfo.DataInDocType); if (mappingAtt != null) { keyName = mappingAtt.KeyName; } } bool isEntity = dataInfo.ObjectData.GetType().IsSubclassOf(typeof(MB.Orm.Common.BaseModel)); if (string.IsNullOrEmpty(keyName) && isEntity) { MB.Orm.Mapping.Att.ModelMapAttribute tattr = Attribute.GetCustomAttribute(dataInfo.ObjectData.GetType(), typeof(MB.Orm.Mapping.Att.ModelMapAttribute)) as MB.Orm.Mapping.Att.ModelMapAttribute; string[] keys = tattr.PrimaryKeys; if (keys != null && keys.Length > 0) { MB.Util.TraceEx.Assert(keys.Length > 1, "业务类:" + baseRule.GetType().FullName + " 主表对象的键值配置不能存在联合主键!"); } keyName = keys[0]; } if (string.IsNullOrEmpty(keyName)) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 的主表对象的主键信息没有配置!", MB.Util.APPMessageType.SysErrInfo); } if (isEntity) { return(MB.Util.MyReflection.Instance.InvokePropertyForGet(dataInfo.ObjectData, keyName)); } else if ((dataInfo.ObjectData as DataRow) != null) { return((dataInfo.ObjectData as DataRow)[keyName]); } else { throw new MB.Util.APPException("获取业务类:" + baseRule.GetType().FullName + " 的主表对象的键值有误!", MB.Util.APPMessageType.SysErrInfo); } }
/// <summary> /// 批量创建实体对象。 /// </summary> /// <param name="baseRule"></param> /// <param name="dataInDocType"></param> /// <param name="createCount"></param> /// <returns></returns> public IList CreateNewEntityBatch(IBaseRule baseRule, object dataInDocType, int createCount) { ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType); if (mappingAtt == null) { throw new MB.RuleBase.Exceptions.RequireConfigDataMappingException(dataInDocType); } Type entityType = mappingAtt.EntityType; if (string.Compare(entityType.FullName, "System.Object", true) == 0) { if (mappingAtt.EntityType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 中的:" + dataInDocType.GetType() + " 的数据值:" + dataInDocType.ToString() + " 在配置ObjectDataMapping时需要配置 EntityType", MB.Util.APPMessageType.SysErrInfo); } } List <object> lstData = new List <object>(); int id = 0; //判断是否为自增列 if (mappingAtt.KeyIsSelfAdd) { id = MB.Orm.Persistence.EntityIdentityHelper.NewInstance.GetEntityIdentity(mappingAtt.MappingTableName, createCount); } for (int i = 0; i < createCount; i++) { object entity = MB.Util.DllFactory.Instance.CreateInstance(entityType); //判断是否为自增列 if (id > 0) { MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, mappingAtt.KeyName, id++); } if (entity.GetType().IsSubclassOf(typeof(MB.Orm.Common.BaseModel))) { (entity as MB.Orm.Common.BaseModel).EntityState = EntityState.New; } //设置默认值 MB.Orm.Mapping.ModelMappingInfo mappingInfo = MB.Orm.Mapping.AttMappingManager.Instance.GetModelMappingInfo(entityType); foreach (MB.Orm.Mapping.FieldPropertyInfo colProperty in mappingInfo.FieldPropertys.Values) { if (colProperty.DefaultValue == null) { continue; } System.Reflection.PropertyInfo proInfo = entityType.GetProperty(colProperty.PropertyName); proInfo.SetValue(entity, colProperty.DefaultValue, null); } lstData.Add(entity); } return(lstData); }
/// <summary> /// 通过主键获取对应明细的数据。 /// </summary> /// <typeparam name="T">从MB.Orm.Common.BaseModel 中继承的数据对象类型。</typeparam> /// <param name="baseRule">指定操作的业务类</param> /// <param name="dataInDocType">指定的数据类型</param> /// <param name="mainKeyValue">主键</param> /// <returns></returns> public List <T> GetObjectsByForeingKey <T>(IBaseRule baseRule, object dataInDocType, object mainKeyValue) { ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType); if (mappingAtt == null) { throw new MB.RuleBase.Exceptions.RequireConfigDataMappingException(dataInDocType); } Type entityType = typeof(T); if (string.Compare(entityType.FullName, "System.Object", true) == 0) { if (mappingAtt.EntityType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 中的:" + dataInDocType.GetType() + " 的数据值:" + dataInDocType.ToString() + " 在配置ObjectDataMapping时需要配置 EntityType", MB.Util.APPMessageType.SysErrInfo); } entityType = mappingAtt.EntityType; } return(DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml <T>(entityType, mappingAtt.MappingXmlFileName, MB.Orm.Mapping.Xml.XmlSqlMappingInfo.SQL_GET_BY_FOREING_KEY, mainKeyValue)); }
/// <summary> /// 根据键值获取包含值的实体对象。 /// </summary> /// <typeparam name="T">从MB.Orm.Common.BaseModel 中继承的数据对象类型。</typeparam> /// <param name="baseRule">获取实体对象集合的业务类。</param> /// <param name="dataInDocType">在业务类中的数据类型。</param> /// <param name="keyValue">需要获取实体键值。</param> /// <returns>返回指定类型的实体对象。</returns> public T GetObjectByKey <T>(IBaseRule baseRule, object dataInDocType, object keyValue) { ObjectDataMappingAttribute mappingAtt = AttributeConfigHelper.Instance.GetObjectDataMappingAttribute(dataInDocType); if (mappingAtt == null) { throw new MB.RuleBase.Exceptions.RequireConfigDataMappingException(dataInDocType); } Database db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase(); System.Data.Common.DbCommand[] cmds = MB.Orm.Persistence.PersistenceManagerHelper.NewInstance.CreateDbCommandByXml(db, mappingAtt.MappingXmlFileName, MB.Orm.Mapping.Xml.XmlSqlMappingInfo.SQL_SELECT_BY_KEY, keyValue); if (cmds.Length != 1) { throw new MB.RuleBase.Exceptions.SelectSqlXmlConfigException(mappingAtt.MappingXmlFileName, MB.Orm.Mapping.Xml.XmlSqlMappingInfo.SQL_SELECT_BY_KEY); } System.Data.Common.DbCommand cmd = cmds[0]; Type entityType = typeof(T); if (string.Compare(entityType.FullName, "System.Object", true) == 0) { if (mappingAtt.EntityType == null) { throw new MB.Util.APPException("业务类:" + baseRule.GetType().FullName + " 中的:" + dataInDocType.GetType() + " 的数据值:" + dataInDocType.ToString() + " 在配置ObjectDataMapping时需要配置 EntityType", MB.Util.APPMessageType.SysErrInfo); } entityType = mappingAtt.EntityType; } bool isBaseModelSub = entityType.IsSubclassOf(typeof(BaseModel)); MB.Orm.Mapping.ModelMappingInfo modelMapping = MB.Orm.Mapping.AttMappingManager.Instance.GetModelMappingInfo(entityType); object entity = MB.Util.DllFactory.Instance.CreateInstance(entityType); string cmdMsg = MB.Orm.Persistence.DbCommandExecuteTrack.Instance.CommandToTrackMessage(db, cmd); MB.Util.TraceEx.Write("正在执行:" + cmdMsg); using (new Util.MethodTraceWithTime(cmdMsg)) { using (IDataReader reader = db.ExecuteReader(cmd)) { try { DataTable dtReader = reader.GetSchemaTable(); while (reader.Read()) { foreach (MB.Orm.Mapping.FieldPropertyInfo proInfo in modelMapping.FieldPropertys.Values) { if (dtReader.Select(string.Format("ColumnName='{0}'", proInfo.FieldName)).Length <= 0) { continue; } if (reader[proInfo.FieldName] != System.DBNull.Value) { MB.Util.MyReflection.Instance.InvokePropertyForSet(entity, proInfo.PropertyName, reader[proInfo.FieldName]); } } break; } } catch (Exception ex) { throw new MB.RuleBase.Exceptions.DatabaseExecuteException("执行" + baseRule.GetType().FullName + "GetObjectByKey<T> 出错!", ex); } finally { } } } if (isBaseModelSub) { MB.Orm.Common.BaseModel temp = entity as MB.Orm.Common.BaseModel; //if (temp == null) // throw new MB.Util.APPException(string.Format("所有的数据实体对象都必须从MB.Orm.Common.BaseModel 继承,请检查{0}", entity.GetType())); temp.EntityState = EntityState.Persistent; } return((T)entity); }
/// <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); }