Castle Active Record for .NET2.0快速入门示例
来源:互联网 发布:建筑行业的大数据 编辑:程序博客网 时间:2024/06/05 17:31
一.创建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; }
}
}
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>
<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);
}
{
// 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>
<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>
后台代码:
内容有些简单,后续有时间会继续介绍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)
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = Employee.FindAllByProperty("Country", "UK");
this.GridView1.DataBind();
}
最后,运行的结果如下:{
this.GridView1.DataSource = Employee.FindAllByProperty("Country", "UK");
this.GridView1.DataBind();
}
- Castle Active Record for .NET2.0快速入门示例
- Castle Active Record for .NET2.0快速入门示例
- C#.Net Castle Active Record for .NET2.0快速入门示例
- Castle windsor快速入门
- Castle IOC容器快速入门
- Castle IOC容器快速入门
- Castle IOC容器快速入门
- Castle ActiveRecord【快速入门指南】
- Castle IOC容器快速入门
- Castle IOC容器快速入门
- Active Record
- Active Record
- ASP.NET2.0快速入门视频教程
- 在asp.net2.0中使用Castle
- Yii Framework 开发教程(26) 数据库-Active Record示例
- Yii Framework 开发教程(27) 数据库-关联Active Record示例
- Yii Framework 开发教程(26) 数据库-Active Record示例
- Yii Framework 开发教程(27) 数据库-关联Active Record示例
- 我的x86系统可以安装Solaris或者OpenSolaris吗?
- Castle ActiveRecord学习实践(9):使用ActiveRecord的一些技巧
- Castle ActiveRecord学习实践(10):深度分析Schema Pitfals
- C#实现HTTP协议下的多线程文件传输
- ISO软件工程模板(6)概要设计说明书
- Castle Active Record for .NET2.0快速入门示例
- 搭建Eclipse+MyEclipse+Tomcat开发环境
- Httphandler学习中的知识点.
- BitBlt用法
- 软件工程:什么是CMMI
- PI系统API函数理解翻译
- Symbian OS Internals——Symbian OS模拟器
- 要用ASP.NET实现邮箱中附件下载的功能
- 为VC++应用程序对话框添加透明位图