Castle Active Record for .NET2.0快速入门示例
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://terrylee.blog.51cto.com/342737/67670 |
一.创建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
(出处:博客园http://terrylee.cnblogs.com) 本文出自 “TerryLee技术专栏” 博客,请务必保留此出处http://terrylee.blog.51cto.com/342737/67670 本文出自 51CTO.COM技术博客 |








}
}
lihuijun
博客统计信息
热门文章
最新评论
友情链接
