Castle ActiveRecord抛弃了Nhibernate的繁杂的配置,采用了一种更加方便的方式来组织项目结构,是对Nhibernate的再次封装,大大简化了开发,下面就来说明其配置过程。
工欲善其事必先利其器,首先我们需要下载,下载地址如下:http://sourceforge.net/projects/castleproject/files/ActiveRecord/3.0/Castle.ActiveRecord-3.0.RC.zip/download 这里下载完成后,我们解压到一个文件夹中,就得到如下的文件排列形式:
然后我们新建一个工程,包含一个User的lib项目和一个winform的项目,具体组织形式如下:
步骤一、现在我们开始来建表,我们建立一个NewsDemo的数据库,然后在其下建立一个Users表,如下所示:
CREATETABLE[dbo].[Users] (
[LogonID][int]IDENTITY (1, 1) NOTNULL ,
[LogonName][varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[Password][varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[EmailAddress][varchar] (40) COLLATE Chinese_PRC_CI_AS NULL ,
[LastLogon][datetime]NULL
) ON[PRIMARY]
GO
步骤二、编写操作Users表的实体类
这个实体类名称是User.cs,具体代码如下:
using System;
using Castle.ActiveRecord;
using System.Collections;
namespace CastleLib
{
[ActiveRecord("Users")]
publicclass User:ActiveRecordBase
{
#region 公共对应属性
privateint _id;
privatestring _name;
privatestring _password;
privatestring _emailAddress;
private DateTime _lastLogon;
[PrimaryKey(PrimaryKeyType.Identity,"LogonID")]
publicint Id
{
get { return _id; }
set { _id = value; }
}
[Property("LogonName")]
publicstring Name
{
get { return _name; }
set { _name = value; }
}
[Property("Password")]
publicstring Password
{
get { return _password; }
set { _password = value; }
}
[Property("EmailAddress")]
publicstring Address
{
get { return _emailAddress; }
set { _emailAddress = value; }
}
[Property("LastLogon")]
public DateTime LastLogon
{
get { return _lastLogon; }
set { _lastLogon = value; }
}
#endregion
publicstaticvoid DeleteAll()
{
DeleteAll(typeof(User));
}
publicstatic IList FindAll()
{
return (IList)FindAll(typeof(User));
}
publicstatic User Find(int id)
{
return (User)FindByPrimaryKey(typeof(User), id);
}
}
}
在这里需要注意的是类的属性,这里是ActiveRecord("Users"),表明对应的是数据库中的Users表,而PrimaryKey则是表明主键,Property则是对应的属性列。编写的所有实体类,都要集继承自ActiveRecordBase类,这个是ActiveRecord操作的基类。上面的代码包含了一部分的CRUD操作,这些操作都是继承自ActiveRecordBase类中的,非常方便编写。
步骤三、配置映射文件
这里的配置,我是直接写到代码中的:
privatevoid Init()
{
// XmlConfigurationSource source = new XmlConfigurationSource("http://www.cnblogs.com/App.config");
//// IConfigurationSource source = ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
// ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));
InPlaceConfigurationSource source =new InPlaceConfigurationSource();
IDictionary<string, string> properties =new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2008Dialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
properties.Add("connection.connection_string", "UID=sa;Password=******;Initial Catalog=NewsDemo;Data Source=.;");
source.Add(typeof(ActiveRecordBase), properties);
ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));
}
需要注意的是,这个配置节 properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"); 一定要添加上,否则会出现未知的错误。
步骤四、操作数据库,这里我贴上具体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Castle.ActiveRecord.Framework;
using System.Configuration;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using System.Collections;
using CastleLib;
namespace CastleAPP
{
publicpartialclass MainFrm : Form
{
public MainFrm()
{
InitializeComponent();
Init();
}
privatevoid Init()
{
// XmlConfigurationSource source = new XmlConfigurationSource("http://www.cnblogs.com/App.config");
//// IConfigurationSource source = ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
// ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));
InPlaceConfigurationSource source =new InPlaceConfigurationSource();
IDictionary<string, string> properties =new Dictionary<string, string>();
properties.Add("connection.driver_class", "NHibernate.Driver.SqlClientDriver");
properties.Add("dialect", "NHibernate.Dialect.MsSql2008Dialect");
properties.Add("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
properties.Add("proxyfactory.factory_class", "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");
properties.Add("connection.connection_string", "UID=sa;Password=251147;Initial Catalog=NewsDemo;Data Source=.;");
source.Add(typeof(ActiveRecordBase), properties);
ActiveRecordStarter.Initialize(source, typeof(CastleLib.User));
}
privatevoid Form1_Load(object sender, EventArgs e)
{
AddUsers();
IList list = CastleLib.User.FindAll();
dataGridView1.DataSource = list;
}
publicvoid AddUsers()
{
CastleLib.User user =new CastleLib.User();
user.Name ="***";
user.Password ="ceshi";
user.Address ="河南省信阳市";
user.LastLogon = DateTime.Now;
user.Create();
}
}
}
这样运行的时候,我们就可以看到数据被正确的填装到数据库中了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!