VCS Framework Step by Step (一)

VCSFramework使用教程
本文介绍了如何使用VCSFramework框架进行数据库操作,包括环境搭建、代码生成器使用、以及通过示例展示如何实现数据的增删改查等功能。

由于现在框架数NHibernate最流行,我就以NHibernate为例,介绍VCS Framework的使用方法,本人站在中间立场不评价NHibernate的好坏,如果大家感兴趣,我专门开设一个论坛(http://vcsproject.com/forums/16/ShowForum.aspx),可以放手去评论。

首先讲述一下使用本框架的基本软件环境:

    1、VCS Framework当前的版本是0.9(由于生成器依赖于Codesmith),下载地址:http://www.cnblogs.com/files/luoqing/src.rar
    2、Code Smith http://www.codesmithtools.com/
    3、生成器http://www.cnblogs.com/files/luoqing/Genarated.rar
    4、VS2005
    5、SQL Server2005
    其他下载:http://luoqing.cnblogs.com/archive/2006/04/13/374241.html
本教程只能在SQL Server2005上运行,默认情况下,我将建立并使用一个叫VCS的数据库。

首先,我们先建立一个最简单的Person表,如下完整脚本(你可以进行修改以适合自己的数据库):
当然,由于VCS处理0.9版,所以当前主键只能为Guid类型,并且数据库中有一个SSIndex int IDENTITY(1,1)类型。VCS 1.0在1个月后发布,解决这些问题。

None.gif USE   [ VCS ]
None.gif
GO
None.gif
SET  ANSI_NULLS  ON
None.gif
GO
None.gif
SET  QUOTED_IDENTIFIER  ON
None.gif
GO
None.gif
SET  ANSI_PADDING  ON
None.gif
GO
None.gif
CREATE   TABLE   [ dbo ] . [ Person ] (
None.gif    
[ id ]   [ uniqueidentifier ]   NOT   NULL ,
None.gif    
[ name ]   [ nvarchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS  NOT   NULL ,
None.gif    SSIndex 
int   IDENTITY ( 1 , 1 ),
None.gif 
CONSTRAINT   [ PK_Person ]   PRIMARY   KEY   CLUSTERED  
None.gif(
None.gif    
[ id ]   ASC
None.gif)
WITH  (IGNORE_DUP_KEY  =   OFF ON   [ PRIMARY ]
None.gif
ON   [ PRIMARY ]
None.gif
None.gif
GO
None.gif
SET  ANSI_PADDING  OFF
None.gif

在SQL Server上运行后,下载生成器,打开生成器:
1.GIF
点一下步
3.GIF
把视图名从viewPerson改为Person;并选种Person。
再点两下“下一步”,进入下面页面:
4.GIF
点“生成代码”。
会生成下面的代码。
5.GIF
其中Person.cs(主要用于写业务逻辑,显得清楚)的代码是:

 1 None.gif using  System;
 2 None.gif using  com.Robert.Framework;
 3 None.gif using  Robert.DataService;
 4 None.gif
 5 None.gif
 6 None.gif namespace  com.test
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// 
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public  partial class Person : FrameObject
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif       
14ExpandedSubBlockEnd.gif    }

15ExpandedBlockEnd.gif}

Genarated/Person.cs

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  com.Robert.Framework;
None.gif
None.gif
None.gif
namespace  com.test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [Serializable]
InBlock.gif    
public  partial class Person 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
InBlock.gif        
public Person()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._data = new PersonData();
InBlock.gif
InBlock.gif            
this.id = Guid.NewGuid();
InBlock.gif                                
ExpandedSubBlockEnd.gif        }

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

InBlock.gif            
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Property#region Property
InBlock.gif
InBlock.gif        
InBlock.gif        
public virtual MyGuid id
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn ((PersonData) this.ObjectData).id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{((PersonData) this.ObjectData).id=value;}
ExpandedSubBlockEnd.gif        }

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

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

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

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

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

InBlock.gif                    
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
List#region List
InBlock.gif        
InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
object info#region object info
InBlock.gif        
InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Operation#region Operation
InBlock.gif
InBlock.gif        
public virtual Person Clone()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Person m_obj 
= new Person();
InBlock.gif            m_obj._data 
= this.ObjectData.Clone();
InBlock.gif            
return m_obj;
ExpandedSubBlockEnd.gif        }

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

InBlock.gif        
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
public override bool Equals(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (obj is Person)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.id == ((Person)obj).id;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif        }

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

InBlock.gif                        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

该类主要用于生成,建议最好不用更改,以后可以用XML来重构时,生成器会自动重新生成该类,比如以后加字段或更改字段;
下面是系统生成的XML,该XML在系统的作用不是像NHibernate那样是个配置文件,在本框架中他是一个用于将来要重构的脚本文件(这里暂时先提一下,如果有什么信息,请进入http://vcsproject.com/forums/default.aspx
XML文件:

None.gif < TableInfo  RealObjectName ="Person"  TableName ="Person"  ViewName ="Person"  BaseObjectName =""  IsAbstract ="" >
None.gif  
< Columns >
None.gif    
< Field  Checked ="True"  PropertyName ="id"  ColumnName ="id"  ForeignObjectName =""  ForeignPropertyName =""  IsBaseColumn ="True"  IsPrimaryKeyMember ="True"  DBType ="9"  Size ="16"  IsAbstract =""  IsOverride =""  EnmType =""   />
None.gif    
< Field  Checked ="True"  PropertyName ="name"  ColumnName ="name"  ForeignObjectName =""  ForeignPropertyName =""  IsBaseColumn ="True"  IsPrimaryKeyMember ="False"  DBType ="16"  Size ="50"  IsAbstract =""  IsOverride =""  EnmType =""   />
None.gif    
< Field  Checked ="True"  PropertyName ="SSIndex"  ColumnName ="SSIndex"  ForeignObjectName =""  ForeignPropertyName =""  IsBaseColumn ="True"  IsPrimaryKeyMember ="False"  DBType ="11"  Size ="4"  IsAbstract =""  IsOverride =""  EnmType =""   />
None.gif  
</ Columns >
None.gif  
< Foreigns  />
None.gif
</ TableInfo >
OK, 代码生成完毕;
现在把我的框架打开,新建一个ClassLibrary工程,把生成的代码Copy到新建的工程里,再把com.Robert.Type,com.Robert.Framework,DataService,MyException引用到工程里,再把com.Robert.AccountTestFramework里的ConnectionHelper文件夹Copy到新增的工程里,删除“com.Robert.AccountTestFramework”及“TestFramework”和“TestClass”,由于这三个工程是我在框架里加入的样例代码。

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  Robert.DataService;
 5 None.gif
 6 None.gif namespace  com.EBiz
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif    public class ConnectionHelper
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        public static string GetConnectionString()
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            return "Data Source=TECH-ROBERT;Initial Catalog=ICHUB_Test;User ID=sa;Password=lq430781";
13ExpandedSubBlockEnd.gif        }

14InBlock.gif        public static void BeginTrans()
15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
16InBlock.gif            DataService.DBObject.BeginTrans(GetConnectionString());
17ExpandedSubBlockEnd.gif        }

18InBlock.gif        public static void CommitTrans()
19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
20InBlock.gif            DataService.DBObject.CommitTrans(GetConnectionString());
21ExpandedSubBlockEnd.gif        }

22InBlock.gif        public static void RollTrans()
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            DataService.DBObject.RollTrans(GetConnectionString());
25ExpandedSubBlockEnd.gif        }

26InBlock.gif        public static System.Data.DataTable ExecuteSQL(string sql)
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
28InBlock.gif            return (System.Data.DataTable)DataService.DBObject.ExecuteSql(sql, GetConnectionString());
29ExpandedSubBlockEnd.gif        }

30InBlock.gif        public static void ExecuteNonQuery(string sql)
31ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
32InBlock.gif            DataService.DBObject.ExecuteSql(sql, GetConnectionString());
33ExpandedSubBlockEnd.gif        }

34ExpandedSubBlockEnd.gif    }

35ExpandedBlockEnd.gif}

36 None.gif
GetConnectionString的连接改为你的数据库,就可以了。
好了,现在来解释一下,新增,修改,删除的代码了:
 1 None.gif             Guid m_id = Guid.NewGuid();
 2 None.gif
 3 None.gif             // 新增Person的代码
 4 None.gif             com.test.Person m_newperson  =   new  com.test.Person();
 5 None.gif            m_newperson.id  =  m_id;
 6 None.gif            m_newperson.name  =   " Test " ;
 7 None.gif            m_newperson.Save();
 8 None.gif
 9 None.gif             // 修改Person的代码
10 None.gif             com.test.Person m_edit_person  =   new  com.test.Person(m_id);
11 None.gif            m_edit_person.name  =   " modified " ;
12 None.gif            m_edit_person.Save();
13 None.gif
14 None.gif             // 删除Person的代码
15 None.gif             com.test.Person m_del_person  =   new  com.test.Person(m_id);
16 None.gif            m_del_person.Delete();
简单吧。

如果要得到更详细的信息,请进入 http://vcsproject.com/forums/default.aspx
如果想和NHibernate比较,请进入 http://abluedog.cnblogs.com/archive/2006/04/15/375862.html
请支持中国人的框架

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值