VCS Framework(O/R Mapping) Step by Step (二) 简单维护

本文介绍了当数据库需求变更时,如何使用VCSFramework通过两种方法更新对应的代码类:重新生成类与在原有基础上扩充。文章详细展示了在已有类的基础上添加新字段的具体步骤。

承上一篇:VCS Framework Step by Step (一) 
在练习时前,请下载最新的生成器

    大家做程式的都知道,需求是天天在变动的,需求变化不光是影响业务的变化,还可能引起数据库的变化,字段的新增,修改,删除,怎么办呢,现在给你介绍使用VCS Framework应付这种情况所做的工作。

1、重新生成类;
2、在原有的基础进行扩充。

我们以上一篇生成的类做样例。
如果需求引起数据库更改,比如加了一列:Six bit
怎样处理呢?

首先在数据库上执行下面这个SQL 脚本:

None.gif alter   table  Person  add   Six  bit   null


处理方式有两种:
1、重新生成类:
    这种方式只有适用在没有做任何重构或1-N处理时是最方便的,因为只是简单的处理一下(处理方式和上篇一样重新来过就行了)。
2、在原有的基础进行扩充。
    这才是本篇要介绍的;由于重构或者1-N处理后(后续篇将详细介绍),类就会变得比较复杂。而重新生成的话,可能会忘了这忘了那的,变得非常复杂。

首先打开代码生成器
如下图:
b1.GIF
选取“从XML文件中加载”,“XML文件夹”选取你工程里自动生成的XML文件夹即可:
再点“下一步”进入下一页
b2.GIF
选种Person.xml点“下一步”进入下图:
b5.GIF
先选种Person后,生成器会自动加载原来的属性,再在原来的基础上新增一列Six,如上图下面红色标志所示;
先介绍一下上面各列的用途:
属性名:即将来生成类的属性;
列名:即表的列名;
关联类名、关联对象属性名、自定义权举类型在后续篇再讲
类型:是指数据库的类型;
表列:由于该框架是以表为基础,以视图为扩展,所以要标明该列是属于视图还是属于表的。


完成上述动作后,点下一步/下一步,进行生成代码区;如下图:
b6.GIF
“代码文件夹”,“XML文件夹”可以直接选取工程里的文件夹及工程里XML文件所在的文件夹即可,再点生成代码;就完成了一个简单的维护。

生成的代码如图:
Genarated/Person.cs

  1 None.gif
  2 ExpandedBlockStart.gifContractedBlock.gif /**/ /*
  3InBlock.gif*********************************************************
  4InBlock.gif*    璇存槑锛氱敱VCSFramework鐢熸垚鍣ㄧ敓鎴?                     *
  5InBlock.gif*   濡傛灉鏈変粈涔堥棶棰橈紝璇疯仈绯籖obert_luoqing@hotmail.com    *
  6InBlock.gif*********************************************************
  7ExpandedBlockEnd.gif*/

  8 None.gif using  System;
  9 None.gif using  System.Data;
 10 None.gif using  com.Robert.Framework;
 11 None.gif
 12 None.gif
 13 None.gif namespace  com.test
 14 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 15ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 16InBlock.gif    /// 
 17ExpandedSubBlockEnd.gif    /// </summary>

 18InBlock.gif    [Serializable]
 19InBlock.gif    public  partial class Person 
 20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 21InBlock.gif        
 22InBlock.gif        public Person()
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            this._data = new PersonData();
 25InBlock.gif
 26InBlock.gif            this.id = Guid.NewGuid();
 27InBlock.gif                                
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30InBlock.gif        public Person(Guid ID)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 32InBlock.gif            this._data = new PersonManager().GetPersonByID(ID)._data;
 33ExpandedSubBlockEnd.gif        }

 34InBlock.gif            
 35InBlock.gif
 36ContractedSubBlock.gifExpandedSubBlockStart.gif        Property#region Property
 37InBlock.gif
 38InBlock.gif        
 39InBlock.gif        public virtual MyGuid id
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).id; }
 42ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{((PersonData) this.ObjectData).id=value;}
 43ExpandedSubBlockEnd.gif        }

 44InBlock.gif
 45InBlock.gif        public virtual MyGuid Oldid
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).Oldid; }
 48ExpandedSubBlockEnd.gif        }

 49InBlock.gif                    
 50InBlock.gif        public virtual string name
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).name; }
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{((PersonData) this.ObjectData).name=value;}
 54ExpandedSubBlockEnd.gif        }

 55InBlock.gif
 56InBlock.gif        public virtual string Oldname
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).Oldname; }
 59ExpandedSubBlockEnd.gif        }

 60InBlock.gif                    
 61InBlock.gif        public virtual MyInt SSIndex
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).SSIndex; }
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{((PersonData) this.ObjectData).SSIndex=value;}
 65ExpandedSubBlockEnd.gif        }

 66InBlock.gif
 67InBlock.gif        public virtual MyInt OldSSIndex
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).OldSSIndex; }
 70ExpandedSubBlockEnd.gif        }

 71InBlock.gif                    
 72InBlock.gif        public virtual MyBool Six
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).Six; }
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{((PersonData) this.ObjectData).Six=value;}
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78InBlock.gif        public virtual MyBool OldSix
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn ((PersonData) this.ObjectData).OldSix; }
 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif                    
 83InBlock.gif
 84ContractedSubBlock.gifExpandedSubBlockStart.gif        List#region List
 85InBlock.gif        
 86InBlock.gif        
 87InBlock.gif
 88ExpandedSubBlockEnd.gif        #endregion

 89InBlock.gif
 90ContractedSubBlock.gifExpandedSubBlockStart.gif        object info#region object info
 91InBlock.gif        
 92InBlock.gif        
 93InBlock.gif
 94ExpandedSubBlockEnd.gif        #endregion

 95InBlock.gif
 96ExpandedSubBlockEnd.gif        #endregion

 97InBlock.gif
 98InBlock.gif        
 99ContractedSubBlock.gifExpandedSubBlockStart.gif        Operation#region Operation
100InBlock.gif
101InBlock.gif        public virtual Person Clone()
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103InBlock.gif            Person m_obj = new Person();
104InBlock.gif            m_obj._data = this.ObjectData.Clone();
105InBlock.gif            return m_obj;
106ExpandedSubBlockEnd.gif        }

107InBlock.gif
108InBlock.gif        protected override BaseProfile GetProfile()
109ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
110InBlock.gif            return new PersonProfile(this);
111ExpandedSubBlockEnd.gif        }

112InBlock.gif        
113InBlock.gif
114ExpandedSubBlockEnd.gif        #endregion

115InBlock.gif
116InBlock.gif        public override bool Equals(object obj)
117ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
118InBlock.gif            if (obj is Person)
119ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
120InBlock.gif                return this.id == ((Person)obj).id;
121ExpandedSubBlockEnd.gif            }

122InBlock.gif            else
123ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
124InBlock.gif                return base.Equals(obj);
125ExpandedSubBlockEnd.gif            }

126ExpandedSubBlockEnd.gif        }

127InBlock.gif        public override int GetHashCode()
128ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
129InBlock.gif            return base.GetHashCode();
130ExpandedSubBlockEnd.gif        }

131InBlock.gif                        
132ExpandedSubBlockEnd.gif    }

133ExpandedBlockEnd.gif}

134 None.gif



下篇:日志处理。
框架源代码下载地址:http://luoqing.cnblogs.com/archive/2006/04/13/374241.html

转载于:https://www.cnblogs.com/luoqing/archive/2006/04/16/376603.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值