Dev.Hong
将欲取之 必先予之……
随笔- 53  文章- 0  评论- 220 
博客园  首页  新随笔  联系  管理  订阅 订阅
Gentle.NET 2.0

Gentle.NET自从发布1.2.9以来已经很久没新版本出来了,并且作者说不会再对Gentle.NET进行任何更新,而现在的一个项目是
采用.Net2.0来做的(太落伍了呵),所以只有自已动手了(有源码就是方便)。虽说是升级到.Net2.0下,其实我也就是
把源码拿到vs2005下进行了一下(不会受到鄙视吧),改了其中几个已过时的方法,编绎通过。


除了拿到vs2005下进行了编译,我加入几个功能进行,如下:

1: CBO: 位于 Gentle.Common.Utility下。看到这个名字,相信对于读过DotNetNuke的人应该很熟悉吧,没错,就是它,它的

功能就是实现从数据库中读取数据并实例化自定义业务对象(Custom Business Object)(简称CBO)。

2: SqlHelper:位于Gentle.Framework.Core下。和ms提供的那个SqlHelper类似,主要用于解决在调用存储过程时不需要手动添

加存储过程参数,它会根据传入的存储过程名称自动填充。

3: 在PersistenceBroker类中(位于Gentle.Framework.Core下)加入了ExecuteNonQuery和ExecuteReader重载方法,主要是调用
SqlHelper方法用于执行一个存储过程或Sql语句。

4: 在GentleSqlFactory基类中(位于Gentle.Framework.Provider下)加入了DiscoverSpParameterSet方法,用于获取指定存
储过程的参数列表,然后在SQLServerFactory和OracleFactory进行了实现。

5: 在ObjectFactory(位于Gentle.Framework.Core.TypeConstruction下)中加入了GetObject和GetObjectCollection重载方法

,调用CBO的FillObject或FillCollection方法用指定的数据和类型生成对象或集合,跟Gentle中的GetInstance与

GetCollection方法类似。

6: 在Transaction(在Gentle.Framework.Client下)中添加了在PersistenceBroker类中新增方法的事务支持,使用方法和原先
使用一样。

以下为简单的使用示例

首先用CodeSmith和提供的模板生成继承自Persistent的实体类

User
  1using System;
  2using Gentle.Framework;
  3
  4namespace HM21Doc
  5{    
  6    /**//// <summary>
  7    /// TODO add description of User here.
  8    /// </summary>

  9    [Serializable]
 10    [TableName("Users")]
 11    public class User : Persistent
 12    {
 13        Private member data#region Private member data
 14        /**////<summary>
 15        /// m_userID accesses the UserID column of the Users table.
 16        ///</summary>

 17        [TableColumn("UserID", NotNull=true), PrimaryKey]
 18        protected string m_userID = String.Empty;
 19        /**////<summary>
 20        /// m_userName accesses the UserName column of the Users table.
 21        ///</summary>

 22        [TableColumn("UserName")]
 23        protected string m_userName = String.Empty;
 24        /**////<summary>
 25        /// m_password accesses the Password column of the Users table.
 26        ///</summary>

 27        [TableColumn("Password")]
 28        protected string m_password = String.Empty;
 29        /**////<summary>
 30        /// m_sex accesses the Sex column of the Users table.
 31        ///</summary>

 32        [TableColumn("Sex")]
 33        protected bool m_sex;
 34        /**////<summary>
 35        /// m_joinDate accesses the JoinDate column of the Users table.
 36        ///</summary>

 37        [TableColumn("JoinDate")]
 38        protected DateTime m_joinDate;
 39        #endregion

 40
 41        Constructors#region Constructors
 42        /**//// <summary>
 43        /// Create  User from existing/full data set (used by the data layer).
 44        /// </summary>

 45        public User() {}        
 46        /**//// <summary>
 47        /// Create  User from existing/full data set (used by the data layer).
 48        /// </summary>

 49        public User( string userID, string userName, string password, bool sex, DateTime joinDate ) 
 50        {
 51            this.m_userID = userID;
 52            this.m_userName = userName;
 53            this.m_password = password;
 54            this.m_sex = sex;
 55            this.m_joinDate = joinDate;
 56
 57        }

 58        /**//// <summary>
 59        /// Select an existing User given its unique identifier
 60        /// </summary>

 61        static public User Retrieve( string m_userID )
 62        {
 63            Key key = new Key( typeof(User), true, "m_userID", m_userID );
 64            try
 65            {
 66                return Broker.RetrieveInstance( typeof(User), key ) as User;
 67            }

 68            catch( Gentle.Common.GentleException ex )
 69            {
 70                if ( ex.Error == Gentle.Common.Error.UnexpectedRowCount )
 71                    return null;
 72                else
 73                    throw;
 74            }

 75        }
        
 76        /**//// <summary>
 77        /// 保存
 78        /// </summary>

 79        public void Save()
 80        {
 81            this.Persist();
 82        }

 83        /**//// <summary>
 84        /// 更新
 85        /// </summary>

 86        public void Update()
 87        {
 88            this.IsPersisted = true;
 89            this.Persist();
 90        }

 91        /**//// <summary>
 92        /// 删除 
 93        /// </summary>

 94        public void Delete()
 95        {
 96            this.IsPersisted = true;
 97            this.Remove();
 98        }

 99        #endregion

100        
101        Public Properties#region Public Properties
102        /**////<summary>
103        /// UserID accesses the UserID column of the Users table.
104        ///</summary>

105        public string UserID 
106        {
107            get { return m_userID; }            
108            set { m_userID = value; }        
109        }
        
110        /**////<summary>
111        /// UserName accesses the UserName column of the Users table.
112        ///</summary>

113        public string UserName 
114        {
115            get { return m_userName; }            
116            set { m_userName = value; }        
117        }
        
118        /**////<summary>
119        /// Password accesses the Password column of the Users table.
120        ///</summary>

121        public string Password 
122        {
123            get { return m_password; }            
124            set { m_password = value; }        
125        }
        
126        /**////<summary>
127        /// Sex accesses the Sex column of the Users table.
128        ///</summary>

129        public bool Sex 
130        {
131            get { return m_sex; }            
132            set { m_sex = value; }        
133        }
        
134        /**////<summary>
135        /// JoinDate accesses the JoinDate column of the Users table.
136        ///</summary>

137        public DateTime JoinDate 
138        {
139            get { return m_joinDate; }            
140            set { m_joinDate = value; }        
141        }
        
142        #endregion

143    }
    
144}

145

//执行自定义的Sql语句,并返回结果

1 //获取男性用户列表
2  List<Users> list = new List<Users>();
3  ObjectFactory.GetObjectCollection(typeof(Users), Broker.SessionBroker.ExecuteReader("Select * From Users Where 
4 
5 Sex = 1"), list);
6  return list;

//执行存储过程,并返回结果
1 ALTER PROCEDURE GetUsersBySex 
2 
3 @Sex bit
4 
5 AS
6 
7 Select * From Users Where Sex = @Sex
8 
9 RETURN

1 //获取男性用户列表
2  List<Users> list = new List<Users>();
3  //存储过程参数值以数组的形式传递,但要确认其顺序和存储过程参数一致
4  ObjectFactory.GetObjectCollection(typeof(Users), Broker.SessionBroker.ExecuteReader("GetUsersBySex", new 
5 
6 object[] { true }), list);
7  return list;

//事务演示

 1 Transaction t = new Transaction();
 2             try
 3             {
 4                 t.ExecuteNonQuery("Delete From Users Where UserName = '张三'");
 5                 t.ExecuteNonQuery("Delete From Users Where UserName = '李四'");
 6                 //提交事务
 7                 t.Commit();
 8             }
 9             catch
10             {
11                 //回滚
12                 t.Rollback();
13                 throw;
14             }
15             finally
16             {
17                 t.Dispose();
18             }


关于其它的使用方法我就不介绍了,大家可以看它的测试代码,或者搜索园子里其他朋友写的文章,以下是其中一篇,比较全

http://www.cnblogs.com/surfsky/archive/2006/06/29/438762.html

请点击此处下载Gentle.Net.2.0 Source
posted on 2007-05-17 15:24 Dev.Hong 阅读(4458) 评论(11) 编辑 收藏
刷新评论刷新页面返回顶部
程序员问答社区,解决您的IT难题
博客园首页博问新闻闪存程序员招聘知识库
Copyright ©2012 Dev.Hong