最近开始接触Castle ActiveRecord,学习资料大部分是从网上找到的。这里要特别感谢TerryLee的系列文章:Castle 开发系列 ,在Castle的学习之路上,这个系列文章对我的影响是十分巨大的!除了这个系列文章之外,Castle的官方网站也是学习Castle的好去处!

本篇学习笔记从一个简单对象的CURD操作入手,介绍ActiveRecord!

主要内容:
1.ActiveRecord概述
2.准备数据表
3.编写实体类
4.编写配置文件
5.对象的CRUD操作
6.表示层调用

一、ActiveRecrod概述
  ActiveRecordCastle中提供的一个数据访问框架,它在底层封装了NHibernate的操作。与NHibernate相比,ActiveRecord使用特性来代替映射文件hbm.xml,它提供的简洁的O/R映射会让你惊叹原来实现持久化数据层是那么简单。

二、准备数据表

Active Record学习笔记(一):初步接触Create Table [Users]
Active Record学习笔记(一):初步接触(
Active Record学习笔记(一):初步接触    
[ID] Int Identity(1,1Primary Key,
Active Record学习笔记(一):初步接触    
[LoginName] Varchar(50not null,
Active Record学习笔记(一):初步接触    
[Password] Varchar(20not null
Active Record学习笔记(一):初步接触)

三、编写实体类User
  1.引用Castle.ActiveRecord.dll组件;
  2.引用Castle.ActiveRecord名称空间:
Active Record学习笔记(一):初步接触using Castle.ActiveRecord;

  3.让User类继承ActiveRecordBase类(此类处于Castle.ActiveRecord名称空间之下):
Active Record学习笔记(一):初步接触public class User : ActiveRecord
}

  4.用[ActiveRecrod()]为类添加特性,指出User类对应的数据表是Users:
Active Record学习笔记(一):初步接触[ActiveRecord("Users")]
Active Record学习笔记(一):初步接触
public class User : ActiveRecordBase
}

  5.用[Property()]为属性添加特性,指出属性对应数据表中的列:
Active Record学习笔记(一):初步接触[ActiveRecord("Users")]
Active Record学习笔记(一):初步接触public
class User : ActiveRecordBase
}

四、编写配置文件App.config或web.config。由于ActiveRecord在底层封装了NHibernate,故配置文件的信息和NHibernate一致。
App.config:
Active Record学习笔记(一):初步接触<?xml version="1.0" encoding="utf-8" ?>
Active Record学习笔记(一):初步接触
<configuration>
Active Record学习笔记(一):初步接触    
<configSections>
Active Record学习笔记(一):初步接触        
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
Active Record学习笔记(一):初步接触    
</configSections>
Active Record学习笔记(一):初步接触    
<activerecord>
Active Record学习笔记(一):初步接触        
<config>
Active Record学习笔记(一):初步接触            
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
Active Record学习笔记(一):初步接触            
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
Active Record学习笔记(一):初步接触            
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
Active Record学习笔记(一):初步接触            
<add key="hibernate.connection.connection_string" value="UID=sa;Password=123456789;Initial Catalog=Castle;Data Source=EMMALEE" />
Active Record学习笔记(一):初步接触        
</config>
Active Record学习笔记(一):初步接触    
</activerecord>
Active Record学习笔记(一):初步接触
</configuration>
web.config
Active Record学习笔记(一):初步接触<?xml version="1.0"?>
Active Record学习笔记(一):初步接触
<configuration>
Active Record学习笔记(一):初步接触    
<configSections>
Active Record学习笔记(一):初步接触        
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
Active Record学习笔记(一):初步接触    
</configSections>
Active Record学习笔记(一):初步接触    
<activerecord isWeb="true">
Active Record学习笔记(一):初步接触        
<config>
Active Record学习笔记(一):初步接触            
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
Active Record学习笔记(一):初步接触            
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
Active Record学习笔记(一):初步接触            
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
Active Record学习笔记(一):初步接触            
<add key="hibernate.connection.connection_string" value="UID=sa;Password=123456789;Initial Catalog=Castle;Data Source=EMMALEE" />
Active Record学习笔记(一):初步接触        
</config>
Active Record学习笔记(一):初步接触    
</activerecord>
Active Record学习笔记(一):初步接触    
<system.web>
Active Record学习笔记(一):初步接触        
<compilation debug="true"/>
Active Record学习笔记(一):初步接触        
<authentication mode="Windows"/>
Active Record学习笔记(一):初步接触            
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
Active Record学习笔记(一):初步接触            
<error statusCode="403" redirect="NoAccess.htm" />
Active Record学习笔记(一):初步接触            
<error statusCode="404" redirect="FileNotFound.htm" />
Active Record学习笔记(一):初步接触        
</customErrors>
Active Record学习笔记(一):初步接触    
</system.web>
Active Record学习笔记(一):初步接触
</configuration>
Active Record学习笔记(一):初步接触

五、对象的CRUD操作。类ActiveRecordBas中定义了许多静态方法用于对象的CRUD操作,如:Create、Delete、DeleteAll、FindAll、FindAllByProperty、FindByPrimaryKey、Save等等一些静态方法。
  1.Create操作
Active Record学习笔记(一):初步接触User objUser = new User();
Active Record学习笔记(一):初步接触objUser.Name 
= "jailu";
Active Record学习笔记(一):初步接触objUser.Password 
= "123456789";
Active Record学习笔记(一):初步接触
Active Record学习笔记(一):初步接触objUser.Create();

  2.Read操作
Active Record学习笔记(一):初步接触User objUser = User.Find(1);    //检索主键ID为1的User对象
Active Record学习笔记(一):初步接触
string strName = objUser.Name;
Active Record学习笔记(一):初步接触
string strPassword = objUser.Password;

  3.Update操作
Active Record学习笔记(一):初步接触User objUser = User.Find(1);    //检索主键ID为1的User对象
Active Record学习笔记(一):初步接触
objUser.Name = "EmmaLee";
Active Record学习笔记(一):初步接触objUser.Password 
= "987654321";
Active Record学习笔记(一):初步接触
Active Record学习笔记(一):初步接触objUser.Update();

  4.Delete操作
Active Record学习笔记(一):初步接触 User objUser = User.Find(1);    //检索主键ID为1的User对象
Active Record学习笔记(一):初步接触

Active Record学习笔记(一):初步接触objUser.Delete();

  5.完整的User类代码:

Active Record学习笔记(一):初步接触using System;
Active Record学习笔记(一):初步接触
using System.Collections.Generic;
Active Record学习笔记(一):初步接触
using System.Text;
Active Record学习笔记(一):初步接触
using System.Collections;
Active Record学习笔记(一):初步接触
Active Record学习笔记(一):初步接触
using Castle.ActiveRecord;
Active Record学习笔记(一):初步接触
using Castle.ActiveRecord.Queries;
Active Record学习笔记(一):初步接触
Active Record学习笔记(一):初步接触
namespace CastleTest

六、表示层调用:
Active Record学习笔记(一):初步接触private void button1_Click(object sender, EventArgs e)
}

至此,ActiveRecord的初步接触就算是完成了。

相关文章: