Castle ActiveRecord快速入门指南

上传人:飞*** 文档编号:32965142 上传时间:2018-02-13 格式:DOC 页数:31 大小:603.50KB
返回 下载 相关 举报
Castle ActiveRecord快速入门指南_第1页
第1页 / 共31页
Castle ActiveRecord快速入门指南_第2页
第2页 / 共31页
Castle ActiveRecord快速入门指南_第3页
第3页 / 共31页
Castle ActiveRecord快速入门指南_第4页
第4页 / 共31页
Castle ActiveRecord快速入门指南_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《Castle ActiveRecord快速入门指南》由会员分享,可在线阅读,更多相关《Castle ActiveRecord快速入门指南(31页珍藏版)》请在金锄头文库上搜索。

1、主要内容1概述2准备相关的数据表3编写 User 实体类4构建配置信息5开始 CRUD 操作6使用 ActiveRecord Generator 生成实体类代码一概述如果你用过 NHibernate,一定会对在 NHibernate 中编写.hbm.xml 文件印象深刻,我也是。而在 Castle ActiveRecord 中,我们不用再为编写繁冗复杂的映射文件而头疼,ActiveRecord 是 Castle 中提供的一个数据访问框架,它在底层封装了 NHibernate 的操作,使用特性来代替映射文件,它提供的简洁的 O/R 映射会让你惊叹原来实现持久化数据层是那么简单。下面我们通过一个简

2、单对象的 CRUD 操作来快速进入 Castle ActiveRecord。二准备相关的数据表假定数据库中有这样一张用户表,用来保存用户的信息,如下CREATE TABLE dbo.Users (LogonID int IDENTITY (1, 1) NOT NULL ,LogonName varchar (40) COLLATE Chinese_PRC_CI_AS NULL ,Password varchar (20) COLLATEChinese_PRC_CI_AS NULL ,EmailAddress varchar (40) COLLATE Chinese_PRC_CI_AS NULL

3、 ,LastLogon datetime NULL ) ON PRIMARYGO三编写 User 实体类首先我们新建一个 User 类并让它继承于 ActiveRecordBase 类public class User : ActiveRecordBase /为 User 类添加特性,其实就是告诉 ActiveRecord,User 类所对应的数据库中的数据表名为 UsersActiveRecord(Users)public class User : ActiveRecordBase/下面我们的工作就是为实体类添加属性ActiveRecord(Users)public class User :

4、 ActiveRecordBaseprivate int _id;private string _name; private string _password;private string _emailAddress;private DateTime _lastLogon;PrimaryKey(PrimaryKeyType.Identity, LogonID)public int Idget return _id; set _id = value; Property(LogonName)public string Nameget return _name; set _name = value;

5、 Property(Password) public string Passwordget return _password; set _password = value; Property(EmailAddress)public string Addressget return _emailAddress; set _emailAddress = value;Property(LastLogon)public DateTime LastLogonget return _lastLogon; set _lastLogon = value; 大家可能注意到了,每一个属性上面都加上了特性Prope

6、rty()。简单的说明一下,这里用PrimaryKey特性指定 Id 作为主键,并且说明了主键的类型为自增型的,用 PrimaryKeyType.Identity 来说明,在后续文章中我会详细说明的。如果属性名和字段名一致,Property()中可以为空,也可以写上字段的名字。下一步我们为实体类根据需要加上静态的操作方法,至于 Create(),Update(),Delete(),Save()等方法则会直接从 ActiveRecordBase 基类中继承ActiveRecord(Users)public class User : ActiveRecordBase/public static v

7、oid DeleteAll()DeleteAll( typeof(User) ); public static IList FindAll()return (IList) FindAll( typeof(User) );public static User Find(int id)return (User) FindByPrimaryKey( typeof(User), id );整个完成后的实体类代码using System;using System.Collections;using Castle.ActiveRecord;namespace ARDemo/ / User 的摘要说明。/

8、ActiveRecord(Users)public class User : ActiveRecordBaseprivate int _id;private string _name;private string _password;private string _emailAddress;private DateTime _lastLogon;PrimaryKey(PrimaryKeyType.Identity, LogonID)public int Idget return _id; set _id = value; Property(LogonName)public string Nam

9、eget return _name; set _name = value; Property(Password)public string Passwordget return _password; set _password = value;Property(EmailAddress)public string Addressget return _emailAddress; set _emailAddress = value; Property(LastLogon)public DateTime LastLogonget return _lastLogon;set _lastLogon =

10、 value;public static void DeleteAll()DeleteAll( typeof(User) );public static IList FindAll()return (IList) FindAll( typeof(User) ); public static User Find(int id)return (User) FindByPrimaryKey( typeof(User), id );四构建配置信息现在我们要告诉 ActiveRecord 相关的数据库、数据驱动等信息,最简单的就是使用配置文件用过 NHibernate 的朋友一定会对这段配置代码很熟悉,

11、没错,因为 ActiveRecord 在底层封装了 NHibernate,所以这里的配置跟使用 NHibernate 时的配置一样,同样是指定了数据源驱动,连接字符串等信息。如果使用了配置文件在代码中只要这样去初始化就可以了IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig(activerecord) as IConfigurationSource;ActiveRecordStarter.Initialize( source, typeof(User) );我们也可以不使用配置文件

12、,而使用代码指定的方式,但是由于这种方式相当于硬编码了,不大推荐大家使用这种方式:InPlaceConfigurationSource source = new InPlaceConfigurationSource();Hashtable properties = new Hashtable();properties.Add(hibernate.connection.driver_class, NHibernate.Driver.SqlClientDriver);properties.Add(hibernate.dialect, NHibernate.Dialect.MsSql2000Dial

13、ect);properties.Add(hibernate.connection.provider, NHibernate.Connection.DriverConnectionProvider);properties.Add(hibernate.connection.connection_string, UID=sa;Password=19811218;Initial Catalog=ARDemo;Data Source=.);source.Add( typeof(ActiveRecordBase),properties );ActiveRecordStarter.Initialize( s

14、ource, typeof(User) );五开始 CRUD 操作好了,经过了前面的步骤之后,就可以正式开始我们的对象 CRUD 操作了。1增加 User 对象Testpublic void AddUser()User user = new User();user.Name = Terrylee;user.Password = aaa;user.Address = lhj_;user.LastLogon = DateTime.Now;user.Create();是不是非常简单?我们甚至都没有写过 Create()方法,它直接从 ActiveRecordBase类继承。我们所做的只是创建这样一个

15、 User 对象,然后调用它的方法就可以了。2查询所有的 User 对象Testpublic void FildAll() IList list = User.FindAll();Assert.IsNotNull(list);int actual = list.Count;int expected = 2;Assert.AreEqual(expected,actual);3查询某一个指定 Id 的 User 对象Testpublic void Fild()int id = 5;User actual = User.Find(id);Assert.IsNotNull(actual);Assert.AreEqual(Terrylee,actual.Name);Assert.AreEqual(aaa,actual.Password);4修改 User 对象Testpublic void UpdateUser()

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号