一.創建Web工程
創建一個Web站點或者Web應用程序,添加對Castle.ActiveRecord.dll的引用。
二.創建需要持久化的業務實體
在.NET2.0下,由於引入了泛型,創建業務實體比1.1下簡單了許多,業務實體只需要繼承於泛型的ActiveRecordBase類,其中默認已經實現了一些靜態的方法,不需要我們再在業務實體中實現。
[ActiveRecord("Employees")]
public class Employee : ActiveRecordBase<Employee>
{
private string employeeID;
private string lastName;
private string city;
private string address;
private string homePhone;
private string country;
[PrimaryKey(PrimaryKeyType.Assigned)]
public string EmployeeID
{
get { return employeeID; }
set { employeeID = value; }
}
[Property]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
[Property]
public string City
{
get { return city; }
set { city = value; }
}
[Property]
public string Address
{
get { return address; }
set { address = value; }
}
[Property]
public string HomePhone
{
get { return homePhone; }
set { homePhone = value; }
}
[Property]
public string Country
{
get { return country; }
set { country = value; }
}
}
三.設置配置信息
在Web.config中設置如下信息,這部分與1.1沒有什麼區別
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
</configSections>
<connectionStrings>
<add name="NorthWind" connectionString="Data Source=RJ-097;Initial Catalog=Northwind;User ID=sa;Password=sa"/>
</connectionStrings>
<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="ConnectionString = ${NorthWind}"/>
</config>
</activerecord>
</configuration>
四.初始化ActiveRecord
在Global.asax的Application_Start添加初始化代碼
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Castle.ActiveRecord.Framework.IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as Castle.ActiveRecord.Framework.IConfigurationSource;
Castle.ActiveRecord.ActiveRecordStarter.Initialize(typeof(Employee).Assembly, source);
}
五.使用業務實體
這部分也是與1.1一樣,同樣可以使用Create(),Save(),Update()等方法,不詳細說了,這裡我們用一個GridView來展示讀取國家為UK的員工列表
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<html>
<head runat="server">
<title>Castle Active Record for 2.0快速入門示例</title>
</head>
<body>
<form id="form1" runat="server">
<h1>Castle Active Record for 2.0快速入門示例</h1>
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="EmployeeID" />
<asp:BoundField HeaderText="LastName" DataField="LastName" />
<asp:BoundField HeaderText="City" DataField="City" />
<asp:BoundField HeaderText="Address" DataField="Address" />
<asp:BoundField HeaderText="HomePhone" DataField="HomePhone" />
<asp:BoundField HeaderText="Country" DataField="Country" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
後台代碼:
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Employee.FindAllByProperty("Country", "UK");
this.GridView1.DataBind();
}
最後,運行的結果如下:
內容有些簡單,後續有時間會繼續介紹Castle Active Record for .NET2.0
本文配套源碼