现在的位置: 首页 > 综合 > 正文

我的ActiveRecord学习之路(一)

2011年06月13日 ⁄ 综合 ⁄ 共 4009字 ⁄ 字号 评论关闭

     写这篇文章之前,我想先谢谢TerryLee的文章,不是我吹捧他,他的文章写的真的很好,正因为如此,我才对AR产生了很大的兴趣并坚持一直研究下去,我写以下的文章的目的主要是把我在测试AR例子是碰到的一些问题及一些心得拿出来与大家共享.    

     最近一直在看Castle里的ActiveRecord(以下简称AR),因为之前一直在用WebSharp和IBatisNet,并且总觉得它的灵活性和易操作性不如IBatisNet,比如对多表的操作,复杂查询以及调用存储过程.但对于一些数据关系不是很复杂的项目来说,我觉得AR还是一个不错的选择.

   用过NHibernate和IBatisNet的人一定对编写XML的映射文件印象很深刻吧,尽管可以自动生成,但总是会让我感觉很麻烦,而在AR中则不需要那样的影射文件了,它在底层封装了NHibernate的操作,使用特性(Attributes)来代替映射文件,这种简洁的O/R映射正是它吸引我的因素之一.

一、首先我们先来看一下如何构建AR的配置信息,如下:

      在Web.Config文件中加入以下节点:

<configSections> <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" /> </configSections> <activerecord isWeb="true"> <config> <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" /> <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" /> <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" /> <add key="hibernate.connection.connection_string" value="UID=sa;Password=;Initial Catalog=ActiveRecord;Data Source=." /> </config> </activerecord>

用过NHibernate的人肯定对这段配置文件很熟悉,因为AR在底层封装了NHibernate,所以必须有这样的配置信息。isWeb="true"是针对Web项目才要加上的。

配置文件写好之后,我们再来看看如何在代码中初始化,我的做法是在Global.asax.cs 中的 Application_Start 方法中加入如下代码:

protected void Application_Start(Object sender, EventArgs e) { IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource; ActiveRecordStarter.Initialize( source,typeof(Company)); }

当然我们实际使用中肯定不是一个实体,这个我也只是用了个笨办法,哪位有更好的方法告诉我一下,先谢了,代码如下:

protected void Application_Start(Object sender, EventArgs e) { IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource; Type[] paramTypes = new Type[2]; paramTypes[0] = typeof(Company); paramTypes[1] = typeof(People); ActiveRecordStarter.Initialize( source,paramTypes); }

二、编写AR实体类

1、数据库表

我把我练习时的数据库表结构贴上来,以后我所举的例子也都是围绕这几张表来进行的,如下:

/************************************************************************************ * * Many to Many of ActiveRecord * by pw 2006.05.20 * ************************************************************************************/ --建库 create database ActiveRecord go --1.部门表 create table companies ( id int identity not null, pid int null, cname varchar(50) null, type varchar(20) null, primary key(id) ) go --2.员工表 create table people ( id int identity not null, pname varchar(20) null, primary key(id) ) go --3.部门员工对照表 create table people_companies ( people_id int not null, company_id int not null ) go

实体类代码如下:

using System; using System.Collections; using Castle.ActiveRecord; namespace AR.Model { /// <summary> /// Company 的摘要说明。 /// </summary> [ActiveRecord("companies")] public class Company : Castle.ActiveRecord.ActiveRecordBase { private int _id; private int _pid; private string _cname; private string _type; public Company() { } [PrimaryKey] public int Id { get{return _id;} set{_id = value;} } [Property] public int Pid { get{return _pid;} set{_pid = value;} } [Property] public string Cname { get{return _cname;} set{_cname = value;} } [Property] public string Type { get{return _type;} set{_type = value;} }         public static void DeleteAll() { ActiveRecordBase.DeleteAll(typeof(Company)); } public static Company[] FindAll() { return ((Company[])(ActiveRecordBase.FindAll(typeof(Company)))); } public static Company Find(int id) { return ((Company)(ActiveRecordBase.FindByPrimaryKey(typeof(Company), id))); }
   }
}

当然像这样的实体类的代码是不需要我们一个个的写的,AR提供了自动生成的工具,当然会CodeSmith的人可以自己写一个更方便的。以后我们在配置One to Many 、Many to Many 等关系时需要自己手工修改实体类的代码。

三、单表操作

  有了上面的基础,我们现在可以进行一些简单的对数据库的操作了。

1、ADD

public void Add() {    Company company = new Company();    company.Cname = "公司名称";    company.Type = "民营";    company.Create(); }

2、Select

public Company SimpleSelect() {    //查询指定ID的对象    int id = 1;    return Company.Find(1);    //查询所有的Company对象,返回一个Company对象的集合(Company[])    //Company.FindAll(); }

3、Update

public void UpdateCompany() {    //首先得到一个Company对象    Company com = Company.Find(1);    com.Cname = "修改后的信息";    com.Type = "国有";    com.Update(); }

4、Delete

public void Delete() {    //删除指定的对象    int id = 1;    Company.Delete();    //删除所有    Company.DeleteAll(); }

这样以来我们对AR就有了一个初步的认识了,它的操作和NHibernate一样简洁,而且在配置映射关系方面上比其他几种持久层框架要更简便。今天我先说到这,后面的下次再说吧.

我测试的时候没有用NUnit,所有代码都在VS2003下执行过了,应该没问题.

抱歉!评论已关闭.