第一个Castle实例(2010-01-29 23:26:50)
第一步:准备工作
1、下载AR 2.1.2.zip,地址:http://sourceforge.net/projects/castleproject/files/
2、创建数据库表(Users)
3、编写实体类
4、配置数据库连接(差点挂在上面)
5、编写代码测试(最期待的时刻)
第二步:开始工作
1、创建数据库表(Users)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 USE [CastleDB]
2 GO
3 /****** 对象: Table [dbo].[Users] 脚本日期: 01/29/2010 23:32:28 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 SET ANSI_PADDING ON
9 GO
10 CREATE TABLE [dbo].[Users](
11 [LogonID] [int] IDENTITY(1,1) NOT NULL,
12 [LogonName] [varchar](40) COLLATE Chinese_PRC_CI_AS NULL,
13 [Password] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
14 [EmailAddress] [varchar](40) COLLATE Chinese_PRC_CI_AS NULL,
15 [LastLogon] [datetime] NULL,
16 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
17 (
18 [LogonID] ASC
19 )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
20 ) ON [PRIMARY]
21
22 GO
23 SET ANSI_PADDING OFF
2 GO
3 /****** 对象: Table [dbo].[Users] 脚本日期: 01/29/2010 23:32:28 ******/
4 SET ANSI_NULLS ON
5 GO
6 SET QUOTED_IDENTIFIER ON
7 GO
8 SET ANSI_PADDING ON
9 GO
10 CREATE TABLE [dbo].[Users](
11 [LogonID] [int] IDENTITY(1,1) NOT NULL,
12 [LogonName] [varchar](40) COLLATE Chinese_PRC_CI_AS NULL,
13 [Password] [varchar](20) COLLATE Chinese_PRC_CI_AS NULL,
14 [EmailAddress] [varchar](40) COLLATE Chinese_PRC_CI_AS NULL,
15 [LastLogon] [datetime] NULL,
16 CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED
17 (
18 [LogonID] ASC
19 )WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
20 ) ON [PRIMARY]
21
22 GO
23 SET ANSI_PADDING OFF
2、编写实体类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Collections;
3 using Castle.ActiveRecord;
4
5 namespace UDS.Core.Domain
6 {
7 [ActiveRecord("Users")]
8 public class Users : ActiveRecordBase
9 {
10 #region 成员
11 private int v_LogonID;
12 private string v_LogonName;
13 private string v_Password;
14 private string v_EmailAddress;
15 private DateTime v_LastLogon;
16 #endregion
17
18 #region 属性
19 [PrimaryKey(PrimaryKeyType.Identity, "LogonID")]
20 public int LogonID
21 {
22 get { return v_LogonID; }
23 set { v_LogonID = value; }
24 }
25
26 [Property("LogonName")]
27 public string LogonName
28 {
29 get { return v_LogonName; }
30 set { v_LogonName = value; }
31 }
32
33 [Property("Password")]
34 public string Password
35 {
36 get { return v_Password; }
37 set { v_Password = value; }
38 }
39
40 [Property("EmailAddress")]
41 public string EmailAddress
42 {
43 get { return v_EmailAddress; }
44 set { v_EmailAddress = value; }
45 }
46
47 [Property("LastLogon")]
48 public DateTime LastLogon
49 {
50 get { return v_LastLogon; }
51 set { v_LastLogon = value; }
52 }
53
54 #endregion
55
56 public static void DeleteAll()
57 {
58 DeleteAll(typeof(Users));
59 }
60
61 public static IList FindAll()
62 {
63 return (IList)FindAll(typeof(Users));
64 }
65
66 public static Users Find(int id)
67 {
68 return (Users)FindByPrimaryKey(typeof(Users), id);
69 }
70 }
71 }
72
2 using System.Collections;
3 using Castle.ActiveRecord;
4
5 namespace UDS.Core.Domain
6 {
7 [ActiveRecord("Users")]
8 public class Users : ActiveRecordBase
9 {
10 #region 成员
11 private int v_LogonID;
12 private string v_LogonName;
13 private string v_Password;
14 private string v_EmailAddress;
15 private DateTime v_LastLogon;
16 #endregion
17
18 #region 属性
19 [PrimaryKey(PrimaryKeyType.Identity, "LogonID")]
20 public int LogonID
21 {
22 get { return v_LogonID; }
23 set { v_LogonID = value; }
24 }
25
26 [Property("LogonName")]
27 public string LogonName
28 {
29 get { return v_LogonName; }
30 set { v_LogonName = value; }
31 }
32
33 [Property("Password")]
34 public string Password
35 {
36 get { return v_Password; }
37 set { v_Password = value; }
38 }
39
40 [Property("EmailAddress")]
41 public string EmailAddress
42 {
43 get { return v_EmailAddress; }
44 set { v_EmailAddress = value; }
45 }
46
47 [Property("LastLogon")]
48 public DateTime LastLogon
49 {
50 get { return v_LastLogon; }
51 set { v_LastLogon = value; }
52 }
53
54 #endregion
55
56 public static void DeleteAll()
57 {
58 DeleteAll(typeof(Users));
59 }
60
61 public static IList FindAll()
62 {
63 return (IList)FindAll(typeof(Users));
64 }
65
66 public static Users Find(int id)
67 {
68 return (Users)FindByPrimaryKey(typeof(Users), id);
69 }
70 }
71 }
72
3、配置数据库连接(一定要认真),根据TerryLee的配置,及相应改正后提示:The user must provide an ADO.NET connection - NHibernate is not creating it. 知道是配置文件上出错,只要坚持没有解决不了的问题。源配数据配置文件,请到TerryLee的文章中去看,地址:http://terrylee.cnblogs.com/archive/2006/04/03/365762.html 。以下为修改后代码。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <sectionGroup>
2 <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
3 </configSections>
4 <activerecord isWeb="true">
5 <config>
6 <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
7 <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
8 <add key="connection.connection_string" value="Server=YANGAN-WU2008\SQLEXPRESS;initial catalog=CastleDB;User Id=sa;Password=7374520;"/>
9 <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
10 <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle"/>
11 </config>
12 </activerecord>
2 <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>
3 </configSections>
4 <activerecord isWeb="true">
5 <config>
6 <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
7 <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
8 <add key="connection.connection_string" value="Server=YANGAN-WU2008\SQLEXPRESS;initial catalog=CastleDB;User Id=sa;Password=7374520;"/>
9 <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
10 <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle"/>
11 </config>
12 </activerecord>
4、编写测试代码(非常简单)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Configuration;
5 using System.Data;
6 using System.Linq;
7 using System.Web;
8 using System.Web.Security;
9 using System.Web.UI;
10 using System.Web.UI.HtmlControls;
11 using System.Web.UI.WebControls;
12 using System.Web.UI.WebControls.WebParts;
13 using System.Xml.Linq;
14
15 using System.Web.Configuration;
16
17 using UDS.Core.Domain;
18
19 using Castle.ActiveRecord;
20
21 public partial class _Default : System.Web.UI.Page
22 {
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 FindAll();
26 }
27
28 public void FindAll()
29 {
30 IList lst = Users.FindAll();
31
32 Response.Write(lst.Count.ToString());
33 }
34 }
35
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Configuration;
5 using System.Data;
6 using System.Linq;
7 using System.Web;
8 using System.Web.Security;
9 using System.Web.UI;
10 using System.Web.UI.HtmlControls;
11 using System.Web.UI.WebControls;
12 using System.Web.UI.WebControls.WebParts;
13 using System.Xml.Linq;
14
15 using System.Web.Configuration;
16
17 using UDS.Core.Domain;
18
19 using Castle.ActiveRecord;
20
21 public partial class _Default : System.Web.UI.Page
22 {
23 protected void Page_Load(object sender, EventArgs e)
24 {
25 FindAll();
26 }
27
28 public void FindAll()
29 {
30 IList lst = Users.FindAll();
31
32 Response.Write(lst.Count.ToString());
33 }
34 }
35
第三步:运行,查看结果