예제 #1
0
        /// <summary>
        /// 判断本实例更变的值与被比实体对应值是否相等
        /// </summary>
        /// <param name="objEntity">被比较实体</param>
        /// <returns>是否相等</returns>
        public bool EqualsModify(EntityObjectBase objEntity)
        {
            if (objEntity.GetType() != this.GetType())
            {
                return(false);
            }

            TableAttributes taEntity = objEntity.GetEntityColumns();
            TableAttributes taThis   = this.GetEntityColumns();

            bool bEquals = true;

            for (int i = 0; i < taEntity.Columns.Count; i++)
            {
                ColumnAttributes caEntity = taEntity.Columns[i] as ColumnAttributes;
                ColumnAttributes caThis   = taThis.Columns[i] as ColumnAttributes;
                if (caThis.IsModify && (caEntity.Value.ToString() != caThis.Value.ToString()))
                {
                    bEquals = false;
                    break;
                }
            }

            return(bEquals);
        }
예제 #2
0
        /// <summary>
        /// 把传输对像转换成数据表
        /// </summary>
        /// <param name="objTranfser">数据传输对像</param>
        /// <returns>转换得到的表</returns>
        private DataTable MappingToTable(EntityObjectBase objTranfser)
        {
            ArrayList alRow = new ArrayList();

            DataTable dt = new DataTable(objTranfser.GetType().Name);

            foreach (PropertyInfo pi in objTranfser.GetType().GetProperties())
            { // 根据属性映射创建表模式并记录属性值
                object[] obj = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (obj.Length > 0)
                {
                    if (obj[0] is ColumnMapping)
                    {
                        ColumnMapping tmAtrr = obj[0] as ColumnMapping;

                        if (pi.CanRead && !dt.Columns.Contains(tmAtrr.ColumnName))
                        {
                            dt.Columns.Add(tmAtrr.ColumnName);         // 创建表例

                            alRow.Add(pi.GetValue(objTranfser, null)); // 记录属性值
                        }
                    }
                }
            }

            dt.Rows.Add(alRow.ToArray());

            return(dt);
        }
예제 #3
0
        /// <summary>
        /// 数据行与实体对像之间的映射
        /// </summary>
        /// <param name="objTranfser">被映射的传输对像</param>
        private void MappingToObject(EntityObjectBase objTranfser)
        {
            DataRow row = this.table.Rows[0];

            foreach (DataColumn col in this.table.Columns)
            {
                if (row[col] != System.DBNull.Value)
                {
                    this.SetMappingValue(objTranfser, col.ColumnName, row[col]);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 将指定实体修改过的值同步到本实例
        /// </summary>
        /// <param name="objEntity">同步实体</param>
        public void SynchronizeModifyValue(EntityObjectBase objEntity)
        {
            TableAttributes taEntity = objEntity.GetEntityColumns();

            foreach (ColumnAttributes caCurrent in taEntity.Columns)
            {
                if (true == caCurrent.IsModify)
                {
                    SetMappingValue(this, caCurrent.ColumnName, caCurrent.Value);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// 取实体映射的数据库表信息
        /// </summary>
        /// <param name="objEntity">实体对像</param>
        /// <returns>映射表信息</returns>
        public static TableAttributes GetEntityColumns(EntityObjectBase objEntity)
        {
            TableAttributes taEntity = new TableAttributes();

            #region 取表名称

            object[] objTable = objEntity.GetType().GetCustomAttributes(typeof(TableMapping), false);
            if (objTable.Length > 0)
            {
                if (objTable[0] is TableMapping)
                {
                    TableMapping tmCurrent = objTable[0] as TableMapping;
                    taEntity.TableName = tmCurrent.TableName;
                }
            }

            #endregion

            #region 取字段名称

            foreach (PropertyInfo pi in objEntity.GetType().GetProperties())
            { // 根据属性映射创建表模式并记录属性值
                object[] objColumn = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (objColumn.Length > 0)
                {
                    if (objColumn[0] is ColumnMapping)
                    {
                        if (pi.CanRead)
                        {
                            ColumnMapping    attrCurrent = objColumn[0] as ColumnMapping;
                            ColumnAttributes caCurrent   = new ColumnAttributes();

                            caCurrent.ColumnName      = attrCurrent.ColumnName;
                            caCurrent.IsIdentity      = attrCurrent.IsIdentity;
                            caCurrent.IsPrimaryKey    = attrCurrent.IsPrimaryKey;
                            caCurrent.IsVersionNumber = attrCurrent.IsVersionNumber;
                            caCurrent.Value           = pi.GetValue(objEntity, null);
                            caCurrent.OriginalValue   = objEntity.table.Rows[0][attrCurrent.ColumnName];
                            if (caCurrent.OriginalValue.ToString() != caCurrent.Value.ToString())
                            {
                                caCurrent.IsModify = true;
                            }

                            taEntity.Columns.Add(caCurrent);
                        }
                    }
                }
            }

            #endregion

            return(taEntity);
        }
예제 #6
0
        public static EntityObjectBase Copy(EntityObjectBase objEntity)
        {
            //生成拷贝实例
            Type             type    = objEntity.GetType();
            EntityObjectBase objCopy = (EntityObjectBase)Activator.CreateInstance(type);

            //拷贝属性值
            objCopy.FromTable(objEntity.ToTable());

            //拷贝原始值
            objCopy.table = objEntity.table.Copy();

            return(objCopy);
        }
예제 #7
0
        /// <summary>
        /// 同步设置主键及外键值
        /// </summary>
        /// <param name="objForgetKeyEntity">从表实体</param>
        /// <param name="objValue">主键值</param>
        public void SetForgetKeyValue(EntityObjectBase objForgetKeyEntity, object objValue)
        {
            Type   type            = this.GetType();
            string strKeyCoumnName = "";

            foreach (PropertyInfo pi in  type.GetProperties())
            {
                object[] obj = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (obj.Length > 0)
                {
                    if (obj[0] is ColumnMapping)
                    {
                        ColumnMapping tmAtrr = obj[0] as ColumnMapping;
                        if (true == tmAtrr.IsPrimaryKey)
                        {
                            pi.SetValue(this, Convert.ChangeType(objValue, pi.PropertyType), null);
                            strKeyCoumnName = tmAtrr.ColumnName;
                            break;
                        }
                    }
                }
            }

            type = objForgetKeyEntity.GetType();
            foreach (PropertyInfo pi in  type.GetProperties())
            {
                object[] obj = pi.GetCustomAttributes(typeof(ColumnMapping), false);
                if (obj.Length > 0)
                {
                    if (obj[0] is ColumnMapping)
                    {
                        ColumnMapping tmAtrr = obj[0] as ColumnMapping;
                        if (strKeyCoumnName == tmAtrr.ColumnName)
                        {
                            pi.SetValue(objForgetKeyEntity, Convert.ChangeType(objValue, pi.PropertyType), null);
                            break;
                        }
                    }
                }
            }
        }
예제 #8
0
 /// <summary>
 /// 设置原始值
 /// </summary>
 /// <param name="objEntity">实体对像</param>
 public static void SetOriginalValue(EntityObjectBase objEntity)
 {
     objEntity.table = objEntity.ToTable();
 }