技术篇(5)--QPG数据处理方法

实际开发中一定少不了数据处理。QPG如何简化这个繁杂的工作呢?

QPG最早使用自己的DataMapping,后来因为维护的问题转用了NHibernate;因为集成Castle到平台里,所以后来也尝试使用ActiveRecord.这样就不用编写HBM文件了(Java版有一本好书<<深入浅出Hibernate>>),讲得不错。我是在那里学到高级用法的。

ActiveRecord相关的使用可以看:http://www.castleproject.org/index.php/ActiveRecord:Reference_Documentation
我已经把他下载到demo包的根目录。

为了执行演示,您先下载开发演示文件
1) 建立一个demo1数据库
2) 如果帐号密码不是sa; 空则修改bin\config\sys_config.xml为合适的
<add key="hibernate.connection.connection_string" value="Server=localhost;Database=demo1;Uid=sa;Pwd=" />
3) 运行bon\Demo.ORM.exe 就会自动建立数据表,会把结果放到DataGrid中

由于Blog例子太经典。所以我就直接引用Castle的了。如下:


namespace Demo.ORM.Model
{
    
using System;
    
using System.Collections;

    
using Castle.ActiveRecord;


    [ActiveRecord(
"Blogs")]
    
public class Blog : ActiveRecordBase
    
{
        
private int _id;
        
private String _name;
        
private String _author;
        
private IList _posts;
        
private IList _publishedposts;
        
private IList _unpublishedposts;
        
private IList _recentposts;

        [PrimaryKey(PrimaryKeyType.Native)]
        
public int Id
        
{
            
get return _id; }
            
set { _id = value; }
        }


        [Property]
        
public String Name
        
{
            
get return _name; }
            
set { _name = value; }
        }


        [Property]
        
public String Author
        
{
            
get return _author; }
            
set { _author = value; }
        }


        [HasMany(
typeof(Post), Table="Posts", ColumnKey="blogid")]
        
public IList Posts
        
{
            
get return _posts; }
            
set { _posts = value; }
        }


        [HasMany(
typeof(Post), Table="Posts", ColumnKey="blogid", Where="published = 1")]
        
public IList PublishedPosts
        
{
            
get return _publishedposts; }
            
set { _publishedposts = value; }
        }


        [HasMany(
typeof(Post), Table="Posts", ColumnKey="blogid", Where="published = 0")]
        
public IList UnPublishedPosts
        
{
            
get return _unpublishedposts; }
            
set { _unpublishedposts = value; }
        }


        [HasMany(
typeof(Post), Table="Posts", ColumnKey="blogid", OrderBy="created desc")]
        
public IList RecentPosts
        
{
            
get return _recentposts; }
            
set { _recentposts = value; }
        }


        
public static void DeleteAll()
        
{
            ActiveRecordBase.DeleteAll( 
typeof(Blog) );
        }


        
public static Blog[] FindAll()
        
{
            
return (Blog[]) ActiveRecordBase.FindAll( typeof(Blog) );
        }


        
public static Blog Find(int id)
        
{
            
return (Blog) ActiveRecordBase.FindByPrimaryKey( typeof(Blog), id );
        }

    }

}

更多细节还是读源码吧。
posted @ 2005-10-10 12:34  成为-行动-拥有(BeDoHave)  阅读(1397)  评论(0编辑  收藏  举报