随着Castle1.0 RC2版本的发布,Castle逐渐走向成熟和趋于稳定,打算在近期写几篇关于AR的Hands On Lab。本篇文章将会介绍AR中最基本的数据访问,包括数据简单筛选和模糊查询等。在开始本Hands On Lab之前,请先下载并安装Castle 1.0 RC2。在整个Hands On Lab中,我们将使用Northwind数据库作为示例,基于.NET 2.0。
简单数据访问
数据筛选
模糊查询
……
随着Castle1.0 RC2版本的发布,Castle逐渐走向成熟和趋于稳定,打算在近期写几篇关于AR的Hands On Lab。本篇文章将会介绍AR中最基本的数据访问,包括数据简单筛选和模糊查询等。在开始本Hands On Lab之前,请先下载并安装Castle 1.0 RC2。在整个Hands On Lab中,我们将使用Northwind数据库作为示例,开发环境为VS2005。
1.新建Castle ActiveRecord 项目,安装Castle之后会在新建项目对话框中看到Castle ActiveRecord项目类型,输入项目名Models。
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_001.gif)
在生成一个Models项目的同时,也为我们生成了一个Models.Test的单元测试项目,这里我们不用测试驱动的方法,直接删除该测试项目就可以了。
2.在Models项目中添加Customer实体类,并引入命名空间:
using Castle.ActiveRecord;
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_007.gif)
完成后Customer实体类代码如下:
[ActiveRecord("Customers")]
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class Customer : ActiveRecordBase<Customer>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _customerID;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _companyName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _contactName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _contactTitle;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _address;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _city;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _region;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _postalCode;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _country;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _phone;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private String _fax;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[PrimaryKey(PrimaryKeyType.Assigned)]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String CustomerID
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _customerID; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _customerID = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String CompanyName
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _companyName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _companyName = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String ContactName
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _contactName; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _contactName = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String ContactTitle
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _contactTitle; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _contactTitle = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Address
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _address; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _address = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String City
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _city; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _city = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Region
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _region; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _region = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String PostalCode
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _postalCode; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _postalCode = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Country
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _country; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _country = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Phone
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _phone; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _phone = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Property]
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public String Fax
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _fax; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _fax = value; }
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
用ActiveRecord特性来指定表名为Customers。
3.新建Web Site,删除默认的Default.aspx页面,添加一个Master页面,在页面加入如下元素:
<div>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<h2>Castle ActiveRecord Hands On Lab</h2>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:contentplaceholder>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</div>
4.添加ASPX页面,并选择刚才的Master页面作为它的母版页,在ASPX页面中添加一个GridView控件,如下所示:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_002.gif)
在GridView中添加绑定列,最终代码如下:
<asp:GridView ID="GridView1" runat="server" Width="610px" AutoGenerateColumns="false">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Columns>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="Country" HeaderText="Country" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:BoundField DataField="Phone" HeaderText="Phone" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Columns>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:GridView>
5.编写绑定代码,在Page_Load中加入GridView的绑定代码。
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
this.GridView1.DataSource = Customer.FindAll();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataBind();
}
这里使用了静态的FindAll()方法,该方法已经在泛型的ActiveRecordBase中作了定义,不需要再在实体类中实现。
6.编写配置文件,在Web项目中添加Web.config文件,在Web.config中添加ActiveRecord配置区。
<configSections>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</configSections>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<connectionStrings>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add name="NorthWind" connectionString="Data Source=1DD43B143D104DB;Initial Catalog=Northwind;User ID=sa;Password=sa"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</connectionStrings>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<activerecord isWeb="true">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<config>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add key="hibernate.connection.driver class" value="NHibernate.Driver.SqlClientDriver"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<add key="hibernate.connection.connection_string" value="ConnectionString = ${NorthWind}"/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</config>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</activerecord>
由于是Web项目,这里ActiveRecord配置区的isWeb属性必须设置为true。
7.初始化ActiveRecord实体类。在Web Site中添加Global.asax,并导入命名空间:
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Import Namespace="Models" %>
在Application_Start中添加初始化代码:
void Application_Start(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Castle.ActiveRecord.Framework.IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as Castle.ActiveRecord.Framework.IConfigurationSource;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Castle.ActiveRecord.ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Models"),source);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
这里可以对单个实体类进行初始化或者加载整个程序集。
8.至此,一个最简单的基于ActiveRecord的数据查询就做完了,运行后如下:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_003.gif)
9.简单的数据筛选,在ActiveRecordBase基类中提供了一个方法FindAllByProperty,可以使用属性进行查询,如我们只查询国家为“UK”的Customer,在Page_Load中修改绑定代码如下:
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataSource = Customer.FindAllByProperty("Country","UK");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
运行后如下:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_004.gif)
10.使用NHibernate的表达式来进行数据筛选。首先引入命名空间:
using NHibernate.Expression;
在Page_Load中修改绑定代码如下所示,我们仍然查询国家为“UK”的Customer。
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataSource = Customer.FindAll(Expression.Eq("Country", "UK"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
运行后与上面使用FindAllByProperty()方法的查询结果一致。
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_004.gif)
11.实现简单的模糊匹配查询。在Default.aspx页面中添加一个Label、TextBox、Button控件,如下图所示,这里我们对ContactName添加模糊查询功能:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_006.gif)
HTML代码:
<asp:Label ID="Label1" runat="server" Text="ContactName:"></asp:Label>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:TextBox ID="txt_Search" runat="server" Width="242px"></asp:TextBox>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Button ID="btn_Search" runat="server" OnClick="btn_Search_Click" Text="Search" />
切换到设计视图,双击Search按钮,添加Click事件,并编写代码,这里用HQL语句来实现,关于HQL语句可以参考http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html。完成后代码如下:
protected void btn_Search_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
IActiveRecordQuery query = new HqlBasedQuery(typeof(Customer), "from Customer c where c.ContactName like ?", "%" + this.txt_Search.Text + "%");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataSource = ActiveRecordMediator.ExecuteQuery(query);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.GridView1.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
运行后在文本框中输入Mar,可以看到结果如下:
![](https://www.cnblogs.com/images/cnblogs_com/terrylee/ARHandsOnLab_006.gif)
本文简单的介绍到这里。
示例下载:https://files.cnblogs.com/Terrylee/ARHandsOnLab.rar
更多Castle的文章请访问《Castle 开发系列》