Castle Active Record for .NET2.0快速入门示例

一.创建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.asaxApplication_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

完整代码下载:/Files/Terrylee/ARExamWithAspnet.rar

更多Castle的文章你可以访问:http://terrylee.cnblogs.com/archive/2006/04/28/castl_ioc_article.html

(出处:博客园http://terrylee.cnblogs.com

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值